1 /*
2 * Copyright(c) 2018 Intel Corporation
3 * SPDX - License - Identifier: BSD - 2 - Clause - Patent
4 */
5 
6 #include <stdlib.h>
7 #include <string.h>
8 
9 #include "EbSei.h"
10 
EbVideoUsabilityInfoCopy(AppVideoUsabilityInfo_t * dstVuiPtr,AppVideoUsabilityInfo_t * srcVuiPtr)11 void EbVideoUsabilityInfoCopy(
12     AppVideoUsabilityInfo_t *dstVuiPtr,
13     AppVideoUsabilityInfo_t *srcVuiPtr)
14 {
15     size_t sizeCopy = (size_t)((EB_U64)(&dstVuiPtr->hrdParametersPtr) -
16         (EB_U64)(&dstVuiPtr->aspectRatioInfoPresentFlag));
17 
18     EB_MEMCPY(dstVuiPtr, srcVuiPtr, sizeCopy);
19     EB_MEMCPY(dstVuiPtr->hrdParametersPtr, srcVuiPtr->hrdParametersPtr, sizeof(AppHrdParameters_t));
20 
21     return;
22 }
23 
EbVideoUsabilityInfoDctor(EB_PTR p)24 static void EbVideoUsabilityInfoDctor(EB_PTR p)
25 {
26     AppVideoUsabilityInfo_t *obj = (AppVideoUsabilityInfo_t*)p;
27     EB_FREE(obj->hrdParametersPtr);
28 }
29 
EbVideoUsabilityInfoCtor(AppVideoUsabilityInfo_t * vuiPtr)30 EB_ERRORTYPE EbVideoUsabilityInfoCtor(
31     AppVideoUsabilityInfo_t *vuiPtr)
32 {
33     EB_CALLOC(vuiPtr->hrdParametersPtr, 1, sizeof(AppHrdParameters_t));
34     // Initialize vui variables
35     vuiPtr->dctor = EbVideoUsabilityInfoDctor;
36     vuiPtr->aspectRatioInfoPresentFlag = EB_TRUE;
37     vuiPtr->defaultDisplayWindowFlag = EB_TRUE;
38     vuiPtr->hrdParametersPtr->cpbDpbDelaysPresentFlag = (EB_BOOL)((vuiPtr->hrdParametersPtr->nalHrdParametersPresentFlag || vuiPtr->hrdParametersPtr->vclHrdParametersPresentFlag)
39                                                                    && vuiPtr->vuiHrdParametersPresentFlag);
40 
41     return EB_ErrorNone;
42 }
43 
44 /**************************************************
45  * GetUvlcCodeLength
46  **************************************************/
GetUvlcCodeLength(EB_U32 code)47 EB_U32 GetUvlcCodeLength(
48     EB_U32      code)
49 {
50     EB_U32 numberOfBits = 1;
51     EB_U32 tempBits = ++code;
52 
53     while( 1 != tempBits )
54     {
55         tempBits >>= 1;
56         numberOfBits += 2;
57     }
58 
59     return numberOfBits;
60 }
61 
62 /**************************************************
63  * GetUvlcCodeLength
64  **************************************************/
GetSvlcCodeLength(EB_S32 code)65 EB_U32 GetSvlcCodeLength(
66     EB_S32      code)
67 {
68     EB_U32 numberOfBits = 1;
69     EB_U32 tempBits;
70     EB_U32 bits;
71 
72     bits = (code <= 0) ? -code<<1 : (code<<1)-1;;
73 
74     tempBits = ++bits;
75 
76     while( 1 != tempBits )
77     {
78         tempBits >>= 1;
79         numberOfBits += 2;
80     }
81 
82     return numberOfBits;
83 }
84 
GetPictureTimingSEILength(AppPictureTimingSei_t * picTimingSeiPtr,AppVideoUsabilityInfo_t * vuiPtr)85 EB_U32 GetPictureTimingSEILength(
86     AppPictureTimingSei_t      *picTimingSeiPtr,
87     AppVideoUsabilityInfo_t    *vuiPtr)
88 {
89     EB_U32    seiLength = 0;
90     EB_U32    decodingUnitIndex;
91 
92     if(vuiPtr->frameFieldInfoPresentFlag) {
93         // pic_struct
94         seiLength += 4;
95 
96         // source_scan_type
97         seiLength += 2;
98 
99         // duplicate_flag
100         seiLength += 1;
101     }
102 
103     if(vuiPtr->hrdParametersPtr->cpbDpbDelaysPresentFlag) {
104         // au_cpb_removal_delay_minus1
105         seiLength += vuiPtr->hrdParametersPtr->auCpbRemovalDelayLengthMinus1 + 1;
106 
107         // pic_dpb_output_delay
108         seiLength += vuiPtr->hrdParametersPtr->dpbOutputDelayLengthMinus1 + 1;
109 
110         if(vuiPtr->hrdParametersPtr->subPicCpbParamsPresentFlag) {
111             // pic_dpb_output_du_delay
112             seiLength += vuiPtr->hrdParametersPtr->dpbOutputDelayDuLengthMinus1 + 1;
113         }
114 
115         if(vuiPtr->hrdParametersPtr->subPicCpbParamsPresentFlag && vuiPtr->hrdParametersPtr->subPicCpbParamsPicTimingSeiFlag) {
116             // num_decoding_units_minus1
117             seiLength += GetUvlcCodeLength(
118                              picTimingSeiPtr->numDecodingUnitsMinus1);
119 
120             // du_common_cpb_removal_delay_flag
121             seiLength += 1;
122 
123             if(picTimingSeiPtr->duCommonCpbRemovalDelayFlag) {
124                 // du_common_cpb_removal_delay_minus1
125                 seiLength += vuiPtr->hrdParametersPtr->duCpbRemovalDelayLengthMinus1 + 1;
126             }
127 
128             for(decodingUnitIndex = 0; decodingUnitIndex <= picTimingSeiPtr->numDecodingUnitsMinus1; ++decodingUnitIndex) {
129                 // num_nalus_in_du_minus1
130                 seiLength += GetUvlcCodeLength(
131                                  picTimingSeiPtr->numNalusInDuMinus1);
132 
133                 if(!picTimingSeiPtr->duCommonCpbRemovalDelayFlag && decodingUnitIndex < picTimingSeiPtr->numDecodingUnitsMinus1) {
134                     // du_cpb_removal_delay_minus1
135                     seiLength += vuiPtr->hrdParametersPtr->duCpbRemovalDelayLengthMinus1 + 1;
136                 }
137             }
138         }
139     }
140 
141     seiLength = (seiLength + 7) >> 3;
142 
143     return seiLength;
144 }
145 
GetBufPeriodSEILength(AppBufferingPeriodSei_t * bufferingPeriodPtr,AppVideoUsabilityInfo_t * vuiPtr)146 EB_U32 GetBufPeriodSEILength(
147     AppBufferingPeriodSei_t    *bufferingPeriodPtr,
148     AppVideoUsabilityInfo_t    *vuiPtr)
149 {
150     EB_U32       seiLength = 0;
151     EB_U32       nalVclIndex;
152     EB_U32       cpbIndex;
153 
154     // bp_seq_parameter_set_id
155     seiLength += GetUvlcCodeLength(
156                      bufferingPeriodPtr->bpSeqParameterSetId);
157 
158     if(!vuiPtr->hrdParametersPtr->subPicCpbParamsPresentFlag) {
159         // rap_cpb_params_present_flag
160         seiLength += 1;
161     }
162 
163     if(bufferingPeriodPtr->rapCpbParamsPresentFlag) {
164         // cpb_delay_offset
165         seiLength += vuiPtr->hrdParametersPtr->auCpbRemovalDelayLengthMinus1 + 1;
166 
167         // dpb_delay_offset
168         seiLength += vuiPtr->hrdParametersPtr->dpbOutputDelayDuLengthMinus1 + 1;
169     }
170 
171     // concatenation_flag
172     seiLength += 1;
173 
174     // au_cpb_removal_delay_delta_minus1
175     seiLength += vuiPtr->hrdParametersPtr->auCpbRemovalDelayLengthMinus1 + 1;
176 
177     for(nalVclIndex = 0; nalVclIndex < 2; ++nalVclIndex) {
178         if((nalVclIndex == 0 && vuiPtr->hrdParametersPtr->nalHrdParametersPresentFlag) ||
179                 (nalVclIndex == 1 && vuiPtr->hrdParametersPtr->vclHrdParametersPresentFlag)) {
180 
181             for(cpbIndex = 0; cpbIndex < vuiPtr->hrdParametersPtr->cpbCountMinus1[0] + 1; ++cpbIndex) {
182                 // initial_cpb_removal_delay
183                 seiLength += vuiPtr->hrdParametersPtr->initialCpbRemovalDelayLengthMinus1 + 1;
184 
185                 // initial_cpb_removal_delay_offset
186                 seiLength += vuiPtr->hrdParametersPtr->initialCpbRemovalDelayLengthMinus1 + 1;
187 
188                 if(vuiPtr->hrdParametersPtr->subPicCpbParamsPresentFlag || bufferingPeriodPtr->rapCpbParamsPresentFlag) {
189                     // initial_alt_cpb_removal_delay
190                     seiLength += vuiPtr->hrdParametersPtr->initialCpbRemovalDelayLengthMinus1 + 1;
191 
192                     // initial_alt_cpb_removal_delay_offset
193                     seiLength += vuiPtr->hrdParametersPtr->initialCpbRemovalDelayLengthMinus1 + 1;
194                 }
195             }
196         }
197     }
198 
199     seiLength = (seiLength + 7) >> 3;
200 
201     return seiLength;
202 }
203 
GetActiveParameterSetSEILength(AppActiveparameterSetSei_t * activeParameterSet)204 EB_U32 GetActiveParameterSetSEILength(
205     AppActiveparameterSetSei_t    *activeParameterSet)
206 {
207     EB_U32       seiLength = 0;
208 
209     // active_video_parameter_set_id
210     seiLength += 4;
211 
212     // self_contained_cvs_flag
213     seiLength += 1;
214 
215     // no_param_set_update_flag
216     seiLength += 1;
217 
218     //num_sps_ids_minus1
219     seiLength += GetUvlcCodeLength(activeParameterSet->numSpsIdsMinus1);
220 
221     //active_seq_param_set_id
222     seiLength += GetUvlcCodeLength(activeParameterSet->activeSeqParameterSetId);
223 
224     seiLength = (seiLength + 7) >> 3;
225 
226     return seiLength;
227 }
228 
GetRecoveryPointSEILength(AppRecoveryPoint_t * recoveryPointSeiPtr)229 EB_U32 GetRecoveryPointSEILength(
230     AppRecoveryPoint_t    *recoveryPointSeiPtr)
231 {
232     EB_U32    seiLength = 0;
233 
234     // recovery_poc_cnt
235     seiLength += GetSvlcCodeLength(
236                      recoveryPointSeiPtr->recoveryPocCnt);
237 
238     // exact_matching_flag
239     seiLength += 1;
240 
241     // broken_link_flag
242     seiLength += 1;
243 
244     seiLength = (seiLength + 7) >> 3;
245 
246     return seiLength;
247 }
248 
GetContentLightLevelSEILength()249 EB_U32 GetContentLightLevelSEILength()
250 {
251     EB_U32    seiLength = 0;
252 
253     // max_content_light_level
254     seiLength += 16;
255 
256     // max_pixel_average_light_level
257     seiLength += 16;
258 
259     seiLength = (seiLength + 7) >> 3;
260 
261     return seiLength;
262 }
263 
GetMasteringDisplayColorVolumeSEILength()264 EB_U32 GetMasteringDisplayColorVolumeSEILength()
265 {
266     EB_U32    seiLength = 0;
267 
268     // R, G, B Primaries
269     seiLength += 2 * 16 + 2 * 16 + 2 * 16;
270 
271     // White Point Co-Ordinates
272     seiLength += 2 * 16;
273 
274     // min & max luminance values
275     seiLength += 2 * 32;
276 
277     seiLength = (seiLength + 7) >> 3;
278 
279     return seiLength;
280 }
281