1 /*
2  *
3  * Copyright 2019 Samsung Electronics S.LSI Co. LTD
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 /*
19  * @file    VendorVideoAPI.cpp
20  * @author  ByungGwan Kang (bk0917.kang@samsung.com)
21  * @version 1.0
22  * @history
23  *   2019.08.08 : Create
24  */
25 
26 //#define LOG_NDEBUG 0
27 #define LOG_TAG "VendorVideoAPI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <log/log.h>
33 
34 #include <VendorVideoAPI.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
Exynos_parsing_user_data_registered_itu_t_t35(ExynosHdrDynamicInfo * dest,void * src)40 int Exynos_parsing_user_data_registered_itu_t_t35 (
41     ExynosHdrDynamicInfo *dest,
42     void                 *src)
43 {
44     int   bit_offset = 0;
45     int   data       = 0;
46     ExynosHdrDynamicInfo *pHdr10PlusInfo;
47 
48     int windows = 0;
49     int targeted_system_display_actual_peak_luminance_flag     = 0;
50     int num_rows_targeted_system_display_actual_peak_luminance = 0;
51     int num_cols_targeted_system_display_actual_peak_luminance = 0;
52     int mastering_display_actual_peak_luminance_flag           = 0;
53     int num_rows_mastering_display_actual_peak_luminance       = 0;
54     int color_saturation_mapping_flag                          = 0;
55     int num_cols_mastering_display_actual_peak_luminance       = 0;
56 
57     int extraBit, extraByte = 0;
58 
59     int i, j, k, l;
60 
61     if ((dest == NULL) || (src == NULL)) {
62         ALOGE("[%s] invalid parameters", __FUNCTION__);
63         return -1;
64     }
65 
66     pHdr10PlusInfo = dest;
67 
68     /* country_code : 8bit */
69     for (i = 0; i < 1; i++) {
70         for (j = 0; j < 8; j++) {
71             data |= (*((char *)src + (bit_offset / 8) + i) & (1 << j));
72         }
73     }
74     bit_offset += 8;
75     pHdr10PlusInfo->data.country_code = data;
76     data = 0;
77 
78     /* terminal_provider_code : 16bit */
79     for (i = 0; i < 2; i++) {
80         for (j = 0; j < 8; j++) {
81             data |= (*((char *)src + (bit_offset / 8) + i) & (1 << j));
82         }
83     }
84     bit_offset += 16;
85     pHdr10PlusInfo->data.provider_code = data;
86     data = 0;
87 
88     /* terminal_provider_oriented_code : 16bit */
89     for (i = 0; i < 2; i++) {
90         for (j = 0; j < 8; j++) {
91             data |= (*((char *)src + (bit_offset / 8) + i) & (1 << j));
92         }
93     }
94     bit_offset += 16;
95     pHdr10PlusInfo->data.provider_oriented_code = data;
96     data = 0;
97 
98     /* application_identifier : 8bit*/
99     for (i = 0; i < 1; i++) {
100         for (j = 0; j < 8; j++) {
101             data |= (*((char *)src + (bit_offset / 8) + i) & (1 << j));
102         }
103     }
104     bit_offset += 8;
105     pHdr10PlusInfo->data.application_identifier = data;
106     data = 0;
107 
108     /* application_version : 8bit*/
109     for (i = 0; i < 1; i++) {
110         for (j = 0; j < 8; j++) {
111             data |= (*((char *)src + (bit_offset / 8) + i) & (1 << j));
112         }
113     }
114     bit_offset += 8;
115     pHdr10PlusInfo->data.application_version = data;
116     data = 0;
117 
118     /* num_windows : 2bit*/
119     for (i = 0; i < 1; i++) {
120         for (j = 0; j < 2; j++) {
121             data |= (*((char *)src + (bit_offset / 8) + i) & (1 << (7 - j)));
122         }
123     }
124     bit_offset += 2;
125     extraBit   = (bit_offset % 8);
126     data       = data >> (8 - extraBit);
127     //pHdr10PlusInfo->data.num_windows = data;
128     windows = data;
129     data = 0;
130 
131 
132     for (i = 1; i < windows; i++) {
133         /* window_upper_left_corner_x : 16bit */
134         if (extraBit > 0)
135             extraByte = 1;
136 
137         for (j = 0; j < 2 + extraByte; j++) {
138             for (k = extraBit; k < 8; k++) {
139                 data |= (*((char *)src + (bit_offset / 8) + j) & (1 << (7 - k)));
140             }
141 
142             if (j < 2)
143                 data = data << 8;
144 
145             extraBit += (k - extraBit);
146             extraBit %= 8;
147         }
148         bit_offset += 16;
149         extraByte   = 0;
150         extraBit    = bit_offset % 8;
151         data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
152         //pHdr10PlusInfo->data.window_upper_left_corner_x[i] = data;
153         data = 0;
154 
155 
156         /* window_upper_left_corner_y : 16bit */
157         if (extraBit > 0)
158             extraByte = 1;
159 
160         for (j = 0; j < 2 + extraByte; j++) {
161             for (k = extraBit; k < 8; k++) {
162                 data |= (*((char *)src + (bit_offset / 8) + j) & (1 << (7 - k)));
163             }
164 
165             if (j < 2)
166                 data = data << 8;
167 
168             extraBit += (k - extraBit);
169             extraBit %= 8;
170         }
171         bit_offset += 16;
172         extraByte   = 0;
173         extraBit    = bit_offset % 8;
174         data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
175         //pHdr10PlusInfo->data.window_upper_left_corner_y[i] = data;
176         data = 0;
177 
178 
179         /* window_upper_right_corner_x : 16bit */
180         if (extraBit > 0)
181             extraByte = 1;
182 
183         for (j = 0; j < 2 + extraByte; j++) {
184             for (k = extraBit; k < 8; k++) {
185                 data |= (*((char *)src + (bit_offset / 8) + j) & (1 << (7 - k)));
186             }
187 
188             if (j < 2)
189                 data = data << 8;
190 
191             extraBit += (k - extraBit);
192             extraBit %= 8;
193         }
194         bit_offset += 16;
195         extraByte   = 0;
196         extraBit    = bit_offset % 8;
197         data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
198         //pHdr10PlusInfo->data.window_upper_right_corner_x[i] = data;
199         data = 0;
200 
201 
202         /* window_upper_right_corner_y : 16bit */
203         if (extraBit > 0)
204             extraByte = 1;
205 
206         for (j = 0; j < 2 + extraByte; j++) {
207             for (k = extraBit; k < 8; k++) {
208                 data |= (*((char *)src + (bit_offset / 8) + j) & (1 << (7 - k)));
209             }
210 
211             if (j < 2)
212                 data = data << 8;
213 
214             extraBit += (k - extraBit);
215             extraBit %= 8;
216         }
217         bit_offset += 16;
218         extraByte   = 0;
219         extraBit    = bit_offset % 8;
220         data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
221         //pHdr10PlusInfo->data.window_upper_right_corner_y[i] = data;
222         data = 0;
223 
224 
225         /* center_of_ellipse_x : 16bit */
226         if (extraBit > 0)
227             extraByte = 1;
228 
229         for (j = 0; j < 2 + extraByte; j++) {
230             for (k = extraBit; k < 8; k++) {
231                 data |= (*((char *)src + (bit_offset / 8) + j) & (1 << (7 - k)));
232             }
233 
234             if (j < 2)
235                 data = data << 8;
236 
237             extraBit += (k - extraBit);
238             extraBit %= 8;
239         }
240         bit_offset += 16;
241         extraByte   = 0;
242         extraBit    = bit_offset % 8;
243         data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
244         //pHdr10PlusInfo->data.window_upper_right_corner_y[i] = data;
245         data = 0;
246 
247 
248         /* center_of_ellipse_y : 16bit */
249         if (extraBit > 0)
250             extraByte = 1;
251 
252         for (j = 0; j < 2 + extraByte; j++) {
253             for (k = extraBit; k < 8; k++) {
254                 data |= (*((char *)src + (bit_offset / 8) + j) & (1 << (7 - k)));
255             }
256 
257             if (j < 2)
258                 data = data << 8;
259 
260             extraBit += (k - extraBit);
261             extraBit %= 8;
262         }
263         bit_offset += 16;
264         extraByte   = 0;
265         extraBit    = bit_offset % 8;
266         data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
267         //pHdr10PlusInfo->data.window_upper_right_corner_y[i] = data;
268         data = 0;
269 
270 
271         /* rotation_angle : 8bit */
272         if (extraBit > 0)
273             extraByte = 1;
274 
275         for (j = 0; j < 1 + extraByte; j++) {
276             for (k = extraBit; k < 8; k++) {
277                 data |= (*((char *)src + (bit_offset / 8) + j) & (1 << (7 - k)));
278             }
279 
280             if (j < 1)
281                 data = data << 8;
282 
283             extraBit += (k - extraBit);
284             extraBit %= 8;
285         }
286         bit_offset += 8;
287         extraByte   = 0;
288         extraBit    = bit_offset % 8;
289         data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
290         //pHdr10PlusInfo->data.rotation_angle[i] = data;
291         data = 0;
292 
293 
294         /* semimajor_axis_internal_ellipse : 16bit */
295         if (extraBit > 0)
296             extraByte = 1;
297 
298         for (j = 0; j < 2 + extraByte; j++) {
299             for (k = extraBit; k < 8; k++) {
300                 data |= (*((char *)src + (bit_offset / 8) + j) & (1 << (7 - k)));
301             }
302 
303             if (j < 2)
304                 data = data << 8;
305 
306             extraBit += (k - extraBit);
307             extraBit %= 8;
308         }
309         bit_offset += 16;
310         extraByte   = 0;
311         extraBit    = bit_offset % 8;
312         data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
313         //pHdr10PlusInfo->data.semimajor_axis_internal_ellipse[i] = data;
314         data = 0;
315 
316 
317         /* semimajor_axis_external_ellipse : 16bit */
318         if (extraBit > 0)
319             extraByte = 1;
320 
321         for (j = 0; j < 2 + extraByte; j++) {
322             for (k = extraBit; k < 8; k++) {
323                 data |= (*((char *)src + (bit_offset / 8) + j) & (1 << (7 - k)));
324             }
325 
326             if (j < 2)
327                 data = data << 8;
328 
329             extraBit += (k - extraBit);
330             extraBit %= 8;
331         }
332         bit_offset += 16;
333         extraByte   = 0;
334         extraBit    = bit_offset % 8;
335         data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
336         //pHdr10PlusInfo->data.semimajor_axis_external_ellipse[i] = data;
337         data = 0;
338 
339 
340         /* semiminor_axis_external_ellipse : 16bit */
341         if (extraBit > 0)
342             extraByte = 1;
343 
344         for (j = 0; j < 2 + extraByte; j++) {
345             for (k = extraBit; k < 8; k++) {
346                 data |= (*((char *)src + (bit_offset / 8) + j) & (1 << (7 - k)));
347             }
348 
349             if (j < 2)
350                 data = data << 8;
351 
352             extraBit += (k - extraBit);
353             extraBit %= 8;
354         }
355         bit_offset += 16;
356         extraByte   = 0;
357         extraBit    = bit_offset % 8;
358         data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
359         //pHdr10PlusInfo->data.semiminor_axis_external_ellipse[i] = data;
360         data = 0;
361 
362 
363         /* overlap_process_option : 1bit */
364         data |= (*((char *)src + (bit_offset / 8)) & (1 << (7 - extraBit)));
365 
366         bit_offset  += 1;
367         extraByte    = 0;
368         extraBit     = bit_offset % 8;
369         data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
370         //pHdr10PlusInfo->data.overlap_process_option[i] = data;
371         data = 0;
372     }
373 
374 
375     /* targeted_system_display_maximum_luminance : 27bit */
376     if (extraBit > 5)
377         extraByte = 2;
378     else if (extraBit <= 5)
379         extraByte = 1;
380 
381     for (i = 0; i < 3 + extraByte; i++) {
382         for (j = extraBit; j < 8; j++) {
383             data |= (*((char *)src + (bit_offset / 8) + i) & (1 << (7 - j)));
384         }
385 
386         if (i < 3 + extraByte - 1)
387             data = data << 8;
388 
389         extraBit += (k - extraBit);
390         extraBit %= 8;
391     }
392     bit_offset += 27;
393     extraByte   = 0;
394     extraBit    = bit_offset % 8;
395     data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
396     pHdr10PlusInfo->data.display_maximum_luminance = data;
397     data = 0;
398 
399 
400     /* targeted_system_display_actual_peak_luminance_flag : 1bit */
401     data |= (*((char *)src + (bit_offset / 8)) & (1 << (7 - extraBit)));
402 
403     bit_offset  += 1;
404     extraBit     = bit_offset % 8;
405     data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
406     //pHdr10PlusInfo->data.targeted_system_display_actual_peak_luminance_flag = data;
407     targeted_system_display_actual_peak_luminance_flag = data;
408     data = 0;
409 
410 
411     if (targeted_system_display_actual_peak_luminance_flag) {
412         /* num_rows_targeted_system_display_actual_peak_luminance : 5bit */
413         if (extraBit > 3)
414             extraByte = 1;
415 
416         for (i = 0; i < 1 + extraByte; i++) {
417             for (j = extraBit; j < 8; j++) {
418                 data |= (*((char *)src + (bit_offset / 8) + i) & (1 << (7 - j)));
419             }
420 
421             if (i < 1)
422                 data = data << 8;
423 
424             extraBit += (j - extraBit);
425             extraBit %= 8;
426         }
427         bit_offset += 5;
428         extraByte   = 0;
429         extraBit    = bit_offset % 8;
430         data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
431         //pHdr10PlusInfo->data.num_rows_targeted_system_display_actual_peak_luminance = data;
432         num_rows_targeted_system_display_actual_peak_luminance = data;
433         data = 0;
434 
435 
436         /* num_cols_targeted_system_display_actual_peak_luminance : 5bit */
437         if (extraBit > 3)
438             extraByte = 1;
439 
440         for (i = 0; i < 1 + extraByte; i++) {
441             for (j = extraBit; j < 8; j++) {
442                 data |= (*((char *)src + (bit_offset / 8) + i) & (1 << (7 - j)));
443             }
444 
445             if (i < 1)
446                 data = data << 8;
447 
448             extraBit += (j - extraBit);
449             extraBit %= 8;
450         }
451         bit_offset += 5;
452         extraByte   = 0;
453         extraBit    = bit_offset % 8;
454         data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
455         //pHdr10PlusInfo->data.num_cols_targeted_system_display_actual_peak_luminance = data;
456         num_cols_targeted_system_display_actual_peak_luminance = data;
457         data = 0;
458 
459 
460         for (i = 0; i < num_rows_targeted_system_display_actual_peak_luminance; i++) {
461             for (j = 0; j < num_cols_targeted_system_display_actual_peak_luminance; j++) {
462                 /* mastering_display_actual_peak_luminance : 4bit */
463                 if (extraBit > 4)
464                     extraByte = 1;
465 
466                 for (k = 0; k < 1 + extraByte; k++) {
467                     for (l = extraBit; l < 8; l++) {
468                         data |= (*((char *)src + (bit_offset / 8) + k) & (1 << (7 - l)));
469                     }
470 
471                     if (k < 1)
472                         data = data << 8;
473 
474                     extraBit += (l - extraBit);
475                     extraBit %= 8;
476                 }
477                 bit_offset += 4;
478                 extraByte   = 0;
479                 extraBit    = bit_offset % 8;
480                 data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
481                 //pHdr10PlusInfo->data.mastering_display_actual_peak_luminance = data;
482                 data = 0;
483             }
484         }
485     }
486 
487     for (i = 0; i < windows; i++) {
488         for (j = 0; j < 3; j++) {
489             /* maxscl : 17bit */
490             for (k = 0; k < 3; k++) {
491                 for (l = extraBit; l < 8; l++) {
492                     data |= (*((char *)src + (bit_offset / 8) + k) & (1 << (7 - l)));
493                 }
494 
495                 if (k < 2)
496                     data = data << 8;
497 
498                 extraBit += (l - extraBit);
499                 extraBit %= 8;
500             }
501             bit_offset += 17;
502             extraBit    = bit_offset % 8;
503 
504             if (extraBit != 0)
505                 data = data >> (8 - extraBit);
506 
507             pHdr10PlusInfo->data.maxscl[j] = data;
508             data = 0;
509         }
510 
511         /* average_maxrgb : 17bit */
512         for (j = 0; j < 3; j++) {
513             for (k = extraBit; k < 8; k++) {
514                 data |= (*((char *)src + (bit_offset / 8) + j) & (1 << (7 - k)));
515             }
516 
517             if (j < 2)
518                 data = data << 8;
519 
520             extraBit += (k - extraBit);
521             extraBit %= 8;
522         }
523         bit_offset += 17;
524         extraBit    = bit_offset % 8;
525         data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
526         //pHdr10PlusInfo->data.average_maxrgb = data;
527         data = 0;
528 
529 
530         /* num_distribution_maxrgb_percentiles : 4bit */
531         if (extraBit > 4)
532             extraByte = 1;
533 
534         for (j = 0; j < 1 + extraByte; j++) {
535             for (k = extraBit; k < 8; k++) {
536                 data |= (*((char *)src + (bit_offset / 8) + j) & (1 << (7 - k)));
537             }
538 
539             if (extraByte > 0) {
540                 if (j < 1)
541                     data = data << 8;
542             }
543 
544             extraBit += (k - extraBit);
545             extraBit %= 8;
546         }
547         bit_offset += 4;
548         extraByte   = 0;
549         extraBit    = bit_offset % 8;
550         data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
551         pHdr10PlusInfo->data.num_maxrgb_percentiles = data;
552         data = 0;
553 
554 
555         for (j = 0; j < pHdr10PlusInfo->data.num_maxrgb_percentiles; j++) {
556             /* distribution_maxrgb_percentages : 7bit */
557             if (extraBit > 1)
558                 extraByte = 1;
559 
560             for (k = 0; k < 1 + extraByte; k++) {
561                 for (l = extraBit; l < 8; l++) {
562                     data |= (*((char *)src + (bit_offset / 8) + k) & (1 << (7 - l)));
563                 }
564 
565                 if (k < 1)
566                     data = data << 8;
567 
568                 extraBit += (l - extraBit);
569                 extraBit %= 8;
570             }
571             bit_offset += 7;
572             extraByte   = 0;
573             extraBit    = bit_offset % 8;
574             data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
575             pHdr10PlusInfo->data.maxrgb_percentages[j] = data;
576             data = 0;
577 
578 
579             /* distribution_maxrgb_percentiles : 17bit */
580             if (extraBit >= 0)
581                 extraByte = 1;
582 
583             for (k = 0; k < 2 + extraByte; k++) {
584                 for (l = extraBit; l < 8; l++) {
585                     data |= (*((char *)src + (bit_offset / 8) + k) & (1 << (7 - l)));
586                 }
587 
588                 if (k < 2)
589                     data = data << 8;
590 
591                 extraBit += (l - extraBit);
592                 extraBit %= 8;
593             }
594             bit_offset += 17;
595             extraByte   = 0;
596             extraBit    = bit_offset % 8;
597             data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
598             pHdr10PlusInfo->data.maxrgb_percentiles[j] = data;
599             data = 0;
600         }
601 
602 
603         /* fraction_bright_pixels : 10bit*/
604         if (extraBit > 6)
605             extraByte = 2;
606         else if (extraBit <= 6)
607             extraByte = 1;
608 
609         for (j = 0; j < 1 + extraByte; j++) {
610             for (k = extraBit; k < 8; k++) {
611                 data |= (*((char *)src + (bit_offset / 8) + j) & (1 << (7 - k)));
612             }
613 
614             if (j < 2)
615                 data = data << 8;
616 
617             extraBit += (k - extraBit);
618             extraBit %= 8;
619         }
620         bit_offset += 10;
621         extraByte   = 0;
622         extraBit    = bit_offset % 8;
623         data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
624         //pHdr10PlusInfo->data.fraction_bright_pixels = data;
625         data = 0;
626     }
627 
628 
629     /* mastering_display_actual_peak_luminance_flag : 1bit */
630     data |= (*((char *)src + (bit_offset / 8)) & (1 << (7 - extraBit)));
631 
632     bit_offset  += 1;
633     extraBit     = bit_offset % 8;
634     data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
635     //pHdr10PlusInfo->data.mastering_display_actual_peak_luminance_flag = data;
636     mastering_display_actual_peak_luminance_flag = data;
637     data = 0;
638 
639 
640     if (mastering_display_actual_peak_luminance_flag) {
641         /* num_rows_mastering_display_actual_peak_luminance : 5bit */
642         if (extraBit > 3)
643             extraByte = 1;
644 
645         for (i = 0; i < 1 + extraByte; i++) {
646             for (j = extraBit; j < 8; j++) {
647                 data |= (*((char *)src + (bit_offset / 8) + i) & (1 << (7 - j)));
648             }
649 
650             if (i < 1)
651                 data = data << 8;
652 
653             extraBit += (j - extraBit);
654             extraBit %= 8;
655         }
656         bit_offset += 5;
657         extraByte   = 0;
658         extraBit    = bit_offset % 8;
659         data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
660         //pHdr10PlusInfo->data.num_rows_mastering_display_actual_peak_luminance = data;
661         num_rows_mastering_display_actual_peak_luminance = data;
662         data = 0;
663 
664 
665         /* num_cols_mastering_display_actual_peak_luminance : 5bit */
666         if (extraBit > 3)
667             extraByte = 1;
668 
669         for (i = 0; i < 1 + extraByte; i++) {
670             for (j = extraBit; j < 8; j++) {
671                 data |= (*((char *)src + (bit_offset / 8) + i) & (1 << (7 - j)));
672             }
673 
674             if (i < 1)
675                 data = data << 8;
676 
677             extraBit += (j - extraBit);
678             extraBit %= 8;
679         }
680         bit_offset += 5;
681         extraByte   = 0;
682         extraBit    = bit_offset % 8;
683         data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
684         //pHdr10PlusInfo->data.num_cols_mastering_display_actual_peak_luminance = data;
685         num_cols_mastering_display_actual_peak_luminance = data;
686         data = 0;
687 
688 
689         for (i = 0; i < num_rows_mastering_display_actual_peak_luminance; i++) {
690             for (j = 0; j < num_cols_mastering_display_actual_peak_luminance; j++) {
691                 /* mastering_display_actual_peak_luminance : 4bit */
692                 if (extraBit > 4)
693                     extraByte = 1;
694 
695                 for (k = 0; k < 1 + extraByte; k++) {
696                     for (l = extraBit; l < 8; l++) {
697                         data |= (*((char *)src + (bit_offset / 8) + k) & (1 << (7 - l)));
698                     }
699 
700                     if (k < 1)
701                         data = data << 8;
702 
703                     extraBit += (l - extraBit);
704                     extraBit %= 8;
705                 }
706                 bit_offset += 4;
707                 extraByte   = 0;
708                 extraBit    = bit_offset % 8;
709                 data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
710                 //pHdr10PlusInfo->data.mastering_display_actual_peak_luminance = data;
711                 data = 0;
712             }
713         }
714     }
715 
716     for (i = 0; i < windows; i++) {
717         /* tone_mapping_flag : 1bit */
718         data |= (*((char *)src + (bit_offset / 8)) & (1 << (7 - extraBit)));
719 
720         bit_offset  += 1;
721         extraBit     = bit_offset % 8;
722         data         = data >> (8 - extraBit);
723         pHdr10PlusInfo->data.tone_mapping.tone_mapping_flag = data;
724         data = 0;
725 
726 
727         if (pHdr10PlusInfo->data.tone_mapping.tone_mapping_flag) {
728             /* knee_point_x : 12bit */
729             if (extraBit > 5)
730                 extraByte = 2;
731             else if (extraBit <= 5)
732                 extraByte = 1;
733 
734             for (j = 0; j < 1 + extraByte; j++) {
735                 for (k = extraBit; k < 8; k++) {
736                     data |= (*((char *)src + (bit_offset / 8) + j) & (1 << (7 - k)));
737                 }
738 
739                 if (j < 1 + extraByte - 1)
740                     data = data << 8;
741 
742                 extraBit += (k - extraBit);
743                 extraBit %= 8;
744             }
745             bit_offset += 12;
746             extraByte   = 0;
747             extraBit    = bit_offset % 8;
748             data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
749             pHdr10PlusInfo->data.tone_mapping.knee_point_x = data;
750             data = 0;
751 
752 
753             /* knee_point_y : 12bit */
754             if (extraBit > 5)
755                 extraByte = 2;
756             else if (extraBit <= 5)
757                 extraByte = 1;
758 
759             for (j = 0; j < 1 + extraByte; j++) {
760                 for (k = extraBit; k < 8; k++) {
761                     data |= (*((char *)src + (bit_offset / 8) + j) & (1 << (7 - k)));
762                 }
763 
764                 if (j < 1 + extraByte - 1)
765                     data = data << 8;
766 
767                 extraBit += (k - extraBit);
768                 extraBit %= 8;
769             }
770             bit_offset += 12;
771             extraByte   = 0;
772             extraBit    = bit_offset % 8;
773             data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
774             pHdr10PlusInfo->data.tone_mapping.knee_point_y = data;
775             data = 0;
776 
777 
778             /* num_bezier_curve_anchors : 4bit */
779             if (extraBit > 4)
780                 extraByte = 1;
781 
782             for (j = 0; j < 1 + extraByte; j++) {
783                 for (k = extraBit; k < 8; k++) {
784                     data |= (*((char *)src + (bit_offset / 8) + j) & (1 << (7 - k)));
785                 }
786 
787                 if (j < 1 + extraByte - 1)
788                     data = data << 8;
789 
790                 extraBit += (k - extraBit);
791                 extraBit %= 8;
792             }
793             bit_offset += 4;
794             extraByte   = 0;
795             extraBit    = bit_offset % 8;
796             data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
797             pHdr10PlusInfo->data.tone_mapping.num_bezier_curve_anchors = data;
798             data = 0;
799 
800 
801             for (j = 0; j < pHdr10PlusInfo->data.tone_mapping.num_bezier_curve_anchors; j++) {
802                 /* bezier_curve_anchors : 10bit */
803                 if (extraBit > 6)
804                     extraByte = 2;
805                 else if (extraBit <= 6)
806                     extraByte = 1;
807 
808                 for (k = 0; k < 1 + extraByte; k++) {
809                     for (l = extraBit; l < 8; l++) {
810                         data |= (*((char *)src + (bit_offset / 8) + k) & (1 << (7 - l)));
811                     }
812 
813                     if (k < 1 + extraByte - 1)
814                         data = data << 8;
815 
816                     extraBit += (l - extraBit);
817                     extraBit %= 8;
818                 }
819                 bit_offset += 10;
820                 extraByte   = 0;
821                 extraBit    = bit_offset % 8;
822                 data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
823                 pHdr10PlusInfo->data.tone_mapping.bezier_curve_anchors[j] = data;
824                 data = 0;
825             }
826         }
827 
828 
829         /* color_saturation_mapping_flag : 1bit */
830         data |= (*((char *)src + (bit_offset / 8)) & (1 << (7 - extraBit)));
831 
832         bit_offset  += 1;
833         extraByte    = 0;
834         extraBit     = bit_offset % 8;
835         data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
836         //pHdr10PlusInfo->data.color_saturation_mapping_flag = data;
837         color_saturation_mapping_flag = data;
838         data = 0;
839 
840 
841         if (color_saturation_mapping_flag) {
842             /* color_saturation_weight : 6bit */
843             if (extraBit > 3)
844                 extraByte = 1;
845 
846             for (j = 0; j < 1 + extraByte; j++) {
847                 for (k = extraBit; k < 8; k++) {
848                     data |= (*((char *)src + (bit_offset / 8) + j) & (1 << (7 - k)));
849                 }
850 
851                 if (j < 1 + extraByte - 1)
852                     data = data << 8;
853 
854                 extraBit += (k - extraBit);
855                 extraBit %= 8;
856             }
857             bit_offset += 6;
858             extraByte   = 0;
859             extraBit    = bit_offset % 8;
860             data        = (extraBit != 0) ? (data >> (8 - extraBit)) : data;
861             //pHdr10PlusInfo->color_saturation_weight = data;
862             data = 0;
863         }
864     }
865 
866     return 0;
867 }
868 
Exynos_dynamic_meta_to_itu_t_t35(ExynosHdrDynamicInfo * src,char * dst)869 int Exynos_dynamic_meta_to_itu_t_t35 (
870     ExynosHdrDynamicInfo *src,
871     char                 *dst)
872 {
873     ExynosHdrDynamicInfo *pHDRDynamicInfo = NULL;
874     char                 *pBlob           = NULL;
875 
876     int size         = 0;
877     int bit_offset   = 0;
878     int extraBit     = 0;
879     int extraByte    = 0;
880     int offset_limit = 0;
881 
882     int buffer     = 0;
883     int tempBuffer = 0;
884 
885     int i, j, k;
886 
887     if ((src == NULL) || (dst == NULL)) {
888         ALOGE("[%s] invalid parameters", __FUNCTION__);
889         return -1;
890     }
891 
892     pHDRDynamicInfo = src;
893     pBlob           = dst;
894 
895     /* country_code: 8bit */
896     offset_limit = bit_offset + 8;
897 
898     for (i = 0; i < 8; i++) {
899         *((char *)pBlob + (bit_offset / 8)) |=
900             (*((char *)&pHDRDynamicInfo->data.country_code) & (1 << (7 - i)));
901         bit_offset++;
902 
903         if (bit_offset == offset_limit)
904             break;
905     }
906 
907     /* terminal_provider_code: 16bit */
908     offset_limit = bit_offset + 16;
909 
910     for (i = 0; i < 2; i++) {
911         for (j = 0; j < 8; j++) {
912             *((char *)pBlob + (bit_offset / 8)) |=
913                 (*((char *)&pHDRDynamicInfo->data.provider_code + 1 - i) & (1 << (7 - j)));
914             bit_offset++;
915 
916             if (bit_offset == offset_limit)
917                 break;
918         }
919     }
920 
921     /* terminal_provider_oriented_code: 16bit */
922     offset_limit = bit_offset + 16;
923 
924     for (i = 0; i < 2; i++) {
925         for (j = 0; j < 8; j++) {
926             *((char *)pBlob + (bit_offset / 8)) |=
927                 (*((char *)&pHDRDynamicInfo->data.provider_oriented_code + 1 - i) & (1 << (7 - j)));
928             bit_offset++;
929 
930             if (bit_offset == offset_limit)
931                 break;
932         }
933     }
934 
935     /* application_identifier: 8bit */
936     offset_limit = bit_offset + 8;
937 
938     for (i = 0; i < 8; i++) {
939         *((char *)pBlob + (bit_offset / 8)) |=
940             (*((char *)&pHDRDynamicInfo->data.application_identifier) & (1 << (7 - i)));
941         bit_offset++;
942 
943         if (bit_offset == offset_limit)
944             break;
945     }
946 
947     /* application_version: 8bit */
948     offset_limit = bit_offset + 8;
949 
950     for (i = 0; i < 8; i++) {
951         *((char *)pBlob + (bit_offset / 8)) |=
952             (*((char *)&pHDRDynamicInfo->data.application_version) & (1 << (7 - i)));
953         bit_offset++;
954 
955         if (bit_offset == offset_limit)
956             break;
957     }
958 
959     /* num_windows: 2bit */
960     offset_limit = bit_offset + 2;
961 
962     for (i = 0; i < 2; i++) {
963         /* num_windows is always 1 now */
964         *((char *)pBlob + (bit_offset / 8)) |= ((char)0x40/* num_windows */ & (1 << (7 - i)));
965         bit_offset++;
966 
967         if (bit_offset == offset_limit)
968             break;
969     }
970     extraBit = (bit_offset % 8);
971 
972     for (i = 1; i < 1 /* num_windows */; i++) {
973         /* window_upper_left_corner_x: 16bit */
974         bit_offset += 16;
975         extraBit = (bit_offset % 8);
976 
977         /* window_upper_left_corner_y: 16bit */
978         bit_offset += 16;
979         extraBit = (bit_offset % 8);
980 
981         /* window_upper_right_corner_x: 16bit */
982         bit_offset += 16;
983         extraBit = (bit_offset % 8);
984 
985         /* window_upper_right_corner_y: 16bit */
986         bit_offset += 16;
987         extraBit = (bit_offset % 8);
988 
989         /* center_of_ellipse_x: 16bit */
990         bit_offset += 16;
991         extraBit = (bit_offset % 8);
992 
993         /* center_of_ellipse_y: 16bit */
994         bit_offset += 16;
995         extraBit = (bit_offset % 8);
996 
997         /* rotation_angle: 8bit */
998         bit_offset += 8;
999         extraBit = (bit_offset % 8);
1000 
1001         /* semimajor_axis_internal_ellipse: 16bit */
1002         bit_offset += 16;
1003         extraBit = (bit_offset % 8);
1004 
1005         /* semimajor_axis_external_ellipse: 16bit */
1006         bit_offset += 16;
1007         extraBit = (bit_offset % 8);
1008 
1009         /* semiminor_axis_external_ellipse: 16bit */
1010         bit_offset += 16;
1011         extraBit = (bit_offset % 8);
1012 
1013         /* overlap_process_option: 1bit */
1014         bit_offset += 1;
1015         extraBit = (bit_offset % 8);
1016     }
1017 
1018     /* targeted_system_display_maximum_luminance: 27bit */
1019     offset_limit = bit_offset + 27;
1020 
1021     tempBuffer = pHDRDynamicInfo->data.display_maximum_luminance << (5 - extraBit);
1022     for (i = 0; i < 4; i++) {
1023         memcpy((char *)&buffer + i, (char *)&tempBuffer + (3 - i), sizeof(char));
1024     }
1025 
1026     for (i = 0; i < 4; i++) {
1027         for (j = extraBit; j < 8; j++) {
1028             *((char *)pBlob + (bit_offset / 8)) |= (*((char *)&buffer + i) & (1 << (7 - j)));
1029             bit_offset++;
1030 
1031             if (bit_offset == offset_limit)
1032                 break;
1033         }
1034         extraBit = (bit_offset % 8);
1035     }
1036     buffer     = 0;
1037     tempBuffer = 0;
1038 
1039     /* targeted_system_display_actual_peak_luminance_flag: 1bit (always 0) */
1040     bit_offset += 1;
1041     extraBit = (bit_offset % 8);
1042 
1043     /* NOTE: These info would not set because targeted_system_display_actual_peak_luminance_flag is always 0
1044      * - num_rows_targeted_system_display_actual_peak_luminance: 5bit
1045      * - num_cols_targeted_system_display_actual_peak_luminance: 5bit
1046      * - targeted_system_display_actual_peak_luminance: 4bit */
1047 
1048     /* maxscl: 17bit */
1049     for (i = 0; i < 3; i++) {
1050         offset_limit = bit_offset + 17;
1051 
1052         tempBuffer = pHDRDynamicInfo->data.maxscl[i] << (15 - extraBit);
1053         for (j = 0; j < 4; j++) {
1054             memcpy((char *)&buffer + j, (char *)&tempBuffer + (3 - j), sizeof(char));
1055         }
1056 
1057         for (j = 0; j < 3; j++) {
1058             for (k = extraBit; k < 8; k++) {
1059                 *((char *)pBlob + (bit_offset / 8)) |= (*((char *)&buffer + j) & (1 << (7 - k)));
1060                 bit_offset++;
1061 
1062                 if (bit_offset == offset_limit)
1063                     break;
1064             }
1065             extraBit = (bit_offset % 8);
1066         }
1067         buffer     = 0;
1068         tempBuffer = 0;
1069     }
1070 
1071     /* average_maxrgb: 17bit */
1072     bit_offset += 17;
1073     extraBit = (bit_offset % 8);
1074 
1075     /* num_distribution_maxrgb_percentiles: 4bit */
1076     offset_limit = bit_offset + 4;
1077 
1078     tempBuffer = pHDRDynamicInfo->data.num_maxrgb_percentiles << (4 - extraBit);
1079 
1080     for (i = extraBit; i < 8; i++) {
1081         *((char *)pBlob + (bit_offset / 8)) |= (*((char *)&tempBuffer) & (1 << (7 - i)));
1082         bit_offset++;
1083 
1084         if (bit_offset == offset_limit)
1085             break;
1086     }
1087     extraBit = (bit_offset % 8);
1088     tempBuffer = 0;
1089 
1090     for (i = 0; i < pHDRDynamicInfo->data.num_maxrgb_percentiles; i++) {
1091         /* distribution_maxrgb_percentaged: 7bit */
1092         offset_limit = bit_offset + 7;
1093 
1094         if (extraBit > 1)
1095             extraByte = 1;
1096 
1097         tempBuffer = pHDRDynamicInfo->data.maxrgb_percentages[i];
1098         tempBuffer = tempBuffer << (25 - extraBit);
1099 
1100         for (j = 0; j < 4; j++) {
1101             memcpy((char *)&buffer + j, (char *)&tempBuffer + (3 - j), sizeof(char));
1102         }
1103 
1104         for (j = 0; j < (1 + extraByte); j++) {
1105             for (k = extraBit; k < 8; k++) {
1106                 *((char *)pBlob + (bit_offset / 8)) |= (*((char *)&buffer + j) & (1 << (7 - k)));
1107                 bit_offset++;
1108 
1109                 if (bit_offset == offset_limit) {
1110                     break;
1111                 }
1112             }
1113             extraBit = (bit_offset % 8);
1114         }
1115         buffer     = 0;
1116         tempBuffer = 0;
1117         extraByte  = 0;
1118 
1119         /* distribution_maxrgb_percentiles: 17bit */
1120         offset_limit = bit_offset + 17;
1121 
1122         tempBuffer = pHDRDynamicInfo->data.maxrgb_percentiles[i] << (15 - extraBit);
1123 
1124         for (j = 0; j < 4; j++) {
1125             memcpy((char *)&buffer + j, (char *)&tempBuffer + (3 - j), sizeof(char));
1126         }
1127 
1128         for (j = 0; j < 3; j++) {
1129             for (k = extraBit; k < 8; k++) {
1130                 *((char *)pBlob + (bit_offset / 8)) |= (*((char *)&buffer + j) & (1 << (7 - k)));
1131                 bit_offset++;
1132 
1133                 if (bit_offset == offset_limit) {
1134                     break;
1135                 }
1136             }
1137             extraBit = (bit_offset % 8);
1138         }
1139         buffer     = 0;
1140         tempBuffer = 0;
1141     }
1142 
1143     /* fraction_bright_pixels: 10bit */
1144     bit_offset += 10;
1145     extraBit = (bit_offset % 8);
1146 
1147     /* mastering_display_actual_peak_luminance_flag: 1bit */
1148     bit_offset += 1;
1149     extraBit = (bit_offset % 8);
1150 
1151     /* NOTE: These infos would not be set because mastering_display_actual_peak_luminance_flag is always 0.
1152      * - num_rows_mastering_display_actual_peak_luminance: 5bit
1153      * - num_cols_mastering_display_actual_peak_luminance: 5bit
1154      * - mastering_display_actual_peak_luminance: 4bit */
1155 
1156     /* tone_mapping_flag: 1bit */
1157     if (pHDRDynamicInfo->data.tone_mapping.tone_mapping_flag) {
1158         *((char *)pBlob + (bit_offset / 8)) |= (1 << (7 - extraBit));
1159     }
1160     bit_offset += 1;
1161     extraBit = (bit_offset % 8);
1162 
1163     if (pHDRDynamicInfo->data.tone_mapping.tone_mapping_flag) {
1164         /* knee_point_x: 12bit */
1165         if (extraBit > 4)
1166             extraByte = 1;
1167 
1168         offset_limit = bit_offset + 12;
1169 
1170         tempBuffer = pHDRDynamicInfo->data.tone_mapping.knee_point_x;
1171         tempBuffer = tempBuffer << (20 - extraBit);
1172 
1173         for (i = 0; i < 4; i++) {
1174             memcpy((char *)&buffer + i, (char *)&tempBuffer + (3 - i), sizeof(char));
1175         }
1176 
1177         for (i = 0; i < (2 + extraByte); i++) {
1178             for (j = extraBit; j < 8; j++) {
1179                 *((char *)pBlob + (bit_offset / 8)) |= (*((char *)&buffer + i) & (1 << (7 - j)));
1180                 bit_offset++;
1181 
1182                 if (bit_offset == offset_limit) {
1183                     break;
1184                 }
1185             }
1186             extraBit = (bit_offset % 8);
1187         }
1188         buffer     = 0;
1189         tempBuffer = 0;
1190         extraByte  = 0;
1191 
1192         /* knee_point_y: 12bit */
1193         if (extraBit > 4)
1194             extraByte = 1;
1195 
1196         offset_limit = bit_offset + 12;
1197 
1198         tempBuffer = pHDRDynamicInfo->data.tone_mapping.knee_point_y;
1199         tempBuffer = tempBuffer << (20 - extraBit);
1200 
1201         for (j = 0; j < 4; j++) {
1202             memcpy((char *)&buffer + j, (char *)&tempBuffer + (3 - j), sizeof(char));
1203         }
1204 
1205         for (j = 0; j < (2 + extraByte); j++) {
1206             for (k = extraBit; k < 8; k++) {
1207                 *((char *)pBlob + (bit_offset / 8)) |= (*((char *)&buffer + j) & (1 << (7 - k)));
1208                 bit_offset++;
1209 
1210                 if (bit_offset == offset_limit) {
1211                     break;
1212                 }
1213             }
1214             extraBit = (bit_offset % 8);
1215         }
1216         buffer     = 0;
1217         tempBuffer = 0;
1218         extraByte  = 0;
1219 
1220         /* num_bezier_curve_anchors: 4bit */
1221         if (extraBit > 4)
1222             extraByte = 1;
1223 
1224         offset_limit = bit_offset + 4;
1225 
1226         tempBuffer = pHDRDynamicInfo->data.tone_mapping.num_bezier_curve_anchors;
1227         tempBuffer = tempBuffer << (28 - extraBit);
1228 
1229         for (j = 0; j < 4; j++) {
1230             memcpy((char *)&buffer + j, (char *)&tempBuffer + (3 - j), sizeof(char));
1231         }
1232 
1233         for (j = 0; j < (1 + extraByte); j++) {
1234             for (k = extraBit; k < 8; k++) {
1235                 *((char *)pBlob + (bit_offset / 8)) |= (*((char *)&buffer + j) & (1 << (7 - k)));
1236                 bit_offset++;
1237 
1238                 if (bit_offset == offset_limit) {
1239                     break;
1240                 }
1241             }
1242             extraBit = (bit_offset % 8);
1243         }
1244         buffer     = 0;
1245         tempBuffer = 0;
1246         extraByte  = 0;
1247 
1248         for (i = 0; i < pHDRDynamicInfo->data.tone_mapping.num_bezier_curve_anchors; i++) {
1249             /* bezier_curve_anchors: 10bit */
1250             if (extraBit > 6)
1251                 extraByte = 1;
1252 
1253             offset_limit = bit_offset + 10;
1254 
1255             tempBuffer = pHDRDynamicInfo->data.tone_mapping.bezier_curve_anchors[i];
1256             tempBuffer = tempBuffer << (22 - extraBit);
1257 
1258             for (j = 0; j < 4; j++) {
1259                 memcpy((char *)&buffer + j, (char *)&tempBuffer + (3 - j), sizeof(char));
1260             }
1261 
1262             for (j = 0; j < (2 + extraByte); j++) {
1263                 for (k = extraBit; k < 8; k++) {
1264                     *((char *)pBlob + (bit_offset / 8)) |= (*((char *)&buffer + j) & (1 << (7 - k)));
1265                     bit_offset++;
1266 
1267                     if (bit_offset == offset_limit) {
1268                         break;
1269                     }
1270                 }
1271                 extraBit = (bit_offset % 8);
1272             }
1273             buffer     = 0;
1274             tempBuffer = 0;
1275             extraByte  = 0;
1276         }
1277     }
1278 
1279     /* color_saturation_mapping_flag: 1bit */
1280     bit_offset += 1;
1281     extraBit = (bit_offset % 8);
1282 
1283     /* NOTE: This info would not be set because color_saturation_mapping_flag is always 0.
1284      * - color_saturation_weight: 6bit */
1285 
1286     if(extraBit > 0) {
1287         size = (bit_offset / 8) + 1;
1288     } else {
1289         size = (bit_offset / 8);
1290     }
1291 
1292     return size;
1293 }
1294 
1295 #ifdef __cplusplus
1296 }
1297 #endif
1298