You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
7.2KB

  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. #include "dynamic_hdr10_plus.h"
  19. static const uint8_t usa_country_code = 0xB5;
  20. static const uint16_t smpte_provider_code = 0x003C;
  21. static const uint16_t smpte2094_40_provider_oriented_code = 0x0001;
  22. static const uint16_t smpte2094_40_application_identifier = 0x04;
  23. static const int64_t luminance_den = 1;
  24. static const int32_t peak_luminance_den = 15;
  25. static const int64_t rgb_den = 100000;
  26. static const int32_t fraction_pixel_den = 1000;
  27. static const int32_t knee_point_den = 4095;
  28. static const int32_t bezier_anchor_den = 1023;
  29. static const int32_t saturation_weight_den = 8;
  30. int ff_parse_itu_t_t35_to_dynamic_hdr10_plus(GetBitContext *gb, AVDynamicHDRPlus *s)
  31. {
  32. if (!s)
  33. return AVERROR(ENOMEM);
  34. s->application_version = get_bits(gb, 8);
  35. if (get_bits_left(gb) < 2)
  36. return AVERROR_INVALIDDATA;
  37. s->num_windows = get_bits(gb, 2);
  38. if (s->num_windows < 1 || s->num_windows > 3) {
  39. return AVERROR_INVALIDDATA;
  40. }
  41. if (get_bits_left(gb) < ((19 * 8 + 1) * (s->num_windows - 1)))
  42. return AVERROR_INVALIDDATA;
  43. for (int w = 1; w < s->num_windows; w++) {
  44. // The corners are set to absolute coordinates here. They should be
  45. // converted to the relative coordinates (in [0, 1]) in the decoder.
  46. AVHDRPlusColorTransformParams *params = &s->params[w];
  47. params->window_upper_left_corner_x =
  48. (AVRational){get_bits(gb, 16), 1};
  49. params->window_upper_left_corner_y =
  50. (AVRational){get_bits(gb, 16), 1};
  51. params->window_lower_right_corner_x =
  52. (AVRational){get_bits(gb, 16), 1};
  53. params->window_lower_right_corner_y =
  54. (AVRational){get_bits(gb, 16), 1};
  55. params->center_of_ellipse_x = get_bits(gb, 16);
  56. params->center_of_ellipse_y = get_bits(gb, 16);
  57. params->rotation_angle = get_bits(gb, 8);
  58. params->semimajor_axis_internal_ellipse = get_bits(gb, 16);
  59. params->semimajor_axis_external_ellipse = get_bits(gb, 16);
  60. params->semiminor_axis_external_ellipse = get_bits(gb, 16);
  61. params->overlap_process_option = get_bits1(gb);
  62. }
  63. if (get_bits_left(gb) < 28)
  64. return AVERROR(EINVAL);
  65. s->targeted_system_display_maximum_luminance =
  66. (AVRational){get_bits(gb, 27), luminance_den};
  67. s->targeted_system_display_actual_peak_luminance_flag = get_bits1(gb);
  68. if (s->targeted_system_display_actual_peak_luminance_flag) {
  69. int rows, cols;
  70. if (get_bits_left(gb) < 10)
  71. return AVERROR(EINVAL);
  72. rows = get_bits(gb, 5);
  73. cols = get_bits(gb, 5);
  74. if (((rows < 2) || (rows > 25)) || ((cols < 2) || (cols > 25))) {
  75. return AVERROR_INVALIDDATA;
  76. }
  77. s->num_rows_targeted_system_display_actual_peak_luminance = rows;
  78. s->num_cols_targeted_system_display_actual_peak_luminance = cols;
  79. if (get_bits_left(gb) < (rows * cols * 4))
  80. return AVERROR(EINVAL);
  81. for (int i = 0; i < rows; i++) {
  82. for (int j = 0; j < cols; j++) {
  83. s->targeted_system_display_actual_peak_luminance[i][j] =
  84. (AVRational){get_bits(gb, 4), peak_luminance_den};
  85. }
  86. }
  87. }
  88. for (int w = 0; w < s->num_windows; w++) {
  89. AVHDRPlusColorTransformParams *params = &s->params[w];
  90. if (get_bits_left(gb) < (3 * 17 + 17 + 4))
  91. return AVERROR(EINVAL);
  92. for (int i = 0; i < 3; i++) {
  93. params->maxscl[i] =
  94. (AVRational){get_bits(gb, 17), rgb_den};
  95. }
  96. params->average_maxrgb =
  97. (AVRational){get_bits(gb, 17), rgb_den};
  98. params->num_distribution_maxrgb_percentiles = get_bits(gb, 4);
  99. if (get_bits_left(gb) <
  100. (params->num_distribution_maxrgb_percentiles * 24))
  101. return AVERROR(EINVAL);
  102. for (int i = 0; i < params->num_distribution_maxrgb_percentiles; i++) {
  103. params->distribution_maxrgb[i].percentage = get_bits(gb, 7);
  104. params->distribution_maxrgb[i].percentile =
  105. (AVRational){get_bits(gb, 17), rgb_den};
  106. }
  107. if (get_bits_left(gb) < 10)
  108. return AVERROR(EINVAL);
  109. params->fraction_bright_pixels = (AVRational){get_bits(gb, 10), fraction_pixel_den};
  110. }
  111. if (get_bits_left(gb) < 1)
  112. return AVERROR(EINVAL);
  113. s->mastering_display_actual_peak_luminance_flag = get_bits1(gb);
  114. if (s->mastering_display_actual_peak_luminance_flag) {
  115. int rows, cols;
  116. if (get_bits_left(gb) < 10)
  117. return AVERROR(EINVAL);
  118. rows = get_bits(gb, 5);
  119. cols = get_bits(gb, 5);
  120. if (((rows < 2) || (rows > 25)) || ((cols < 2) || (cols > 25))) {
  121. return AVERROR_INVALIDDATA;
  122. }
  123. s->num_rows_mastering_display_actual_peak_luminance = rows;
  124. s->num_cols_mastering_display_actual_peak_luminance = cols;
  125. if (get_bits_left(gb) < (rows * cols * 4))
  126. return AVERROR(EINVAL);
  127. for (int i = 0; i < rows; i++) {
  128. for (int j = 0; j < cols; j++) {
  129. s->mastering_display_actual_peak_luminance[i][j] =
  130. (AVRational){get_bits(gb, 4), peak_luminance_den};
  131. }
  132. }
  133. }
  134. for (int w = 0; w < s->num_windows; w++) {
  135. AVHDRPlusColorTransformParams *params = &s->params[w];
  136. if (get_bits_left(gb) < 1)
  137. return AVERROR(EINVAL);
  138. params->tone_mapping_flag = get_bits1(gb);
  139. if (params->tone_mapping_flag) {
  140. if (get_bits_left(gb) < 28)
  141. return AVERROR(EINVAL);
  142. params->knee_point_x =
  143. (AVRational){get_bits(gb, 12), knee_point_den};
  144. params->knee_point_y =
  145. (AVRational){get_bits(gb, 12), knee_point_den};
  146. params->num_bezier_curve_anchors = get_bits(gb, 4);
  147. if (get_bits_left(gb) < (params->num_bezier_curve_anchors * 10))
  148. return AVERROR(EINVAL);
  149. for (int i = 0; i < params->num_bezier_curve_anchors; i++) {
  150. params->bezier_curve_anchors[i] =
  151. (AVRational){get_bits(gb, 10), bezier_anchor_den};
  152. }
  153. }
  154. if (get_bits_left(gb) < 1)
  155. return AVERROR(EINVAL);
  156. params->color_saturation_mapping_flag = get_bits1(gb);
  157. if (params->color_saturation_mapping_flag) {
  158. if (get_bits_left(gb) < 6)
  159. return AVERROR(EINVAL);
  160. params->color_saturation_weight =
  161. (AVRational){get_bits(gb, 6), saturation_weight_den};
  162. }
  163. }
  164. skip_bits(gb, get_bits_left(gb));
  165. return 0;
  166. }