7 #include "packager/media/formats/dvb/dvb_image.h"
13 #include "packager/base/logging.h"
21 constexpr
const uint8_t k4To2ReductionMap[] = {
22 0x0, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
23 0x2, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3,
28 constexpr
const RgbaColor kNoColor{145, 92, 47, 0};
32 #define COLOR(r, g, b, t) \
34 static_cast<uint8_t>(255 * (r) / 100), \
35 static_cast<uint8_t>(255 * (g) / 100), \
36 static_cast<uint8_t>(255 * (b) / 100), \
37 static_cast<uint8_t>(255 * (100 - t) / 100) \
40 constexpr
const RgbaColor k2BitDefaultColors[] = {
42 COLOR(100, 100, 100, 0),
47 constexpr
const RgbaColor k4BitDefaultColors[] = {
51 COLOR(100, 100, 0, 0),
53 COLOR(100, 0, 100, 0),
54 COLOR(0, 100, 100, 0),
55 COLOR(100, 100, 100, 0),
67 #define GET_BIT(n) ((entry_id >> (8 - (n))) & 0x1)
69 RgbaColor Get8BitDefaultColor(uint8_t entry_id) {
72 return COLOR(0, 0, 0, 100);
73 }
else if ((entry_id & 0xf8) == 0) {
78 }
else if (!GET_BIT(1)) {
79 r = (33 * GET_BIT(8)) + (67 * GET_BIT(4));
80 g = (33 * GET_BIT(7)) + (67 * GET_BIT(3));
81 b = (33 * GET_BIT(6)) + (67 * GET_BIT(2));
82 t = GET_BIT(5) ? 50 : 0;
84 r = (17 * GET_BIT(8)) + (33 * GET_BIT(4)) + (GET_BIT(5) ? 0 : 50);
85 g = (17 * GET_BIT(7)) + (33 * GET_BIT(3)) + (GET_BIT(5) ? 0 : 50);
86 b = (17 * GET_BIT(6)) + (33 * GET_BIT(2)) + (GET_BIT(5) ? 0 : 50);
89 return COLOR(r, g, b, t);
96 DvbImageColorSpace::DvbImageColorSpace() {
97 for (
auto& item : color_map_2_)
99 for (
auto& item : color_map_4_)
101 for (
auto& item : color_map_8_)
105 DvbImageColorSpace::~DvbImageColorSpace() {}
107 RgbaColor DvbImageColorSpace::GetColor(BitDepth bit_depth,
108 uint8_t entry_id)
const {
109 auto color = GetColorRaw(bit_depth, entry_id);
110 if (color != kNoColor)
115 RgbaColor default_color, alt1, alt2;
117 case BitDepth::k2Bit:
118 DCHECK_LT(entry_id, 4u);
119 alt1 = GetColorRaw(BitDepth::k4Bit, bit_depth_2_to_4_[entry_id]);
120 alt2 = GetColorRaw(BitDepth::k8Bit, bit_depth_2_to_8_[entry_id]);
121 default_color = k2BitDefaultColors[entry_id];
123 case BitDepth::k4Bit:
124 DCHECK_LT(entry_id, 16u);
125 alt1 = GetColorRaw(BitDepth::k8Bit, bit_depth_4_to_8_[entry_id]);
126 alt2 = GetColorRaw(BitDepth::k2Bit, k4To2ReductionMap[entry_id]);
127 default_color = k4BitDefaultColors[entry_id];
129 case BitDepth::k8Bit:
131 alt1 = GetColorRaw(BitDepth::k4Bit, entry_id & 0xf);
132 alt2 = GetColorRaw(BitDepth::k2Bit, k4To2ReductionMap[entry_id & 0xf]);
133 default_color = Get8BitDefaultColor(entry_id);
141 if (alt1 != kNoColor)
143 if (alt2 != kNoColor)
145 return default_color;
148 void DvbImageColorSpace::SetColor(BitDepth bit_depth,
151 DCHECK(color != kNoColor);
153 case BitDepth::k2Bit:
154 DCHECK_LT(entry_id, 4u);
155 color_map_2_[entry_id] = color;
157 case BitDepth::k4Bit:
158 DCHECK_LT(entry_id, 16u);
159 color_map_4_[entry_id] = color;
161 case BitDepth::k8Bit:
162 color_map_8_[entry_id] = color;
168 memcpy(bit_depth_2_to_4_, map,
sizeof(bit_depth_2_to_4_));
172 memcpy(bit_depth_2_to_8_, map,
sizeof(bit_depth_2_to_8_));
176 memcpy(bit_depth_4_to_8_, map,
sizeof(bit_depth_4_to_8_));
179 RgbaColor DvbImageColorSpace::GetColorRaw(BitDepth bit_depth,
180 uint8_t entry_id)
const {
182 case BitDepth::k2Bit:
183 return color_map_2_[entry_id];
184 case BitDepth::k4Bit:
185 return color_map_4_[entry_id];
186 case BitDepth::k8Bit:
187 return color_map_8_[entry_id];
193 DvbImageBuilder::DvbImageBuilder(
const DvbImageColorSpace* color_space,
194 const RgbaColor& default_color,
197 : pixels_(new RgbaColor[max_width * max_height]),
198 color_space_(color_space),
201 max_width_(max_width),
202 max_height_(max_height),
204 for (
size_t i = 0; i < static_cast<size_t>(max_width) * max_height; i++)
205 pixels_[i] = default_color;
208 DvbImageBuilder::~DvbImageBuilder() {}
210 bool DvbImageBuilder::AddPixel(BitDepth bit_depth,
213 auto& pos = is_top_rows ? top_pos_ : bottom_pos_;
214 if (pos.x >= max_width_ || pos.y >= max_height_) {
215 LOG(ERROR) <<
"DVB-sub image cannot fit in region/window";
219 pixels_[pos.y * max_width_ + pos.x++] =
220 color_space_->GetColor(bit_depth, byte_code);
226 void DvbImageBuilder::NewRow(
bool is_top_rows) {
227 auto& pos = is_top_rows ? top_pos_ : bottom_pos_;
233 for (
size_t line = 0; line < max_height_ - 1u; line += 2) {
234 std::memcpy(&pixels_[(line + 1) * max_width_], &pixels_[line * max_width_],
237 bottom_pos_ = top_pos_;
238 if (max_height_ % 2 == 0)
246 uint16_t* height)
const {
248 std::tie(min_y, max_y) = std::minmax(top_pos_.y, bottom_pos_.y);
249 if (max_y == 1 || max_y != min_y + 1) {
252 LOG(ERROR) <<
"Incomplete DVB-sub image";
258 *height =
static_cast<uint16_t
>(max_y - 1);
259 *pixels = pixels_.get();
260 if (*height > max_height_) {
261 LOG(ERROR) <<
"DVB-sub image cannot fit in region/window";
All the methods that are virtual are virtual for mocking.