7 #include <packager/media/formats/dvb/subtitle_composer.h>
11 #include <absl/log/check.h>
12 #include <absl/log/log.h>
15 #include <packager/macros/logging.h>
22 const uint16_t kDefaultWidth = 720;
23 const uint16_t kDefaultHeight = 576;
24 const RgbaColor kTransparent{0, 0, 0, 0};
26 struct PngFreeHelper {
27 PngFreeHelper(png_structp* png, png_infop* info) : png(png), info(info) {}
28 ~PngFreeHelper() { png_destroy_write_struct(png, info); }
34 void PngWriteData(png_structp png, png_bytep data, png_size_t length) {
35 auto* output =
reinterpret_cast<std::vector<uint8_t>*
>(png_get_io_ptr(png));
36 output->insert(output->end(), data, data + length);
39 void PngFlushData(png_structp png) {}
41 bool IsTransparent(
const RgbaColor* colors, uint16_t width, uint16_t height) {
42 for (
size_t i = 0; i < static_cast<size_t>(width) * height; i++) {
49 bool GetImageData(
const DvbImageBuilder* image,
50 std::vector<uint8_t>* data,
63 png_create_write_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
64 auto info = png_create_info_struct(png);
65 PngFreeHelper helper(&png, &info);
67 LOG(ERROR) <<
"Error creating libpng struct";
70 if (setjmp(png_jmpbuf(png))) {
72 LOG(ERROR) <<
"Error writing PNG image";
75 png_set_write_fn(png, data, &PngWriteData, &PngFlushData);
77 const RgbaColor* pixels;
78 if (!image->GetPixels(&pixels, width, height))
80 if (IsTransparent(pixels, *width, *height))
82 png_set_IHDR(png, info, *width, *height, 8, PNG_COLOR_TYPE_RGBA,
83 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
84 PNG_FILTER_TYPE_BASE);
85 png_write_info(png, info);
87 const uint8_t* in_data =
reinterpret_cast<const uint8_t*
>(pixels);
88 for (
size_t y = 0; y < *height; y++) {
89 size_t offset = image->max_width() * y *
sizeof(RgbaColor);
90 png_write_row(png, in_data + offset);
92 png_write_end(png,
nullptr);
99 SubtitleComposer::SubtitleComposer()
100 : display_width_(kDefaultWidth), display_height_(kDefaultHeight) {}
102 SubtitleComposer::~SubtitleComposer() {}
104 void SubtitleComposer::SetDisplaySize(uint16_t width, uint16_t height) {
105 display_width_ = width;
106 display_height_ = height;
109 bool SubtitleComposer::SetRegionInfo(uint8_t region_id,
110 uint8_t color_space_id,
113 auto* region = ®ions_[region_id];
114 if (region->x + width > display_width_ ||
115 region->y + height > display_height_) {
116 LOG(ERROR) <<
"DVB-sub region won't fit within display";
119 if (width == 0 || height == 0) {
120 LOG(ERROR) <<
"DVB-sub width/height cannot be 0";
124 region->width = width;
125 region->height = height;
126 region->color_space = &color_spaces_[color_space_id];
130 bool SubtitleComposer::SetRegionPosition(uint8_t region_id,
133 auto* region = ®ions_[region_id];
134 if (x + region->width > display_width_ ||
135 y + region->height > display_height_) {
136 LOG(ERROR) <<
"DVB-sub region won't fit within display";
145 bool SubtitleComposer::SetObjectInfo(uint16_t object_id,
149 int default_color_code) {
150 auto region = regions_.find(region_id);
151 if (region == regions_.end()) {
152 LOG(ERROR) <<
"Unknown DVB-sub region: " << (int)region_id
153 <<
", in object: " << object_id;
156 if (x >= region->second.width || y >= region->second.height) {
157 LOG(ERROR) <<
"DVB-sub object is outside region: " << object_id;
161 auto*
object = &objects_[object_id];
162 object->region = ®ion->second;
163 object->default_color_code = default_color_code;
169 DvbImageColorSpace* SubtitleComposer::GetColorSpace(uint8_t color_space_id) {
170 return &color_spaces_[color_space_id];
173 DvbImageColorSpace* SubtitleComposer::GetColorSpaceForObject(
174 uint16_t object_id) {
175 auto info = objects_.find(object_id);
176 if (info == objects_.end()) {
177 LOG(ERROR) <<
"Unknown DVB-sub object: " << object_id;
180 return info->second.region->color_space;
183 DvbImageBuilder* SubtitleComposer::GetObjectImage(uint16_t object_id) {
184 auto it = images_.find(object_id);
185 if (it == images_.end()) {
186 auto info = objects_.find(object_id);
187 if (info == objects_.end()) {
188 LOG(ERROR) <<
"Unknown DVB-sub object: " << object_id;
192 auto color = info->second.default_color_code < 0
194 : info->second.region->color_space->GetColor(
195 BitDepth::k8Bit, info->second.default_color_code);
197 .emplace(std::piecewise_construct, std::make_tuple(object_id),
199 info->second.region->color_space, color,
200 info->second.region->width - info->second.region->x,
201 info->second.region->height - info->second.region->y))
207 bool SubtitleComposer::GetSamples(
210 std::vector<std::shared_ptr<TextSample>>* samples)
const {
211 for (
const auto& pair : objects_) {
212 auto it = images_.find(pair.first);
213 if (it == images_.end()) {
214 LOG(WARNING) <<
"DVB-sub object " << pair.first
215 <<
" doesn't include object data";
219 uint16_t width, height;
220 std::vector<uint8_t> image_data;
221 if (!GetImageData(&it->second, &image_data, &width, &height))
223 if (image_data.empty()) {
224 VLOG(1) <<
"Skipping transparent object";
227 TextFragment body({}, image_data);
228 DCHECK_LE(width, display_width_);
229 DCHECK_LE(height, display_height_);
231 TextSettings settings;
232 settings.position.emplace(
233 (pair.second.x + pair.second.region->x) * 100.0f / display_width_,
234 TextUnitType::kPercent);
235 settings.line.emplace(
236 (pair.second.y + pair.second.region->y) * 100.0f / display_height_,
237 TextUnitType::kPercent);
238 settings.width.emplace(width * 100.0f / display_width_,
239 TextUnitType::kPercent);
240 settings.height.emplace(height * 100.0f / display_height_,
241 TextUnitType::kPercent);
243 samples->emplace_back(
244 std::make_shared<TextSample>(
"", start, end, settings, body));
250 void SubtitleComposer::ClearObjects() {
All the methods that are virtual are virtual for mocking.