1 /******************************************************************************\
2 Copyright (c) 2005-2019, Intel Corporation
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 
7 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 
9 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10 
11 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
12 
13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 
15 This sample was distributed or derived from the Intel's Media Samples package.
16 The original version of this sample may be obtained from https://software.intel.com/en-us/intel-media-server-studio
17 or https://software.intel.com/en-us/media-client-solutions-support.
18 \**********************************************************************************/
19 
20 #ifndef __AVC_STRUCTURES_H__
21 #define __AVC_STRUCTURES_H__
22 
23 #if defined(_WIN32) || defined(_WIN64)
24 #include <windows.h>
25 #endif
26 #include <string>
27 #include <string.h>
28 #include <vector>
29 #include "mfxstructures.h"
30 
31 namespace ProtectedLibrary
32 {
33 
34 enum
35 {
36     AVC_PROFILE_BASELINE           = 66,
37     AVC_PROFILE_MAIN               = 77,
38     AVC_PROFILE_SCALABLE_BASELINE  = 83,
39     AVC_PROFILE_SCALABLE_HIGH      = 86,
40     AVC_PROFILE_EXTENDED           = 88,
41     AVC_PROFILE_HIGH               = 100,
42     AVC_PROFILE_HIGH10             = 110,
43     AVC_PROFILE_MULTIVIEW_HIGH     = 118,
44     AVC_PROFILE_HIGH422            = 122,
45     AVC_PROFILE_STEREO_HIGH        = 128,
46     AVC_PROFILE_HIGH444            = 144,
47     AVC_PROFILE_ADVANCED444_INTRA  = 166,
48     AVC_PROFILE_ADVANCED444        = 188
49 };
50 
51 enum
52 {
53     AVC_LEVEL_1    = 10,
54     AVC_LEVEL_11   = 11,
55     AVC_LEVEL_1b   = 11,
56     AVC_LEVEL_12   = 12,
57     AVC_LEVEL_13   = 13,
58 
59     AVC_LEVEL_2    = 20,
60     AVC_LEVEL_21   = 21,
61     AVC_LEVEL_22   = 22,
62 
63     AVC_LEVEL_3    = 30,
64     AVC_LEVEL_31   = 31,
65     AVC_LEVEL_32   = 32,
66 
67     AVC_LEVEL_4    = 40,
68     AVC_LEVEL_41   = 41,
69     AVC_LEVEL_42   = 42,
70 
71     AVC_LEVEL_5    = 50,
72     AVC_LEVEL_51   = 51,
73     AVC_LEVEL_MAX  = 51,
74 
75     AVC_LEVEL_9    = 9  // for SVC profiles
76 };
77 
78 // Although the standard allows for a minimum width or height of 4, this
79 // implementation restricts the minimum value to 32.
80 
81 enum    // Valid QP range
82 {
83     AVC_QP_MAX = 51,
84     AVC_QP_MIN = 0
85 };
86 
87 enum {
88     FLD_STRUCTURE       = 0,
89     TOP_FLD_STRUCTURE   = 0,
90     BOTTOM_FLD_STRUCTURE = 1,
91     FRM_STRUCTURE   = 2,
92     AFRM_STRUCTURE  = 3
93 };
94 
95 enum DisplayPictureStruct {
96     DPS_FRAME     = 0,
97     DPS_TOP,         // one field
98     DPS_BOTTOM,      // one field
99     DPS_TOP_BOTTOM,
100     DPS_BOTTOM_TOP,
101     DPS_TOP_BOTTOM_TOP,
102     DPS_BOTTOM_TOP_BOTTOM,
103     DPS_FRAME_DOUBLING,
104     DPS_FRAME_TRIPLING
105 };
106 
107 typedef enum {
108     NAL_UT_UNSPECIFIED  = 0, // Unspecified
109     NAL_UT_SLICE     = 1, // Coded Slice - slice_layer_no_partioning_rbsp
110     NAL_UT_DPA       = 2, // Coded Data partition A - dpa_layer_rbsp
111     NAL_UT_DPB       = 3, // Coded Data partition A - dpa_layer_rbsp
112     NAL_UT_DPC       = 4, // Coded Data partition A - dpa_layer_rbsp
113     NAL_UT_IDR_SLICE = 5, // Coded Slice of a IDR Picture - slice_layer_no_partioning_rbsp
114     NAL_UT_SEI       = 6, // Supplemental Enhancement Information - sei_rbsp
115     NAL_UT_SPS       = 7, // Sequence Parameter Set - seq_parameter_set_rbsp
116     NAL_UT_PPS       = 8, // Picture Parameter Set - pic_parameter_set_rbsp
117     NAL_UT_AUD        = 9, // Access Unit Delimiter - access_unit_delimiter_rbsp
118     NAL_END_OF_SEQ   = 10, // End of sequence end_of_seq_rbsp()
119     NAL_END_OF_STREAM = 11, // End of stream end_of_stream_rbsp
120     NAL_UT_FD        = 12, // Filler Data - filler_data_rbsp
121     NAL_UT_SPS_EX    = 13, // Sequence Parameter Set Extension - seq_parameter_set_extension_rbsp
122     NAL_UNIT_PREFIX  = 14, // Prefix NAL unit in scalable extension - prefix_nal_unit_rbsp
123     NAL_UNIT_SUBSET_SPS = 15, // Subset Sequence Parameter Set - subset_seq_parameter_set_rbsp
124     NAL_UT_AUXILIARY = 19, // Auxiliary coded picture
125     NAL_UT_CODED_SLICE_EXTENSION = 20 // Coded slice in scalable extension - slice_layer_in_scalable_extension_rbsp
126 } NAL_Unit_Type;
127 
128 // Note!  The Picture Code Type values below are no longer used in the
129 // core encoder.   It only knows about slice types, and whether or not
130 // the frame is IDR, Reference or Disposable.  See enum above.
131 
132 enum EnumSliceCodType        // Permitted MB Prediction Types
133 {                        // ------------------------------------
134     PREDSLICE      = 0,    // I (Intra), P (Pred)
135     BPREDSLICE     = 1, // I, P, B (BiPred)
136     INTRASLICE     = 2,    // I
137     S_PREDSLICE    = 3,    // SP (SPred), I
138     S_INTRASLICE   = 4    // SI (SIntra), I
139 };
140 
141 typedef enum
142 {
143     SEI_BUFFERING_PERIOD_TYPE   = 0,
144     SEI_PIC_TIMING_TYPE         = 1,
145     SEI_PAN_SCAN_RECT_TYPE      = 2,
146     SEI_FILLER_TYPE             = 3,
147     SEI_USER_DATA_REGISTERED_TYPE   = 4,
148     SEI_USER_DATA_UNREGISTERED_TYPE = 5,
149     SEI_RECOVERY_POINT_TYPE         = 6,
150     SEI_DEC_REF_PIC_MARKING_TYPE    = 7,
151     SEI_SPARE_PIC_TYPE              = 8,
152     SEI_SCENE_INFO_TYPE             = 9,
153     SEI_SUB_SEQ_INFO_TYPE           = 10,
154     SEI_SUB_SEQ_LAYER_TYPE          = 11,
155     SEI_SUB_SEQ_TYPE                = 12,
156     SEI_FULL_FRAME_FREEZE_TYPE      = 13,
157     SEI_FULL_FRAME_FREEZE_RELEASE_TYPE  = 14,
158     SEI_FULL_FRAME_SNAPSHOT_TYPE        = 15,
159     SEI_PROGRESSIVE_REF_SEGMENT_START_TYPE  = 16,
160     SEI_PROGRESSIVE_REF_SEGMENT_END_TYPE    = 17,
161     SEI_MOTION_CONSTRAINED_SG_SET_TYPE      = 18,
162     SEI_RESERVED                            = 19
163 } SEI_TYPE;
164 
165 
166 #define IS_I_SLICE(SliceType) ((SliceType) == INTRASLICE)
167 #define IS_P_SLICE(SliceType) ((SliceType) == PREDSLICE || (SliceType) == S_PREDSLICE)
168 #define IS_B_SLICE(SliceType) ((SliceType) == BPREDSLICE)
169 
170 enum
171 {
172     MAX_NUM_SEQ_PARAM_SETS = 32,
173     MAX_NUM_PIC_PARAM_SETS = 256,
174 
175     MAX_SLICE_NUM       = 128, //INCREASE IF NEEDED OR SET to -1 for adaptive counting (increases memory usage)
176     MAX_NUM_REF_FRAMES  = 32,
177 
178     MAX_REF_FRAMES_IN_POC_CYCLE = 256,
179 
180     MAX_NUM_SLICE_GROUPS        = 8,
181     MAX_SLICE_GROUP_MAP_TYPE    = 6,
182 
183     NUM_INTRA_TYPE_ELEMENTS     = 16,
184 
185     COEFFICIENTS_BUFFER_SIZE    = 16 * 51,
186 
187     MINIMAL_DATA_SIZE           = 4
188 };
189 
190 // Possible values for disable_deblocking_filter_idc:
191 enum DeblockingModes_t
192 {
193     DEBLOCK_FILTER_ON                   = 0,
194     DEBLOCK_FILTER_OFF                  = 1,
195     DEBLOCK_FILTER_ON_NO_SLICE_EDGES    = 2
196 };
197 
198 #pragma pack(1)
199 
200 struct AVCScalingList4x4
201 {
202     mfxU8 ScalingListCoeffs[16];
203 };
204 
205 struct AVCScalingList8x8
206 {
207     mfxU8 ScalingListCoeffs[64];
208 };
209 
210 struct AVCWholeQPLevelScale4x4
211 {
212     mfxI16 LevelScaleCoeffs[88]/*since we do not support 422 and 444*/[16];
213 };
214 struct AVCWholeQPLevelScale8x8
215 {
216     mfxI16 LevelScaleCoeffs[88]/*since we do not support 422 and 444*/[64];
217 };
218 
219 
220 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
221 // Memory class
222 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
223 class RefCounter
224 {
225 public:
226 
RefCounter()227     RefCounter() : m_refCounter(0)
228     {
229     }
230 
~RefCounter()231     virtual ~RefCounter()
232     {
233     }
234 
IncrementReference()235     void IncrementReference() const
236     {
237         m_refCounter++;
238     }
239 
DecrementReference()240     void DecrementReference()
241     {
242         m_refCounter--;
243 
244         if (!m_refCounter)
245         {
246             Free();
247         }
248     }
249 
ResetRefCounter()250     void ResetRefCounter() {m_refCounter = 0;}
251 
GetRefCounter()252     mfxU32 GetRefCounter() {return m_refCounter;}
253 
254 protected:
255     mutable mfxI32 m_refCounter;
256 
Free()257     virtual void Free()
258     {
259     }
260 };
261 
262 class HeapObject : public RefCounter
263 {
264 public:
265 
~HeapObject()266     virtual ~HeapObject() {}
267 
Reset()268     virtual void Reset()
269     {
270     }
271 
272     virtual void Free();
273 };
274 
275 #pragma pack()
276 
277 #pragma pack(16)
278 
279 typedef mfxU32 IntraType;
280 
281 // Sequence parameter set structure, corresponding to the H.264 bitstream definition.
282 struct AVCSeqParamSetBase
283 {
284     mfxU8        profile_idc;                        // baseline, main, etc.
285     mfxU8        level_idc;
286     mfxU8        constrained_set0_flag;
287     mfxU8        constrained_set1_flag;
288     mfxU8        constrained_set2_flag;
289     mfxU8        constrained_set3_flag;
290     mfxU8        chroma_format_idc;
291     mfxU8        residual_colour_transform_flag;
292     mfxU8        bit_depth_luma;
293     mfxU8        bit_depth_chroma;
294     mfxU8        qpprime_y_zero_transform_bypass_flag;
295     mfxU8        type_of_scaling_list_used[8];
296     mfxU8        seq_scaling_matrix_present_flag;
297     AVCScalingList4x4 ScalingLists4x4[6];
298     AVCScalingList8x8 ScalingLists8x8[2];
299     mfxU8        gaps_in_frame_num_value_allowed_flag;
300     mfxU8        frame_cropping_flag;
301     mfxU32       frame_cropping_rect_left_offset;
302     mfxU32       frame_cropping_rect_right_offset;
303     mfxU32       frame_cropping_rect_top_offset;
304     mfxU32       frame_cropping_rect_bottom_offset;
305     mfxU8        more_than_one_slice_group_allowed_flag;
306     mfxU8        arbitrary_slice_order_allowed_flag;  // If zero, slice order in pictures must
307                                                       // be in increasing MB address order.
308     mfxU8        redundant_pictures_allowed_flag;
309     mfxU8        seq_parameter_set_id;                // id of this sequence parameter set
310     mfxU8        log2_max_frame_num;                  // Number of bits to hold the frame_num
311     mfxU8        pic_order_cnt_type;                  // Picture order counting method
312 
313     mfxU8        delta_pic_order_always_zero_flag;    // If zero, delta_pic_order_cnt fields are
314                                                       // present in slice header.
315     mfxU8        frame_mbs_only_flag;                 // Nonzero indicates all pictures in sequence
316                                                       // are coded as frames (not fields).
317     mfxU8        required_frame_num_update_behavior_flag;
318 
319     mfxU8        mb_adaptive_frame_field_flag;        // Nonzero indicates frame/field switch
320                                                       // at macroblock level
321     mfxU8        direct_8x8_inference_flag;           // Direct motion vector derivation method
322     mfxU8        vui_parameters_present_flag;         // Zero indicates default VUI parameters
323     mfxU32       log2_max_pic_order_cnt_lsb;          // Value of MaxPicOrderCntLsb.
324     mfxI32       offset_for_non_ref_pic;
325 
326     mfxI32       offset_for_top_to_bottom_field;      // Expected pic order count difference from
327                                                       // top field to bottom field.
328 
329     mfxU32       num_ref_frames_in_pic_order_cnt_cycle;
330     mfxU32       num_ref_frames;                      // total number of pics in decoded pic buffer
331     mfxU32       frame_width_in_mbs;
332     mfxU32       frame_height_in_mbs;
333 
334     // These fields are calculated from values above.  They are not written to the bitstream
335     mfxU32       MaxMbAddress;
336     mfxU32       MaxPicOrderCntLsb;
337     // vui part
338     mfxU8        aspect_ratio_info_present_flag;
339     mfxU8        aspect_ratio_idc;
340     mfxU16       sar_width;
341     mfxU16       sar_height;
342     mfxU8        overscan_info_present_flag;
343     mfxU8        overscan_appropriate_flag;
344     mfxU8        video_signal_type_present_flag;
345     mfxU8        video_format;
346     mfxU8        video_full_range_flag;
347     mfxU8        colour_description_present_flag;
348     mfxU8        colour_primaries;
349     mfxU8        transfer_characteristics;
350     mfxU8        matrix_coefficients;
351     mfxU8        chroma_loc_info_present_flag;
352     mfxU8        chroma_sample_loc_type_top_field;
353     mfxU8        chroma_sample_loc_type_bottom_field;
354     mfxU8        timing_info_present_flag;
355     mfxU32       num_units_in_tick;
356     mfxU32       time_scale;
357     mfxU8        fixed_frame_rate_flag;
358     mfxU8        nal_hrd_parameters_present_flag;
359     mfxU8        vcl_hrd_parameters_present_flag;
360     mfxU8        low_delay_hrd_flag;
361     mfxU8        pic_struct_present_flag;
362     mfxU8        bitstream_restriction_flag;
363     mfxU8        motion_vectors_over_pic_boundaries_flag;
364     mfxU8        max_bytes_per_pic_denom;
365     mfxU8        max_bits_per_mb_denom;
366     mfxU8        log2_max_mv_length_horizontal;
367     mfxU8        log2_max_mv_length_vertical;
368     mfxU8        num_reorder_frames;
369     mfxU8        max_dec_frame_buffering;
370     //hrd_parameters
371     mfxU8        cpb_cnt;
372     mfxU8        bit_rate_scale;
373     mfxU8        cpb_size_scale;
374     mfxU32       bit_rate_value[32];
375     mfxU32       cpb_size_value[32];
376     mfxU8        cbr_flag[32];
377     mfxU8        initial_cpb_removal_delay_length;
378     mfxU8        cpb_removal_delay_length;
379     mfxU8        dpb_output_delay_length;
380     mfxU8        time_offset_length;
381 
382     mfxI32       poffset_for_ref_frame[MAX_REF_FRAMES_IN_POC_CYCLE];  // for pic order cnt type 1
383                                                       // length num_stored_frames_in_pic_order_cnt_cycle,
ResetAVCSeqParamSetBase384     void Reset()
385     {
386         AVCSeqParamSetBase sps = {0};
387         *this = sps;
388     }
389 
390 };    // AVCSeqParamSetBase
391 
392 // Sequence parameter set structure, corresponding to the H.264 bitstream definition.
393 struct AVCSeqParamSet : public HeapObject, public AVCSeqParamSetBase
394 {
AVCSeqParamSetAVCSeqParamSet395     AVCSeqParamSet()
396         : HeapObject()
397         , AVCSeqParamSetBase()
398     {
399         Reset();
400     }
401 
~AVCSeqParamSetAVCSeqParamSet402     ~AVCSeqParamSet()
403     {
404     }
405 
GetIDAVCSeqParamSet406     mfxI32 GetID() const
407     {
408         return seq_parameter_set_id;
409     }
410 
ResetAVCSeqParamSet411     virtual void Reset()
412     {
413         AVCSeqParamSetBase::Reset();
414 
415         seq_parameter_set_id = MAX_NUM_SEQ_PARAM_SETS;
416 
417         // set some parameters by default
418         video_format = 5; // unspecified
419         video_full_range_flag = 0;
420         colour_primaries = 2; // unspecified
421         transfer_characteristics = 2; // unspecified
422         matrix_coefficients = 2; // unspecified
423     }
424 };    // AVCSeqParamSet
425 
426 // Sequence parameter set extension structure, corresponding to the H.264 bitstream definition.
427 struct AVCSeqParamSetExtension
428 {
429     mfxU8       seq_parameter_set_id;
430     mfxU8       aux_format_idc;
431     mfxU8       bit_depth_aux;
432     mfxU8       alpha_incr_flag;
433     mfxU8       alpha_opaque_value;
434     mfxU8       alpha_transparent_value;
435     mfxU8       additional_extension_flag;
436 
AVCSeqParamSetExtensionAVCSeqParamSetExtension437     AVCSeqParamSetExtension()
438     {
439         Reset();
440     }
441 
ResetAVCSeqParamSetExtension442     virtual void Reset()
443     {
444         aux_format_idc = 0;
445         seq_parameter_set_id = MAX_NUM_SEQ_PARAM_SETS;    // illegal id
446         bit_depth_aux = 0;
447         alpha_incr_flag = 0;
448         alpha_opaque_value = 0;
449         alpha_transparent_value = 0;
450         additional_extension_flag = 0;
451     }
452 
GetIDAVCSeqParamSetExtension453     mfxI32 GetID() const
454     {
455         return seq_parameter_set_id;
456     }
457 
~AVCSeqParamSetExtensionAVCSeqParamSetExtension458     virtual ~AVCSeqParamSetExtension(){}
459 
460 };    // AVCSeqParamSetExtension
461 
462 // Picture parameter set structure, corresponding to the H.264 bitstream definition.
463 struct AVCPicParamSetBase
464 {
465 // Flexible macroblock order structure, defining the FMO map for a picture
466 // paramter set.
467 
468     struct SliceGroupInfoStruct
469     {
470         mfxU8        slice_group_map_type;                // 0..6
471 
472         // The additional slice group data depends upon map type
473         union
474         {
475             // type 0
476             mfxU32    run_length[MAX_NUM_SLICE_GROUPS];
477 
478             // type 2
479             struct
480             {
481                 mfxU32 top_left[MAX_NUM_SLICE_GROUPS-1];
482                 mfxU32 bottom_right[MAX_NUM_SLICE_GROUPS-1];
483             }t1;
484 
485             // types 3-5
486             struct
487             {
488                 mfxU8  slice_group_change_direction_flag;
489                 mfxU32 slice_group_change_rate;
490             }t2;
491 
492             // type 6
493             struct
494             {
495                 mfxU32 pic_size_in_map_units;     // number of macroblocks if no field coding
496             }t3;
497         };
498 
499         std::vector<mfxU8> pSliceGroupIDMap;          // Id for each slice group map unit (for t3 struct of)
500     };    // SliceGroupInfoStruct
501 
502     mfxU16       pic_parameter_set_id;            // of this picture parameter set
503     mfxU8        seq_parameter_set_id;            // of seq param set used for this pic param set
504     mfxU8        entropy_coding_mode;             // zero: CAVLC, else CABAC
505 
506     mfxU8        pic_order_present_flag;          // Zero indicates only delta_pic_order_cnt[0] is
507                                                   // present in slice header; nonzero indicates
508                                                   // delta_pic_order_cnt[1] is also present.
509 
510     mfxU8        weighted_pred_flag;              // Nonzero indicates weighted prediction applied to
511                                                   // P and SP slices
512     mfxU8        weighted_bipred_idc;             // 0: no weighted prediction in B slices
513                                                   // 1: explicit weighted prediction
514                                                   // 2: implicit weighted prediction
515     mfxI8        pic_init_qp;                     // default QP for I,P,B slices
516     mfxI8        pic_init_qs;                     // default QP for SP, SI slices
517 
518     mfxI8        chroma_qp_index_offset[2];       // offset to add to QP for chroma
519 
520     mfxU8        deblocking_filter_variables_present_flag;    // If nonzero, deblock filter params are
521                                                   // present in the slice header.
522     mfxU8        constrained_intra_pred_flag;     // Nonzero indicates constrained intra mode
523 
524     mfxU8        redundant_pic_cnt_present_flag;  // Nonzero indicates presence of redundant_pic_cnt
525                                                   // in slice header
526     mfxU32       num_slice_groups;                // One: no FMO
527     mfxU32       num_ref_idx_l0_active;           // num of ref pics in list 0 used to decode the picture
528     mfxU32       num_ref_idx_l1_active;           // num of ref pics in list 1 used to decode the picture
529     mfxU8        transform_8x8_mode_flag;
530     mfxU8        type_of_scaling_list_used[8];
531 
532     AVCScalingList4x4 ScalingLists4x4[6];
533     AVCScalingList8x8 ScalingLists8x8[2];
534 
535     // Level Scale addition
536     AVCWholeQPLevelScale4x4        m_LevelScale4x4[6];
537     AVCWholeQPLevelScale8x8        m_LevelScale8x8[2];
538 
539     SliceGroupInfoStruct SliceGroupInfo;    // Used only when num_slice_groups > 1
540 
ResetAVCPicParamSetBase541     void Reset()
542     {
543         AVCPicParamSetBase pps = {0};
544         *this = pps;
545     }
546 };    // AVCPicParamSet
547 
548 // Picture parameter set structure, corresponding to the H.264 bitstream definition.
549 struct AVCPicParamSet : public HeapObject, public AVCPicParamSetBase
550 {
AVCPicParamSetAVCPicParamSet551     AVCPicParamSet()
552         : AVCPicParamSetBase()
553     {
554         Reset();
555     }
556 
ResetAVCPicParamSet557     void Reset()
558     {
559         AVCPicParamSetBase::Reset();
560 
561         pic_parameter_set_id = MAX_NUM_PIC_PARAM_SETS;
562         seq_parameter_set_id = MAX_NUM_SEQ_PARAM_SETS;
563         num_slice_groups = 0;
564         SliceGroupInfo.pSliceGroupIDMap.clear();
565     }
566 
~AVCPicParamSetAVCPicParamSet567     ~AVCPicParamSet()
568     {
569     }
570 
GetIDAVCPicParamSet571     mfxI32 GetID() const
572     {
573         return pic_parameter_set_id;
574     }
575 
576 };    // H264PicParamSet
577 
578 struct RefPicListReorderInfo
579 {
580     mfxU32       num_entries;                 // number of currently valid idc,value pairs
581     mfxU8        reordering_of_pic_nums_idc[MAX_NUM_REF_FRAMES];
582     mfxU32       reorder_value[MAX_NUM_REF_FRAMES];    // abs_diff_pic_num or long_term_pic_num
583 };
584 
585 struct AdaptiveMarkingInfo
586 {
587     mfxU32       num_entries;                 // number of currently valid mmco,value pairs
588     mfxU8        mmco[MAX_NUM_REF_FRAMES];    // memory management control operation id
589     mfxU32       value[MAX_NUM_REF_FRAMES*2]; // operation-dependent data, max 2 per operation
590 };
591 
592 struct PredWeightTable
593 {
594     mfxU8        luma_weight_flag;            // nonzero: luma weight and offset in bitstream
595     mfxU8        chroma_weight_flag;          // nonzero: chroma weight and offset in bitstream
596     mfxI8        luma_weight;                 // luma weighting factor
597     mfxI8        luma_offset;                 // luma weighting offset
598     mfxI8        chroma_weight[2];            // chroma weighting factor (Cb,Cr)
599     mfxI8        chroma_offset[2];            // chroma weighting offset (Cb,Cr)
600 };    // PredWeightTable
601 
602 typedef mfxI32 AVCDecoderMBAddr;
603 // NAL unit SVC extension structure
604 struct AVCNalSvcExtension
605 {
606     mfxU8 idr_flag;
607     mfxU8 priority_id;
608     mfxU8 no_inter_layer_pred_flag;
609     mfxU8 dependency_id;
610     mfxU8 quality_id;
611     mfxU8 temporal_id;
612     mfxU8 use_ref_base_pic_flag;
613     mfxU8 discardable_flag;
614     mfxU8 output_flag;
615 
616     mfxU8 store_ref_base_pic_flag;
617     mfxU8 adaptive_ref_base_pic_marking_mode_flag;
618     AdaptiveMarkingInfo adaptiveMarkingInfo;
619 };
620 
621 // NAL unit SVC extension structure
622 struct AVCNalMvcExtension
623 {
624     mfxU8 non_idr_flag;
625     mfxU16 priority_id;
626     // view_id variable is duplicated to the slice header itself
627     mfxU16 view_id;
628     mfxU8 temporal_id;
629     mfxU8 anchor_pic_flag;
630     mfxU8 inter_view_flag;
631 
632     mfxU8 padding[3];
633 };
634 
635 // NAL unit extension structure
636 struct AVCNalExtension
637 {
638     mfxU8 extension_present;
639 
640     // equal to 1 specifies that NAL extension contains SVC related parameters
641     mfxU8 svc_extension_flag;
642 
643     union
644     {
645         AVCNalSvcExtension svc;
646         AVCNalMvcExtension mvc;
647     };
648 };
649 
650 // Slice header structure, corresponding to the H.264 bitstream definition.
651 struct AVCSliceHeader
652 {
653     // flag equal 1 means that the slice belong to IDR or anchor access unit
654     mfxU32 IdrPicFlag;
655 
656     // specified that NAL unit contains any information accessed from
657     // the decoding process of other NAL units.
658     mfxU8  nal_ref_idc;
659     // specifies the type of RBSP data structure contained in the NAL unit as
660     // specified in Table 7-1 of h264 standard
661     NAL_Unit_Type nal_unit_type;
662 
663     // NAL unit extension parameters
664     AVCNalExtension nal_ext;
665     mfxU32 view_id;
666 
667     mfxU16        pic_parameter_set_id;                 // of pic param set used for this slice
668     mfxU8         field_pic_flag;                       // zero: frame picture, else field picture
669     mfxU8         MbaffFrameFlag;
670     mfxU8         bottom_field_flag;                    // zero: top field, else bottom field
671     mfxU8         direct_spatial_mv_pred_flag;          // zero: temporal direct, else spatial direct
672     mfxU8         num_ref_idx_active_override_flag;     // nonzero: use ref_idx_active from slice header
673                                                         // instead of those from pic param set
674     mfxU8         no_output_of_prior_pics_flag;         // nonzero: remove previously decoded pictures
675                                                         // from decoded picture buffer
676     mfxU8         long_term_reference_flag;             // How to set MaxLongTermFrameIdx
677     mfxU32        cabac_init_idc;                      // CABAC initialization table index (0..2)
678     mfxU8         adaptive_ref_pic_marking_mode_flag;   // Ref pic marking mode of current picture
679     mfxI32        slice_qp_delta;                       // to calculate default slice QP
680     mfxU8         sp_for_switch_flag;                   // SP slice decoding control
681     mfxI32        slice_qs_delta;                       // to calculate default SP,SI slice QS
682     mfxU32        disable_deblocking_filter_idc;       // deblock filter control, 0=filter all edges
683     mfxI32        slice_alpha_c0_offset;               // deblock filter c0, alpha table offset
684     mfxI32        slice_beta_offset;                   // deblock filter beta table offset
685     AVCDecoderMBAddr first_mb_in_slice;
686     mfxI32        frame_num;
687     EnumSliceCodType slice_type;
688     mfxU32        idr_pic_id;                           // ID of an IDR picture
689     mfxI32        pic_order_cnt_lsb;                    // picture order count (mod MaxPicOrderCntLsb)
690     mfxI32        delta_pic_order_cnt_bottom;           // Pic order count difference, top & bottom fields
691     mfxU32        difference_of_pic_nums;               // Ref pic memory mgmt
692     mfxU32        long_term_pic_num;                    // Ref pic memory mgmt
693     mfxU32        long_term_frame_idx;                  // Ref pic memory mgmt
694     mfxU32        max_long_term_frame_idx;              // Ref pic memory mgmt
695     mfxI32        delta_pic_order_cnt[2];               // picture order count differences
696     mfxU32        redundant_pic_cnt;                    // for redundant slices
697     mfxI32        num_ref_idx_l0_active;                // num of ref pics in list 0 used to decode the slice,
698                                                         // see num_ref_idx_active_override_flag
699     mfxI32        num_ref_idx_l1_active;                // num of ref pics in list 1 used to decode the slice
700                                                         // see num_ref_idx_active_override_flag
701     mfxU32        slice_group_change_cycle;             // for FMO
702     mfxU8         luma_log2_weight_denom;               // luma weighting denominator
703     mfxU8         chroma_log2_weight_denom;             // chroma weighting denominator
704 
705     bool          is_auxiliary;
706 }; // AVCSliceHeader
707 
708 
709 struct AVCSEIPayLoadBase
710 {
711     SEI_TYPE payLoadType;
712     mfxU32   payLoadSize;
713 
714     union SEIMessages
715     {
716         struct BufferingPeriod
717         {
718             mfxU32 initial_cbp_removal_delay[2][16];
719             mfxU32 initial_cbp_removal_delay_offset[2][16];
720         }buffering_period;
721 
722         struct PicTiming
723         {
724             mfxU32 cbp_removal_delay;
725             mfxU32 dpb_ouput_delay;
726             DisplayPictureStruct pic_struct;
727             mfxU8  clock_timestamp_flag[16];
728             struct ClockTimestamps
729             {
730                 mfxU8 ct_type;
731                 mfxU8 nunit_field_based_flag;
732                 mfxU8 counting_type;
733                 mfxU8 full_timestamp_flag;
734                 mfxU8 discontinuity_flag;
735                 mfxU8 cnt_dropped_flag;
736                 mfxU8 n_frames;
737                 mfxU8 seconds_value;
738                 mfxU8 minutes_value;
739                 mfxU8 hours_value;
740                 mfxU8 time_offset;
741             }clock_timestamps[16];
742         }pic_timing;
743 
744         struct PanScanRect
745         {
746             mfxU8  pan_scan_rect_id;
747             mfxU8  pan_scan_rect_cancel_flag;
748             mfxU8  pan_scan_cnt;
749             mfxU32 pan_scan_rect_left_offset[32];
750             mfxU32 pan_scan_rect_right_offset[32];
751             mfxU32 pan_scan_rect_top_offset[32];
752             mfxU32 pan_scan_rect_bottom_offset[32];
753             mfxU8  pan_scan_rect_repetition_period;
754         }pan_scan_rect;
755 
756         struct UserDataRegistered
757         {
758             mfxU8 itu_t_t35_country_code;
759             mfxU8 itu_t_t35_country_code_extension_byte;
760         } user_data_registered;
761 
762         struct RecoveryPoint
763         {
764             mfxU8 recovery_frame_cnt;
765             mfxU8 exact_match_flag;
766             mfxU8 broken_link_flag;
767             mfxU8 changing_slice_group_idc;
768         }recovery_point;
769 
770         struct DecRefPicMarkingRepetition
771         {
772             mfxU8 original_idr_flag;
773             mfxU8 original_frame_num;
774             mfxU8 original_field_pic_flag;
775             mfxU8 original_bottom_field_flag;
776             mfxU8 long_term_reference_flag;
777             AdaptiveMarkingInfo adaptiveMarkingInfo;
778         }dec_ref_pic_marking_repetition;
779 
780         struct SparePic
781         {
782             mfxU32 target_frame_num;
783             mfxU8  spare_field_flag;
784             mfxU8  target_bottom_field_flag;
785             mfxU8  num_spare_pics;
786             mfxU8  delta_spare_frame_num[16];
787             mfxU8  spare_bottom_field_flag[16];
788             mfxU8  spare_area_idc[16];
789             mfxU8  *spare_unit_flag[16];
790             mfxU8  *zero_run_length[16];
791         }spare_pic;
792 
793         struct SceneInfo
794         {
795             mfxU8 scene_info_present_flag;
796             mfxU8 scene_id;
797             mfxU8 scene_transition_type;
798             mfxU8 second_scene_id;
799         }scene_info;
800 
801         struct SubSeqInfo
802         {
803             mfxU8 sub_seq_layer_num;
804             mfxU8 sub_seq_id;
805             mfxU8 first_ref_pic_flag;
806             mfxU8 leading_non_ref_pic_flag;
807             mfxU8 last_pic_flag;
808             mfxU8 sub_seq_frame_num_flag;
809             mfxU8 sub_seq_frame_num;
810         }sub_seq_info;
811 
812         struct SubSeqLayerCharacteristics
813         {
814             mfxU8  num_sub_seq_layers;
815             mfxU8  accurate_statistics_flag[16];
816             mfxU16 average_bit_rate[16];
817             mfxU16 average_frame_rate[16];
818         }sub_seq_layer_characteristics;
819 
820         struct SubSeqCharacteristics
821         {
822             mfxU8  sub_seq_layer_num;
823             mfxU8  sub_seq_id;
824             mfxU8  duration_flag;
825             mfxU8  sub_seq_duration;
826             mfxU8  average_rate_flag;
827             mfxU8  accurate_statistics_flag;
828             mfxU16 average_bit_rate;
829             mfxU16 average_frame_rate;
830             mfxU8  num_referenced_subseqs;
831             mfxU8  ref_sub_seq_layer_num[16];
832             mfxU8  ref_sub_seq_id[16];
833             mfxU8  ref_sub_seq_direction[16];
834         }sub_seq_characteristics;
835 
836         struct FullFrameFreeze
837         {
838             mfxU32 full_frame_freeze_repetition_period;
839         }full_frame_freeze;
840 
841         struct FullFrameSnapshot
842         {
843             mfxU8 snapshot_id;
844         }full_frame_snapshot;
845 
846         struct ProgressiveRefinementSegmentStart
847         {
848             mfxU8 progressive_refinement_id;
849             mfxU8 num_refinement_steps;
850         }progressive_refinement_segment_start;
851 
852         struct MotionConstrainedSliceGroupSet
853         {
854             mfxU8 num_slice_groups_in_set;
855             mfxU8 slice_group_id[8];
856             mfxU8 exact_sample_value_match_flag;
857             mfxU8 pan_scan_rect_flag;
858             mfxU8 pan_scan_rect_id;
859         }motion_constrained_slice_group_set;
860 
861         struct FilmGrainCharacteristics
862         {
863             mfxU8 film_grain_characteristics_cancel_flag;
864             mfxU8 model_id;
865             mfxU8 separate_colour_description_present_flag;
866             mfxU8 film_grain_bit_depth_luma;
867             mfxU8 film_grain_bit_depth_chroma;
868             mfxU8 film_grain_full_range_flag;
869             mfxU8 film_grain_colour_primaries;
870             mfxU8 film_grain_transfer_characteristics;
871             mfxU8 film_grain_matrix_coefficients;
872             mfxU8 blending_mode_id;
873             mfxU8 log2_scale_factor;
874             mfxU8 comp_model_present_flag[3];
875             mfxU8 num_intensity_intervals[3];
876             mfxU8 num_model_values[3];
877             mfxU8 intensity_interval_lower_bound[3][256];
878             mfxU8 intensity_interval_upper_bound[3][256];
879             mfxU8 comp_model_value[3][3][256];
880             mfxU8 film_grain_characteristics_repetition_period;
881         }film_grain_characteristics;
882 
883         struct DeblockingFilterDisplayPreference
884         {
885             mfxU8 deblocking_display_preference_cancel_flag;
886             mfxU8 display_prior_to_deblocking_preferred_flag;
887             mfxU8 dec_frame_buffering_constraint_flag;
888             mfxU8 deblocking_display_preference_repetition_period;
889         }deblocking_filter_display_preference;
890 
891         struct StereoVideoInfo
892         {
893             mfxU8 field_views_flag;
894             mfxU8 top_field_is_left_view_flag;
895             mfxU8 current_frame_is_left_view_flag;
896             mfxU8 next_frame_is_second_view_flag;
897             mfxU8 left_view_self_contained_flag;
898             mfxU8 right_view_self_contained_flag;
899         }stereo_video_info;
900 
901     }SEI_messages;
902 
ResetAVCSEIPayLoadBase903     void Reset()
904     {
905         memset(this, 0, sizeof(AVCSEIPayLoadBase));
906 
907         payLoadType = SEI_RESERVED;
908         payLoadSize = 0;
909     }
910 };
911 
912 struct AVCSEIPayLoad : public HeapObject, public AVCSEIPayLoadBase
913 {
914     std::vector<mfxU8> user_data; // for UserDataRegistered or UserDataUnRegistered
915 
AVCSEIPayLoadAVCSEIPayLoad916     AVCSEIPayLoad()
917         : AVCSEIPayLoadBase()
918     {
919     }
920 
ResetAVCSEIPayLoad921     virtual void Reset()
922     {
923         AVCSEIPayLoadBase::Reset();
924         user_data.clear();
925     }
926 
GetIDAVCSEIPayLoad927     mfxI32 GetID() const
928     {
929         return payLoadType;
930     }
931 };
932 
933 #pragma pack()
934 
935 // This file defines some data structures and constants used by the decoder,
936 // that are also needed by other classes, such as post filters and
937 // error concealment.
938 
939 #define INTERP_FACTOR 4
940 #define INTERP_SHIFT 2
941 
942 #define CHROMA_INTERP_FACTOR 8
943 #define CHROMA_INTERP_SHIFT 3
944 
945 // at picture edge, clip motion vectors to only this far beyond the edge,
946 // in pixel units.
947 #define D_MV_CLIP_LIMIT 19
948 
949 enum Direction_t{
950     D_DIR_FWD = 0,
951     D_DIR_BWD = 1,
952     D_DIR_BIDIR = 2,
953     D_DIR_DIRECT = 3,
954     D_DIR_DIRECT_SPATIAL_FWD = 4,
955     D_DIR_DIRECT_SPATIAL_BWD = 5,
956     D_DIR_DIRECT_SPATIAL_BIDIR = 6
957 };
958 
IsForwardOnly(mfxI32 direction)959 inline bool IsForwardOnly(mfxI32 direction)
960 {
961     return (direction == D_DIR_FWD) || (direction == D_DIR_DIRECT_SPATIAL_FWD);
962 }
963 
IsHaveForward(mfxI32 direction)964 inline bool IsHaveForward(mfxI32 direction)
965 {
966     return (direction == D_DIR_FWD) || (direction == D_DIR_BIDIR) ||
967         (direction == D_DIR_DIRECT_SPATIAL_FWD) || (direction == D_DIR_DIRECT_SPATIAL_BIDIR) ||
968          (direction == D_DIR_DIRECT);
969 }
970 
IsBackwardOnly(mfxI32 direction)971 inline bool IsBackwardOnly(mfxI32 direction)
972 {
973     return (direction == D_DIR_BWD) || (direction == D_DIR_DIRECT_SPATIAL_BWD);
974 }
975 
IsHaveBackward(mfxI32 direction)976 inline bool IsHaveBackward(mfxI32 direction)
977 {
978     return (direction == D_DIR_BWD) || (direction == D_DIR_BIDIR) ||
979         (direction == D_DIR_DIRECT_SPATIAL_BWD) || (direction == D_DIR_DIRECT_SPATIAL_BIDIR) ||
980         (direction == D_DIR_DIRECT);
981 }
982 
IsBidirOnly(mfxI32 direction)983 inline bool IsBidirOnly(mfxI32 direction)
984 {
985     return (direction == D_DIR_BIDIR) || (direction == D_DIR_DIRECT_SPATIAL_BIDIR) ||
986         (direction == D_DIR_DIRECT);
987 }
988 
989 // Warning: If these bit defines change, also need to change same
990 // defines  and related code in sresidual.s.
991 enum CBP
992 {
993     D_CBP_LUMA_DC = 0x00001,
994     D_CBP_LUMA_AC = 0x1fffe,
995 
996     D_CBP_CHROMA_DC = 0x00001,
997     D_CBP_CHROMA_AC = 0x1fffe,
998     D_CBP_CHROMA_AC_420 = 0x0001e,
999     D_CBP_CHROMA_AC_422 = 0x001fe,
1000     D_CBP_CHROMA_AC_444 = 0x1fffe,
1001 
1002     D_CBP_1ST_LUMA_AC_BITPOS = 1,
1003     D_CBP_1ST_CHROMA_DC_BITPOS = 17,
1004     D_CBP_1ST_CHROMA_AC_BITPOS = 19
1005 };
1006 
1007 enum
1008 {
1009     FIRST_DC_LUMA = 0,
1010     FIRST_AC_LUMA = 1,
1011     FIRST_DC_CHROMA = 17,
1012     FIRST_AC_CHROMA = 19
1013 };
1014 
1015 enum
1016 {
1017     CHROMA_FORMAT_400       = 0,
1018     CHROMA_FORMAT_420       = 1,
1019     CHROMA_FORMAT_422       = 2,
1020     CHROMA_FORMAT_444       = 3
1021 };
1022 
1023 class AVC_exception
1024 {
1025 public:
1026     AVC_exception(mfxI32 status = -1)
m_Status(status)1027         : m_Status(status)
1028     {
1029     }
1030 
~AVC_exception()1031     virtual ~AVC_exception()
1032     {
1033     }
1034 
GetStatus()1035     mfxI32 GetStatus() const
1036     {
1037         return m_Status;
1038     }
1039 
1040 private:
1041     mfxI32 m_Status;
1042 };
1043 
1044 
1045 #pragma pack(1)
1046 
1047 extern mfxI32 lock_failed;
1048 
1049 #pragma pack()
1050 
1051 template <typename T>
AVC_new_array_throw(mfxI32 size)1052 inline T * AVC_new_array_throw(mfxI32 size)
1053 {
1054     T * t = new T[size];
1055     if (!t)
1056         throw AVC_exception(MFX_ERR_MEMORY_ALLOC);
1057     return t;
1058 }
1059 
1060 template <typename T>
AVC_new_throw()1061 inline T * AVC_new_throw()
1062 {
1063     T * t = new T();
1064     if (!t)
1065         throw AVC_exception(MFX_ERR_MEMORY_ALLOC);
1066     return t;
1067 }
1068 
1069 template <typename T, typename T1>
AVC_new_throw_1(T1 t1)1070 inline T * AVC_new_throw_1(T1 t1)
1071 {
1072     T * t = new T(t1);
1073     if (!t)
1074         throw AVC_exception(MFX_ERR_MEMORY_ALLOC);
1075     return t;
1076 }
1077 
CalculateSuggestedSize(const AVCSeqParamSet * sps)1078 inline mfxU32 CalculateSuggestedSize(const AVCSeqParamSet * sps)
1079 {
1080     mfxU32 base_size = sps->frame_width_in_mbs * sps->frame_height_in_mbs * 256;
1081     mfxU32 size = 0;
1082 
1083     switch (sps->chroma_format_idc)
1084     {
1085     case 0:  // YUV400
1086         size = base_size;
1087         break;
1088     case 1:  // YUV420
1089         size = (base_size * 3) / 2;
1090         break;
1091     case 2: // YUV422
1092         size = base_size + base_size;
1093         break;
1094     case 3: // YUV444
1095         size = base_size + base_size + base_size;
1096         break;
1097     };
1098 
1099     return size;
1100 }
1101 
1102 #define SAMPLE_ASSERT(x)
1103 
1104 } // namespace ProtectedLibrary
1105 
1106 
1107 #endif // __AVC_STRUCTURES_H__
1108