]> granicus.if.org Git - libvpx/blob - third_party/libwebm/common/hdr_util.cc
third_party: Roll libwebm snapshot.
[libvpx] / third_party / libwebm / common / hdr_util.cc
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
10 #include <cstddef>
11 #include <new>
12
13 #include "mkvparser/mkvparser.h"
14
15 namespace libwebm {
16 bool CopyPrimaryChromaticity(const mkvparser::PrimaryChromaticity& parser_pc,
17                              PrimaryChromaticityPtr* muxer_pc) {
18   muxer_pc->reset(new (std::nothrow)
19                       mkvmuxer::PrimaryChromaticity(parser_pc.x, parser_pc.y));
20   if (!muxer_pc->get())
21     return false;
22   return true;
23 }
24
25 bool MasteringMetadataValuePresent(double value) {
26   return value != mkvparser::MasteringMetadata::kValueNotPresent;
27 }
28
29 bool CopyMasteringMetadata(const mkvparser::MasteringMetadata& parser_mm,
30                            mkvmuxer::MasteringMetadata* muxer_mm) {
31   if (MasteringMetadataValuePresent(parser_mm.luminance_max))
32     muxer_mm->luminance_max = parser_mm.luminance_max;
33   if (MasteringMetadataValuePresent(parser_mm.luminance_min))
34     muxer_mm->luminance_min = parser_mm.luminance_min;
35
36   PrimaryChromaticityPtr r_ptr(NULL);
37   PrimaryChromaticityPtr g_ptr(NULL);
38   PrimaryChromaticityPtr b_ptr(NULL);
39   PrimaryChromaticityPtr wp_ptr(NULL);
40
41   if (parser_mm.r) {
42     if (!CopyPrimaryChromaticity(*parser_mm.r, &r_ptr))
43       return false;
44   }
45   if (parser_mm.g) {
46     if (!CopyPrimaryChromaticity(*parser_mm.g, &g_ptr))
47       return false;
48   }
49   if (parser_mm.b) {
50     if (!CopyPrimaryChromaticity(*parser_mm.b, &b_ptr))
51       return false;
52   }
53   if (parser_mm.white_point) {
54     if (!CopyPrimaryChromaticity(*parser_mm.white_point, &wp_ptr))
55       return false;
56   }
57
58   if (!muxer_mm->SetChromaticity(r_ptr.get(), g_ptr.get(), b_ptr.get(),
59                                  wp_ptr.get())) {
60     return false;
61   }
62
63   return true;
64 }
65
66 bool ColourValuePresent(long long value) {
67   return value != mkvparser::Colour::kValueNotPresent;
68 }
69
70 bool CopyColour(const mkvparser::Colour& parser_colour,
71                 mkvmuxer::Colour* muxer_colour) {
72   if (!muxer_colour)
73     return false;
74
75   if (ColourValuePresent(parser_colour.matrix_coefficients))
76     muxer_colour->matrix_coefficients = parser_colour.matrix_coefficients;
77   if (ColourValuePresent(parser_colour.bits_per_channel))
78     muxer_colour->bits_per_channel = parser_colour.bits_per_channel;
79   if (ColourValuePresent(parser_colour.chroma_subsampling_horz))
80     muxer_colour->chroma_subsampling_horz =
81         parser_colour.chroma_subsampling_horz;
82   if (ColourValuePresent(parser_colour.chroma_subsampling_vert))
83     muxer_colour->chroma_subsampling_vert =
84         parser_colour.chroma_subsampling_vert;
85   if (ColourValuePresent(parser_colour.cb_subsampling_horz))
86     muxer_colour->cb_subsampling_horz = parser_colour.cb_subsampling_horz;
87   if (ColourValuePresent(parser_colour.cb_subsampling_vert))
88     muxer_colour->cb_subsampling_vert = parser_colour.cb_subsampling_vert;
89   if (ColourValuePresent(parser_colour.chroma_siting_horz))
90     muxer_colour->chroma_siting_horz = parser_colour.chroma_siting_horz;
91   if (ColourValuePresent(parser_colour.chroma_siting_vert))
92     muxer_colour->chroma_siting_vert = parser_colour.chroma_siting_vert;
93   if (ColourValuePresent(parser_colour.range))
94     muxer_colour->range = parser_colour.range;
95   if (ColourValuePresent(parser_colour.transfer_characteristics))
96     muxer_colour->transfer_characteristics =
97         parser_colour.transfer_characteristics;
98   if (ColourValuePresent(parser_colour.primaries))
99     muxer_colour->primaries = parser_colour.primaries;
100   if (ColourValuePresent(parser_colour.max_cll))
101     muxer_colour->max_cll = parser_colour.max_cll;
102   if (ColourValuePresent(parser_colour.max_fall))
103     muxer_colour->max_fall = parser_colour.max_fall;
104
105   if (parser_colour.mastering_metadata) {
106     mkvmuxer::MasteringMetadata muxer_mm;
107     if (!CopyMasteringMetadata(*parser_colour.mastering_metadata, &muxer_mm))
108       return false;
109     if (!muxer_colour->SetMasteringMetadata(muxer_mm))
110       return false;
111   }
112   return true;
113 }
114
115 // Format of VPx private data:
116 //
117 //   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
118 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
119 //  |    ID Byte    |             Length            |               |
120 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+               |
121 //  |                                                               |
122 //  :               Bytes 1..Length of Codec Feature                :
123 //  |                                                               |
124 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
125 //
126 // ID Byte Format
127 // ID byte is an unsigned byte.
128 //   0 1 2 3 4 5 6 7
129 //  +-+-+-+-+-+-+-+-+
130 //  |X|    ID       |
131 //  +-+-+-+-+-+-+-+-+
132 //
133 // The X bit is reserved.
134 //
135 // Currently only profile level is supported. ID byte must be set to 1, and
136 // length must be 1. Supported values are:
137 //
138 //   10: Level 1
139 //   11: Level 1.1
140 //   20: Level 2
141 //   21: Level 2.1
142 //   30: Level 3
143 //   31: Level 3.1
144 //   40: Level 4
145 //   41: Level 4.1
146 //   50: Level 5
147 //   51: Level 5.1
148 //   52: Level 5.2
149 //   60: Level 6
150 //   61: Level 6.1
151 //   62: Level 6.2
152 //
153 // See the following link for more information:
154 // http://www.webmproject.org/vp9/profiles/
155 int ParseVpxCodecPrivate(const uint8_t* private_data, int32_t length) {
156   const int kVpxCodecPrivateLength = 3;
157   if (!private_data || length != kVpxCodecPrivateLength)
158     return 0;
159
160   const uint8_t id_byte = *private_data;
161   if (id_byte != 1)
162     return 0;
163
164   const int kVpxProfileLength = 1;
165   const uint8_t length_byte = private_data[1];
166   if (length_byte != kVpxProfileLength)
167     return 0;
168
169   const int level = static_cast<int>(private_data[2]);
170
171   const int kNumLevels = 14;
172   const int levels[kNumLevels] = {10, 11, 20, 21, 30, 31, 40,
173                                   41, 50, 51, 52, 60, 61, 62};
174
175   for (int i = 0; i < kNumLevels; ++i) {
176     if (level == levels[i])
177       return level;
178   }
179
180   return 0;
181 }
182 }  // namespace libwebm