Shaka Packager SDK
Loading...
Searching...
No Matches
dvb_image.cc
1// Copyright 2020 Google LLC. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file or at
5// https://developers.google.com/open-source/licenses/bsd
6
7#include <packager/media/formats/dvb/dvb_image.h>
8
9#include <algorithm>
10#include <cstdint>
11#include <cstring>
12#include <tuple>
13
14#include <absl/log/check.h>
15#include <absl/log/log.h>
16
17namespace shaka {
18namespace media {
19
20namespace {
21
22// See ETSI EN 300 743 Section 9.1.
23constexpr const uint8_t k4To2ReductionMap[] = {
24 0x0, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
25 0x2, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3,
26};
27
28// The only time when A==0 is when it is transparent. This means we can use
29// other values internally for special values.
30constexpr const RgbaColor kNoColor{145, 92, 47, 0};
31
32// DVB uses transparency, but libpng uses alpha, so we need to reverse the T
33// value so we can pass the value to libpng.
34#define COLOR(r, g, b, t) \
35 RgbaColor { \
36 static_cast<uint8_t>(255 * (r) / 100), \
37 static_cast<uint8_t>(255 * (g) / 100), \
38 static_cast<uint8_t>(255 * (b) / 100), \
39 static_cast<uint8_t>(255 * (100 - t) / 100) \
40 }
41// Default color maps see ETSI EN 300 743 Section 10.
42constexpr const RgbaColor k2BitDefaultColors[] = {
43 COLOR(0, 0, 0, 100), // 0 = 0b00
44 COLOR(100, 100, 100, 0), // 1 = 0b01
45 COLOR(0, 0, 0, 0), // 2 = 0b10
46 COLOR(50, 50, 50, 0), // 3 = 0b11
47};
48// Default color maps see ETSI EN 300 743 Section 10.
49constexpr const RgbaColor k4BitDefaultColors[] = {
50 COLOR(0, 0, 0, 100), // 0 = 0b0000
51 COLOR(100, 0, 0, 0), // 1 = 0b0001
52 COLOR(0, 100, 0, 0), // 2 = 0b0010
53 COLOR(100, 100, 0, 0), // 3 = 0b0011
54 COLOR(0, 0, 100, 0), // 4 = 0b0100
55 COLOR(100, 0, 100, 0), // 5 = 0b0101
56 COLOR(0, 100, 100, 0), // 6 = 0b0110
57 COLOR(100, 100, 100, 0), // 7 = 0b0111
58
59 COLOR(0, 0, 0, 0), // 8 = 0b1000
60 COLOR(50, 0, 0, 0), // 9 = 0b1001
61 COLOR(0, 50, 0, 0), // 10 = 0b1010
62 COLOR(50, 50, 0, 0), // 11 = 0b1011
63 COLOR(0, 0, 50, 0), // 12 = 0b1100
64 COLOR(50, 0, 50, 0), // 13 = 0b1101
65 COLOR(0, 50, 50, 0), // 14 = 0b1110
66 COLOR(50, 50, 50, 0), // 15 = 0b1111
67};
68
69#define GET_BIT(n) ((entry_id >> (8 - (n))) & 0x1)
70// Default color maps see ETSI EN 300 743 Section 10.
71RgbaColor Get8BitDefaultColor(uint8_t entry_id) {
72 uint8_t r, g, b, t;
73 if (entry_id == 0) {
74 return COLOR(0, 0, 0, 100);
75 } else if ((entry_id & 0xf8) == 0) {
76 r = 100 * GET_BIT(8);
77 g = 100 * GET_BIT(7);
78 b = 100 * GET_BIT(6);
79 t = 75;
80 } else if (!GET_BIT(1)) {
81 r = (33 * GET_BIT(8)) + (67 * GET_BIT(4));
82 g = (33 * GET_BIT(7)) + (67 * GET_BIT(3));
83 b = (33 * GET_BIT(6)) + (67 * GET_BIT(2));
84 t = GET_BIT(5) ? 50 : 0;
85 } else {
86 r = (17 * GET_BIT(8)) + (33 * GET_BIT(4)) + (GET_BIT(5) ? 0 : 50);
87 g = (17 * GET_BIT(7)) + (33 * GET_BIT(3)) + (GET_BIT(5) ? 0 : 50);
88 b = (17 * GET_BIT(6)) + (33 * GET_BIT(2)) + (GET_BIT(5) ? 0 : 50);
89 t = 0;
90 }
91 return COLOR(r, g, b, t);
92}
93#undef GET_BIT
94#undef COLOR
95
96} // namespace
97
98DvbImageColorSpace::DvbImageColorSpace() {
99 for (auto& item : color_map_2_)
100 item = kNoColor;
101 for (auto& item : color_map_4_)
102 item = kNoColor;
103 for (auto& item : color_map_8_)
104 item = kNoColor;
105}
106
107DvbImageColorSpace::~DvbImageColorSpace() {}
108
109RgbaColor DvbImageColorSpace::GetColor(BitDepth bit_depth,
110 uint8_t entry_id) const {
111 auto color = GetColorRaw(bit_depth, entry_id);
112 if (color != kNoColor)
113 return color;
114
115 // If we don't have the exact bit-depth, try mapping to another bit-depth.
116 // See ETSI EN 300 743 Section 9.
117 RgbaColor default_color, alt1, alt2;
118 switch (bit_depth) {
119 case BitDepth::k2Bit:
120 DCHECK_LT(entry_id, 4u);
121 alt1 = GetColorRaw(BitDepth::k4Bit, bit_depth_2_to_4_[entry_id]);
122 alt2 = GetColorRaw(BitDepth::k8Bit, bit_depth_2_to_8_[entry_id]);
123 default_color = k2BitDefaultColors[entry_id];
124 break;
125 case BitDepth::k4Bit:
126 DCHECK_LT(entry_id, 16u);
127 alt1 = GetColorRaw(BitDepth::k8Bit, bit_depth_4_to_8_[entry_id]);
128 alt2 = GetColorRaw(BitDepth::k2Bit, k4To2ReductionMap[entry_id]);
129 default_color = k4BitDefaultColors[entry_id];
130 break;
131 case BitDepth::k8Bit:
132 // 8-to-4-bit reduction is just take the low bits.
133 alt1 = GetColorRaw(BitDepth::k4Bit, entry_id & 0xf);
134 alt2 = GetColorRaw(BitDepth::k2Bit, k4To2ReductionMap[entry_id & 0xf]);
135 default_color = Get8BitDefaultColor(entry_id);
136 break;
137 default:
138 // Windows can't detect that all enums are handled and doesn't like
139 // NOTIMPLEMENTED.
140 return kNoColor;
141 }
142
143 if (alt1 != kNoColor)
144 return alt1;
145 if (alt2 != kNoColor)
146 return alt2;
147 return default_color;
148}
149
150void DvbImageColorSpace::SetColor(BitDepth bit_depth,
151 uint8_t entry_id,
152 RgbaColor color) {
153 DCHECK(color != kNoColor);
154 switch (bit_depth) {
155 case BitDepth::k2Bit:
156 DCHECK_LT(entry_id, 4u);
157 color_map_2_[entry_id] = color;
158 break;
159 case BitDepth::k4Bit:
160 DCHECK_LT(entry_id, 16u);
161 color_map_4_[entry_id] = color;
162 break;
163 case BitDepth::k8Bit:
164 color_map_8_[entry_id] = color;
165 break;
166 }
167}
168
170 memcpy(bit_depth_2_to_4_, map, sizeof(bit_depth_2_to_4_));
171}
172
174 memcpy(bit_depth_2_to_8_, map, sizeof(bit_depth_2_to_8_));
175}
176
178 memcpy(bit_depth_4_to_8_, map, sizeof(bit_depth_4_to_8_));
179}
180
181RgbaColor DvbImageColorSpace::GetColorRaw(BitDepth bit_depth,
182 uint8_t entry_id) const {
183 switch (bit_depth) {
184 case BitDepth::k2Bit:
185 return color_map_2_[entry_id];
186 case BitDepth::k4Bit:
187 return color_map_4_[entry_id];
188 case BitDepth::k8Bit:
189 return color_map_8_[entry_id];
190 }
191 // Not reached, but Windows doesn't like NOTIMPLEMENTED.
192 return kNoColor;
193}
194
195DvbImageBuilder::DvbImageBuilder(const DvbImageColorSpace* color_space,
196 const RgbaColor& default_color,
197 uint16_t max_width,
198 uint16_t max_height)
199 : pixels_(new RgbaColor[max_width * max_height]),
200 color_space_(color_space),
201 top_pos_{0, 0},
202 bottom_pos_{0, 1}, // Skip top row for bottom row.
203 max_width_(max_width),
204 max_height_(max_height),
205 width_(0) {
206 for (size_t i = 0; i < static_cast<size_t>(max_width) * max_height; i++)
207 pixels_[i] = default_color;
208}
209
210DvbImageBuilder::~DvbImageBuilder() {}
211
212bool DvbImageBuilder::AddPixel(BitDepth bit_depth,
213 uint8_t byte_code,
214 bool is_top_rows) {
215 auto& pos = is_top_rows ? top_pos_ : bottom_pos_;
216 if (pos.x >= max_width_ || pos.y >= max_height_) {
217 LOG(ERROR) << "DVB-sub image cannot fit in region/window";
218 return false;
219 }
220
221 pixels_[pos.y * max_width_ + pos.x++] =
222 color_space_->GetColor(bit_depth, byte_code);
223 if (pos.x > width_)
224 width_ = pos.x;
225 return true;
226}
227
228void DvbImageBuilder::NewRow(bool is_top_rows) {
229 auto& pos = is_top_rows ? top_pos_ : bottom_pos_;
230 pos.x = 0;
231 pos.y += 2; // Skip other row.
232}
233
235 for (size_t line = 0; line < max_height_ - 1u; line += 2) {
236 std::memcpy(&pixels_[(line + 1) * max_width_], &pixels_[line * max_width_],
237 max_width_ * sizeof(RgbaColor));
238 }
239 bottom_pos_ = top_pos_;
240 if (max_height_ % 2 == 0)
241 bottom_pos_.y++;
242 else
243 bottom_pos_.y--; // Odd-height images don't end in odd-row, so move back.
244}
245
247 uint16_t* width,
248 uint16_t* height) const {
249 size_t max_y, min_y;
250 std::tie(min_y, max_y) = std::minmax(top_pos_.y, bottom_pos_.y);
251 if (max_y == 1 || max_y != min_y + 1) {
252 // 1. We should have at least one row.
253 // 2. Both top-rows and bottom-rows should have the same number of rows.
254 LOG(ERROR) << "Incomplete DVB-sub image";
255 return false;
256 }
257
258 *width = width_;
259 // We skipped the other row in NewRow, so rollback.
260 *height = static_cast<uint16_t>(max_y - 1);
261 *pixels = pixels_.get();
262 if (*height > max_height_) {
263 LOG(ERROR) << "DVB-sub image cannot fit in region/window";
264 return false;
265 }
266
267 return true;
268}
269
270} // namespace media
271} // namespace shaka
bool GetPixels(const RgbaColor **pixels, uint16_t *width, uint16_t *height) const
Definition dvb_image.cc:246
void MirrorToBottomRows()
Copies the top-rows to the bottom rows.
Definition dvb_image.cc:234
void Set4To8BitDepthMap(const uint8_t *map)
Must pass a 16-element array; elements are copied over.
Definition dvb_image.cc:177
void Set2To8BitDepthMap(const uint8_t *map)
Must pass a 4-element array; elements are copied over.
Definition dvb_image.cc:173
void Set2To4BitDepthMap(const uint8_t *map)
Must pass a 4-element array; elements are copied over.
Definition dvb_image.cc:169
All the methods that are virtual are virtual for mocking.