7#include <packager/media/formats/dvb/dvb_image.h>
14#include <absl/log/check.h>
15#include <absl/log/log.h>
23constexpr const uint8_t k4To2ReductionMap[] = {
24 0x0, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
25 0x2, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3,
30constexpr const RgbaColor kNoColor{145, 92, 47, 0};
34#define COLOR(r, g, b, t) \
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) \
42constexpr const RgbaColor k2BitDefaultColors[] = {
44 COLOR(100, 100, 100, 0),
49constexpr const RgbaColor k4BitDefaultColors[] = {
53 COLOR(100, 100, 0, 0),
55 COLOR(100, 0, 100, 0),
56 COLOR(0, 100, 100, 0),
57 COLOR(100, 100, 100, 0),
69#define GET_BIT(n) ((entry_id >> (8 - (n))) & 0x1)
71RgbaColor Get8BitDefaultColor(uint8_t entry_id) {
74 return COLOR(0, 0, 0, 100);
75 }
else if ((entry_id & 0xf8) == 0) {
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;
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);
91 return COLOR(r, g, b, t);
98DvbImageColorSpace::DvbImageColorSpace() {
99 for (
auto& item : color_map_2_)
101 for (
auto& item : color_map_4_)
103 for (
auto& item : color_map_8_)
107DvbImageColorSpace::~DvbImageColorSpace() {}
109RgbaColor DvbImageColorSpace::GetColor(BitDepth bit_depth,
110 uint8_t entry_id)
const {
111 auto color = GetColorRaw(bit_depth, entry_id);
112 if (color != kNoColor)
117 RgbaColor default_color, alt1, alt2;
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];
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];
131 case BitDepth::k8Bit:
133 alt1 = GetColorRaw(BitDepth::k4Bit, entry_id & 0xf);
134 alt2 = GetColorRaw(BitDepth::k2Bit, k4To2ReductionMap[entry_id & 0xf]);
135 default_color = Get8BitDefaultColor(entry_id);
143 if (alt1 != kNoColor)
145 if (alt2 != kNoColor)
147 return default_color;
150void DvbImageColorSpace::SetColor(BitDepth bit_depth,
153 DCHECK(color != kNoColor);
155 case BitDepth::k2Bit:
156 DCHECK_LT(entry_id, 4u);
157 color_map_2_[entry_id] = color;
159 case BitDepth::k4Bit:
160 DCHECK_LT(entry_id, 16u);
161 color_map_4_[entry_id] = color;
163 case BitDepth::k8Bit:
164 color_map_8_[entry_id] = color;
170 memcpy(bit_depth_2_to_4_, map,
sizeof(bit_depth_2_to_4_));
174 memcpy(bit_depth_2_to_8_, map,
sizeof(bit_depth_2_to_8_));
178 memcpy(bit_depth_4_to_8_, map,
sizeof(bit_depth_4_to_8_));
181RgbaColor DvbImageColorSpace::GetColorRaw(BitDepth bit_depth,
182 uint8_t entry_id)
const {
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];
195DvbImageBuilder::DvbImageBuilder(
const DvbImageColorSpace* color_space,
196 const RgbaColor& default_color,
199 : pixels_(new RgbaColor[max_width * max_height]),
200 color_space_(color_space),
203 max_width_(max_width),
204 max_height_(max_height),
206 for (
size_t i = 0; i < static_cast<size_t>(max_width) * max_height; i++)
207 pixels_[i] = default_color;
210DvbImageBuilder::~DvbImageBuilder() {}
212bool DvbImageBuilder::AddPixel(BitDepth bit_depth,
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";
221 pixels_[pos.y * max_width_ + pos.x++] =
222 color_space_->GetColor(bit_depth, byte_code);
228void DvbImageBuilder::NewRow(
bool is_top_rows) {
229 auto& pos = is_top_rows ? top_pos_ : bottom_pos_;
235 for (
size_t line = 0; line < max_height_ - 1u; line += 2) {
236 std::memcpy(&pixels_[(line + 1) * max_width_], &pixels_[line * max_width_],
239 bottom_pos_ = top_pos_;
240 if (max_height_ % 2 == 0)
248 uint16_t* height)
const {
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) {
254 LOG(ERROR) <<
"Incomplete DVB-sub image";
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";
All the methods that are virtual are virtual for mocking.