]> git.sesse.net Git - ffmpeg/blob - libavcodec/dynamic_hdr10_plus.c
avcodec/dynamic_hdr10_plus: use get_bits_long() where needed
[ffmpeg] / libavcodec / dynamic_hdr10_plus.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include "dynamic_hdr10_plus.h"
20
21 static const uint8_t usa_country_code = 0xB5;
22 static const uint16_t smpte_provider_code = 0x003C;
23 static const uint16_t smpte2094_40_provider_oriented_code = 0x0001;
24 static const uint16_t smpte2094_40_application_identifier = 0x04;
25 static const int64_t luminance_den = 1;
26 static const int32_t peak_luminance_den = 15;
27 static const int64_t rgb_den = 100000;
28 static const int32_t fraction_pixel_den = 1000;
29 static const int32_t knee_point_den = 4095;
30 static const int32_t bezier_anchor_den = 1023;
31 static const int32_t saturation_weight_den = 8;
32
33 int ff_parse_itu_t_t35_to_dynamic_hdr10_plus(GetBitContext *gb, AVDynamicHDRPlus *s)
34 {
35     if (!s)
36         return AVERROR(ENOMEM);
37
38     s->application_version = get_bits(gb, 8);
39
40     if (get_bits_left(gb) < 2)
41         return AVERROR_INVALIDDATA;
42     s->num_windows = get_bits(gb, 2);
43
44     if (s->num_windows < 1 || s->num_windows > 3) {
45         return AVERROR_INVALIDDATA;
46     }
47
48     if (get_bits_left(gb) < ((19 * 8 + 1) * (s->num_windows - 1)))
49         return AVERROR_INVALIDDATA;
50
51     for (int w = 1; w < s->num_windows; w++) {
52         // The corners are set to absolute coordinates here. They should be
53         // converted to the relative coordinates (in [0, 1]) in the decoder.
54         AVHDRPlusColorTransformParams *params = &s->params[w];
55         params->window_upper_left_corner_x =
56             (AVRational){get_bits(gb, 16), 1};
57         params->window_upper_left_corner_y =
58             (AVRational){get_bits(gb, 16), 1};
59         params->window_lower_right_corner_x =
60             (AVRational){get_bits(gb, 16), 1};
61         params->window_lower_right_corner_y =
62             (AVRational){get_bits(gb, 16), 1};
63
64         params->center_of_ellipse_x = get_bits(gb, 16);
65         params->center_of_ellipse_y = get_bits(gb, 16);
66         params->rotation_angle = get_bits(gb, 8);
67         params->semimajor_axis_internal_ellipse = get_bits(gb, 16);
68         params->semimajor_axis_external_ellipse = get_bits(gb, 16);
69         params->semiminor_axis_external_ellipse = get_bits(gb, 16);
70         params->overlap_process_option = get_bits1(gb);
71     }
72
73     if (get_bits_left(gb) < 28)
74         return AVERROR(EINVAL);
75
76     s->targeted_system_display_maximum_luminance =
77         (AVRational){get_bits_long(gb, 27), luminance_den};
78     s->targeted_system_display_actual_peak_luminance_flag = get_bits1(gb);
79
80     if (s->targeted_system_display_actual_peak_luminance_flag) {
81         int rows, cols;
82         if (get_bits_left(gb) < 10)
83             return AVERROR(EINVAL);
84         rows = get_bits(gb, 5);
85         cols = get_bits(gb, 5);
86         if (((rows < 2) || (rows > 25)) || ((cols < 2) || (cols > 25))) {
87             return AVERROR_INVALIDDATA;
88         }
89         s->num_rows_targeted_system_display_actual_peak_luminance = rows;
90         s->num_cols_targeted_system_display_actual_peak_luminance = cols;
91
92         if (get_bits_left(gb) < (rows * cols * 4))
93             return AVERROR(EINVAL);
94
95         for (int i = 0; i < rows; i++) {
96             for (int j = 0; j < cols; j++) {
97                 s->targeted_system_display_actual_peak_luminance[i][j] =
98                     (AVRational){get_bits(gb, 4), peak_luminance_den};
99             }
100         }
101     }
102     for (int w = 0; w < s->num_windows; w++) {
103         AVHDRPlusColorTransformParams *params = &s->params[w];
104         if (get_bits_left(gb) < (3 * 17 + 17 + 4))
105             return AVERROR(EINVAL);
106
107         for (int i = 0; i < 3; i++) {
108             params->maxscl[i] =
109                 (AVRational){get_bits(gb, 17), rgb_den};
110         }
111         params->average_maxrgb =
112             (AVRational){get_bits(gb, 17), rgb_den};
113         params->num_distribution_maxrgb_percentiles = get_bits(gb, 4);
114
115         if (get_bits_left(gb) <
116             (params->num_distribution_maxrgb_percentiles * 24))
117             return AVERROR(EINVAL);
118
119         for (int i = 0; i < params->num_distribution_maxrgb_percentiles; i++) {
120             params->distribution_maxrgb[i].percentage = get_bits(gb, 7);
121             params->distribution_maxrgb[i].percentile =
122                 (AVRational){get_bits(gb, 17), rgb_den};
123         }
124
125         if (get_bits_left(gb) < 10)
126             return AVERROR(EINVAL);
127
128         params->fraction_bright_pixels = (AVRational){get_bits(gb, 10), fraction_pixel_den};
129     }
130     if (get_bits_left(gb) < 1)
131         return AVERROR(EINVAL);
132     s->mastering_display_actual_peak_luminance_flag = get_bits1(gb);
133     if (s->mastering_display_actual_peak_luminance_flag) {
134         int rows, cols;
135         if (get_bits_left(gb) < 10)
136             return AVERROR(EINVAL);
137         rows = get_bits(gb, 5);
138         cols = get_bits(gb, 5);
139         if (((rows < 2) || (rows > 25)) || ((cols < 2) || (cols > 25))) {
140             return AVERROR_INVALIDDATA;
141         }
142         s->num_rows_mastering_display_actual_peak_luminance = rows;
143         s->num_cols_mastering_display_actual_peak_luminance = cols;
144
145         if (get_bits_left(gb) < (rows * cols * 4))
146             return AVERROR(EINVAL);
147
148         for (int i = 0; i < rows; i++) {
149             for (int j = 0; j < cols; j++) {
150                 s->mastering_display_actual_peak_luminance[i][j] =
151                     (AVRational){get_bits(gb, 4), peak_luminance_den};
152             }
153         }
154     }
155
156     for (int w = 0; w < s->num_windows; w++) {
157         AVHDRPlusColorTransformParams *params = &s->params[w];
158         if (get_bits_left(gb) < 1)
159             return AVERROR(EINVAL);
160
161         params->tone_mapping_flag = get_bits1(gb);
162         if (params->tone_mapping_flag) {
163             if (get_bits_left(gb) < 28)
164                 return AVERROR(EINVAL);
165
166             params->knee_point_x =
167                 (AVRational){get_bits(gb, 12), knee_point_den};
168             params->knee_point_y =
169                 (AVRational){get_bits(gb, 12), knee_point_den};
170             params->num_bezier_curve_anchors = get_bits(gb, 4);
171
172             if (get_bits_left(gb) < (params->num_bezier_curve_anchors * 10))
173                 return AVERROR(EINVAL);
174
175             for (int i = 0; i < params->num_bezier_curve_anchors; i++) {
176                 params->bezier_curve_anchors[i] =
177                     (AVRational){get_bits(gb, 10), bezier_anchor_den};
178             }
179         }
180
181         if (get_bits_left(gb) < 1)
182             return AVERROR(EINVAL);
183         params->color_saturation_mapping_flag = get_bits1(gb);
184         if (params->color_saturation_mapping_flag) {
185             if (get_bits_left(gb) < 6)
186                 return AVERROR(EINVAL);
187             params->color_saturation_weight =
188                 (AVRational){get_bits(gb, 6), saturation_weight_den};
189         }
190     }
191
192     skip_bits(gb, get_bits_left(gb));
193
194     return 0;
195 }