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.

203 lines
7.4KB

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