hdr_util.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright (c) 2016 The WebM project authors. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the LICENSE file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. #include "hdr_util.h"
  9. #include <climits>
  10. #include <cstddef>
  11. #include <new>
  12. #include "mkvparser/mkvparser.h"
  13. namespace libwebm {
  14. const int Vp9CodecFeatures::kValueNotPresent = INT_MAX;
  15. bool CopyPrimaryChromaticity(const mkvparser::PrimaryChromaticity& parser_pc,
  16. PrimaryChromaticityPtr* muxer_pc) {
  17. muxer_pc->reset(new (std::nothrow)
  18. mkvmuxer::PrimaryChromaticity(parser_pc.x, parser_pc.y));
  19. if (!muxer_pc->get())
  20. return false;
  21. return true;
  22. }
  23. bool MasteringMetadataValuePresent(double value) {
  24. return value != mkvparser::MasteringMetadata::kValueNotPresent;
  25. }
  26. bool CopyMasteringMetadata(const mkvparser::MasteringMetadata& parser_mm,
  27. mkvmuxer::MasteringMetadata* muxer_mm) {
  28. if (MasteringMetadataValuePresent(parser_mm.luminance_max))
  29. muxer_mm->set_luminance_max(parser_mm.luminance_max);
  30. if (MasteringMetadataValuePresent(parser_mm.luminance_min))
  31. muxer_mm->set_luminance_min(parser_mm.luminance_min);
  32. PrimaryChromaticityPtr r_ptr(NULL);
  33. PrimaryChromaticityPtr g_ptr(NULL);
  34. PrimaryChromaticityPtr b_ptr(NULL);
  35. PrimaryChromaticityPtr wp_ptr(NULL);
  36. if (parser_mm.r) {
  37. if (!CopyPrimaryChromaticity(*parser_mm.r, &r_ptr))
  38. return false;
  39. }
  40. if (parser_mm.g) {
  41. if (!CopyPrimaryChromaticity(*parser_mm.g, &g_ptr))
  42. return false;
  43. }
  44. if (parser_mm.b) {
  45. if (!CopyPrimaryChromaticity(*parser_mm.b, &b_ptr))
  46. return false;
  47. }
  48. if (parser_mm.white_point) {
  49. if (!CopyPrimaryChromaticity(*parser_mm.white_point, &wp_ptr))
  50. return false;
  51. }
  52. if (!muxer_mm->SetChromaticity(r_ptr.get(), g_ptr.get(), b_ptr.get(),
  53. wp_ptr.get())) {
  54. return false;
  55. }
  56. return true;
  57. }
  58. bool ColourValuePresent(long long value) {
  59. return value != mkvparser::Colour::kValueNotPresent;
  60. }
  61. bool CopyColour(const mkvparser::Colour& parser_colour,
  62. mkvmuxer::Colour* muxer_colour) {
  63. if (!muxer_colour)
  64. return false;
  65. if (ColourValuePresent(parser_colour.matrix_coefficients))
  66. muxer_colour->set_matrix_coefficients(parser_colour.matrix_coefficients);
  67. if (ColourValuePresent(parser_colour.bits_per_channel))
  68. muxer_colour->set_bits_per_channel(parser_colour.bits_per_channel);
  69. if (ColourValuePresent(parser_colour.chroma_subsampling_horz)) {
  70. muxer_colour->set_chroma_subsampling_horz(
  71. parser_colour.chroma_subsampling_horz);
  72. }
  73. if (ColourValuePresent(parser_colour.chroma_subsampling_vert)) {
  74. muxer_colour->set_chroma_subsampling_vert(
  75. parser_colour.chroma_subsampling_vert);
  76. }
  77. if (ColourValuePresent(parser_colour.cb_subsampling_horz))
  78. muxer_colour->set_cb_subsampling_horz(parser_colour.cb_subsampling_horz);
  79. if (ColourValuePresent(parser_colour.cb_subsampling_vert))
  80. muxer_colour->set_cb_subsampling_vert(parser_colour.cb_subsampling_vert);
  81. if (ColourValuePresent(parser_colour.chroma_siting_horz))
  82. muxer_colour->set_chroma_siting_horz(parser_colour.chroma_siting_horz);
  83. if (ColourValuePresent(parser_colour.chroma_siting_vert))
  84. muxer_colour->set_chroma_siting_vert(parser_colour.chroma_siting_vert);
  85. if (ColourValuePresent(parser_colour.range))
  86. muxer_colour->set_range(parser_colour.range);
  87. if (ColourValuePresent(parser_colour.transfer_characteristics)) {
  88. muxer_colour->set_transfer_characteristics(
  89. parser_colour.transfer_characteristics);
  90. }
  91. if (ColourValuePresent(parser_colour.primaries))
  92. muxer_colour->set_primaries(parser_colour.primaries);
  93. if (ColourValuePresent(parser_colour.max_cll))
  94. muxer_colour->set_max_cll(parser_colour.max_cll);
  95. if (ColourValuePresent(parser_colour.max_fall))
  96. muxer_colour->set_max_fall(parser_colour.max_fall);
  97. if (parser_colour.mastering_metadata) {
  98. mkvmuxer::MasteringMetadata muxer_mm;
  99. if (!CopyMasteringMetadata(*parser_colour.mastering_metadata, &muxer_mm))
  100. return false;
  101. if (!muxer_colour->SetMasteringMetadata(muxer_mm))
  102. return false;
  103. }
  104. return true;
  105. }
  106. // Format of VPx private data:
  107. //
  108. // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  109. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  110. // | ID Byte | Length | |
  111. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
  112. // | |
  113. // : Bytes 1..Length of Codec Feature :
  114. // | |
  115. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  116. //
  117. // ID Byte Format
  118. // ID byte is an unsigned byte.
  119. // 0 1 2 3 4 5 6 7
  120. // +-+-+-+-+-+-+-+-+
  121. // |X| ID |
  122. // +-+-+-+-+-+-+-+-+
  123. //
  124. // The X bit is reserved.
  125. //
  126. // See the following link for more information:
  127. // http://www.webmproject.org/vp9/profiles/
  128. bool ParseVpxCodecPrivate(const uint8_t* private_data, int32_t length,
  129. Vp9CodecFeatures* features) {
  130. const int kVpxCodecPrivateMinLength = 3;
  131. if (!private_data || !features || length < kVpxCodecPrivateMinLength)
  132. return false;
  133. const uint8_t kVp9ProfileId = 1;
  134. const uint8_t kVp9LevelId = 2;
  135. const uint8_t kVp9BitDepthId = 3;
  136. const uint8_t kVp9ChromaSubsamplingId = 4;
  137. const int kVpxFeatureLength = 1;
  138. int offset = 0;
  139. // Set features to not set.
  140. features->profile = Vp9CodecFeatures::kValueNotPresent;
  141. features->level = Vp9CodecFeatures::kValueNotPresent;
  142. features->bit_depth = Vp9CodecFeatures::kValueNotPresent;
  143. features->chroma_subsampling = Vp9CodecFeatures::kValueNotPresent;
  144. do {
  145. const uint8_t id_byte = private_data[offset++];
  146. const uint8_t length_byte = private_data[offset++];
  147. if (length_byte != kVpxFeatureLength)
  148. return false;
  149. if (id_byte == kVp9ProfileId) {
  150. const int priv_profile = static_cast<int>(private_data[offset++]);
  151. if (priv_profile < 0 || priv_profile > 3)
  152. return false;
  153. if (features->profile != Vp9CodecFeatures::kValueNotPresent &&
  154. features->profile != priv_profile) {
  155. return false;
  156. }
  157. features->profile = priv_profile;
  158. } else if (id_byte == kVp9LevelId) {
  159. const int priv_level = static_cast<int>(private_data[offset++]);
  160. const int kNumLevels = 14;
  161. const int levels[kNumLevels] = {10, 11, 20, 21, 30, 31, 40,
  162. 41, 50, 51, 52, 60, 61, 62};
  163. for (int i = 0; i < kNumLevels; ++i) {
  164. if (priv_level == levels[i]) {
  165. if (features->level != Vp9CodecFeatures::kValueNotPresent &&
  166. features->level != priv_level) {
  167. return false;
  168. }
  169. features->level = priv_level;
  170. break;
  171. }
  172. }
  173. if (features->level == Vp9CodecFeatures::kValueNotPresent)
  174. return false;
  175. } else if (id_byte == kVp9BitDepthId) {
  176. const int priv_profile = static_cast<int>(private_data[offset++]);
  177. if (priv_profile != 8 && priv_profile != 10 && priv_profile != 12)
  178. return false;
  179. if (features->bit_depth != Vp9CodecFeatures::kValueNotPresent &&
  180. features->bit_depth != priv_profile) {
  181. return false;
  182. }
  183. features->bit_depth = priv_profile;
  184. } else if (id_byte == kVp9ChromaSubsamplingId) {
  185. const int priv_profile = static_cast<int>(private_data[offset++]);
  186. if (priv_profile != 0 && priv_profile != 2 && priv_profile != 3)
  187. return false;
  188. if (features->chroma_subsampling != Vp9CodecFeatures::kValueNotPresent &&
  189. features->chroma_subsampling != priv_profile) {
  190. return false;
  191. }
  192. features->chroma_subsampling = priv_profile;
  193. } else {
  194. // Invalid ID.
  195. return false;
  196. }
  197. } while (offset + kVpxCodecPrivateMinLength <= length);
  198. return true;
  199. }
  200. } // namespace libwebm