7 #include <packager/media/formats/dvb/dvb_sub_parser.h>
11 #include <absl/log/check.h>
12 #include <absl/log/log.h>
14 #include <packager/media/formats/mp2t/mp2t_common.h>
21 RgbaColor ConvertYuv(uint8_t Y, uint8_t Cr, uint8_t Cb, uint8_t T) {
30 const double y_transform = 255.0 / 219 * (std::max<uint8_t>(Y, 16) - 16);
31 const double cb_transform = 255.0 / 244 * 1.772 * (Cb - 128);
32 const double cr_transform = 255.0 / 244 * 1.402 * (Cr - 128);
33 const double f1 = 0.114 / 0.587;
34 const double f2 = 0.299 / 0.587;
35 color.r =
static_cast<uint8_t
>(y_transform + cr_transform);
37 static_cast<uint8_t
>(y_transform - cb_transform * f1 - cr_transform * f2);
38 color.b =
static_cast<uint8_t
>(y_transform + cb_transform);
39 color.a = Y == 0 ? 0 : (T == 0 ? 255 : 256 - T);
45 DvbSubParser::DvbSubParser() : last_pts_(0), timeout_(0) {}
47 DvbSubParser::~DvbSubParser() {}
49 bool DvbSubParser::Parse(DvbSubSegmentType segment_type,
51 const uint8_t* payload,
53 std::vector<std::shared_ptr<TextSample>>* samples) {
54 switch (segment_type) {
55 case DvbSubSegmentType::kPageComposition:
56 return ParsePageComposition(pts, payload, size, samples);
57 case DvbSubSegmentType::kRegionComposition:
58 return ParseRegionComposition(payload, size);
59 case DvbSubSegmentType::kClutDefinition:
60 return ParseClutDefinition(payload, size);
61 case DvbSubSegmentType::kObjectData:
62 return ParseObjectData(pts, payload, size);
63 case DvbSubSegmentType::kDisplayDefinition:
64 return ParseDisplayDefinition(payload, size);
65 case DvbSubSegmentType::kEndOfDisplay:
70 LOG(WARNING) <<
"Unknown DVB-sub segment_type=0x" << std::hex
71 <<
static_cast<uint32_t
>(segment_type);
76 bool DvbSubParser::Flush(std::vector<std::shared_ptr<TextSample>>* samples) {
77 RCHECK(composer_.GetSamples(last_pts_, last_pts_ + timeout_ * kMpeg2Timescale,
79 composer_.ClearObjects();
83 const DvbImageColorSpace* DvbSubParser::GetColorSpace(uint8_t clut_id) {
84 return composer_.GetColorSpace(clut_id);
87 const DvbImageBuilder* DvbSubParser::GetImageForObject(uint16_t object_id) {
88 return composer_.GetObjectImage(object_id);
91 bool DvbSubParser::ParsePageComposition(
95 std::vector<std::shared_ptr<TextSample>>* samples) {
97 BitReader reader(data, size);
100 RCHECK(reader.ReadBits(8, &timeout_));
101 RCHECK(reader.SkipBits(4));
102 RCHECK(reader.ReadBits(2, &page_state));
103 RCHECK(reader.SkipBits(2));
104 if (page_state == 0x1 || page_state == 0x2) {
107 RCHECK(composer_.GetSamples(last_pts_, pts, samples));
108 composer_.ClearObjects();
112 while (reader.bits_available() > 0u) {
115 RCHECK(reader.ReadBits(8, ®ion_id));
116 RCHECK(reader.SkipBits(8));
117 RCHECK(reader.ReadBits(16, &x));
118 RCHECK(reader.ReadBits(16, &y));
120 RCHECK(composer_.SetRegionPosition(region_id, x, y));
126 bool DvbSubParser::ParseRegionComposition(
const uint8_t* data,
size_t size) {
128 BitReader reader(data, size);
130 uint8_t region_id, clut_id;
131 uint16_t region_width, region_height;
132 bool region_fill_flag;
133 int background_pixel_code;
134 RCHECK(reader.ReadBits(8, ®ion_id));
135 RCHECK(reader.SkipBits(4));
136 RCHECK(reader.ReadBits(1, ®ion_fill_flag));
137 RCHECK(reader.SkipBits(3));
138 RCHECK(reader.ReadBits(16, ®ion_width));
139 RCHECK(reader.ReadBits(16, ®ion_height));
140 RCHECK(reader.SkipBits(3));
141 RCHECK(reader.SkipBits(3));
142 RCHECK(reader.SkipBits(2));
143 RCHECK(reader.ReadBits(8, &clut_id));
144 RCHECK(reader.ReadBits(8, &background_pixel_code));
145 RCHECK(reader.SkipBits(4));
146 RCHECK(reader.SkipBits(2));
147 RCHECK(reader.SkipBits(2));
149 composer_.SetRegionInfo(region_id, clut_id, region_width, region_height));
150 if (!region_fill_flag)
151 background_pixel_code = -1;
153 while (reader.bits_available() > 0) {
154 uint16_t object_id, x, y;
156 RCHECK(reader.ReadBits(16, &object_id));
157 RCHECK(reader.ReadBits(2, &object_type));
158 RCHECK(reader.SkipBits(2));
159 RCHECK(reader.ReadBits(12, &x));
160 RCHECK(reader.SkipBits(4));
161 RCHECK(reader.ReadBits(12, &y));
163 if (object_type == 0x01 || object_type == 0x02) {
164 RCHECK(reader.SkipBits(8));
165 RCHECK(reader.SkipBits(8));
167 RCHECK(composer_.SetObjectInfo(object_id, region_id, x, y,
168 background_pixel_code));
174 bool DvbSubParser::ParseClutDefinition(
const uint8_t* data,
size_t size) {
176 BitReader reader(data, size);
179 RCHECK(reader.ReadBits(8, &clut_id));
180 auto* color_space = composer_.GetColorSpace(clut_id);
181 RCHECK(reader.SkipBits(4));
182 RCHECK(reader.SkipBits(4));
183 while (reader.bits_available() > 0) {
184 uint8_t clut_entry_id;
188 uint8_t full_range_flag;
189 RCHECK(reader.ReadBits(8, &clut_entry_id));
190 RCHECK(reader.ReadBits(1, &has_2_bit));
191 RCHECK(reader.ReadBits(1, &has_4_bit));
192 RCHECK(reader.ReadBits(1, &has_8_bit));
193 RCHECK(reader.SkipBits(4));
194 RCHECK(reader.ReadBits(1, &full_range_flag));
196 if (has_2_bit + has_4_bit + has_8_bit != 1) {
197 LOG(ERROR) <<
"Must specify exactly one bit depth in CLUT definition";
200 const BitDepth bit_depth =
201 has_2_bit ? BitDepth::k2Bit
202 : (has_4_bit ? BitDepth::k4Bit : BitDepth::k8Bit);
204 uint8_t Y, Cr, Cb, T;
205 if (full_range_flag) {
206 RCHECK(reader.ReadBits(8, &Y));
207 RCHECK(reader.ReadBits(8, &Cr));
208 RCHECK(reader.ReadBits(8, &Cb));
209 RCHECK(reader.ReadBits(8, &T));
212 RCHECK(reader.ReadBits(6, &Y));
214 RCHECK(reader.ReadBits(4, &Cr));
216 RCHECK(reader.ReadBits(4, &Cb));
218 RCHECK(reader.ReadBits(2, &T));
221 color_space->SetColor(bit_depth, clut_entry_id, ConvertYuv(Y, Cr, Cb, T));
227 bool DvbSubParser::ParseObjectData(int64_t pts,
231 BitReader reader(data, size);
234 uint8_t object_coding_method;
235 RCHECK(reader.ReadBits(16, &object_id));
236 RCHECK(reader.SkipBits(4));
237 RCHECK(reader.ReadBits(2, &object_coding_method));
238 RCHECK(reader.SkipBits(1));
239 RCHECK(reader.SkipBits(1));
241 auto* image = composer_.GetObjectImage(object_id);
242 auto* color_space = composer_.GetColorSpaceForObject(object_id);
243 if (!image || !color_space)
246 if (object_coding_method == 0) {
247 uint16_t top_field_length;
248 uint16_t bottom_field_length;
249 RCHECK(reader.ReadBits(16, &top_field_length));
250 RCHECK(reader.ReadBits(16, &bottom_field_length));
252 RCHECK(ParsePixelDataSubObject(top_field_length,
true, &reader, color_space,
254 RCHECK(ParsePixelDataSubObject(bottom_field_length,
false, &reader,
255 color_space, image));
258 if (bottom_field_length == 0) {
261 image->MirrorToBottomRows();
264 LOG(ERROR) <<
"Unsupported DVB-sub object coding method: "
265 <<
static_cast<int>(object_coding_method);
271 bool DvbSubParser::ParseDisplayDefinition(
const uint8_t* data,
size_t size) {
273 BitReader reader(data, size);
275 uint16_t width, height;
276 RCHECK(reader.SkipBits(4));
277 RCHECK(reader.SkipBits(1));
278 RCHECK(reader.SkipBits(3));
279 RCHECK(reader.ReadBits(16, &width));
280 RCHECK(reader.ReadBits(16, &height));
282 composer_.SetDisplaySize(width + 1, height + 1);
287 bool DvbSubParser::ParsePixelDataSubObject(
size_t sub_object_length,
290 DvbImageColorSpace* color_space,
291 DvbImageBuilder* image) {
292 const size_t start = reader->bit_position() / 8;
293 while (reader->bit_position() / 8 < start + sub_object_length) {
296 RCHECK(reader->ReadBits(8, &data_type));
300 RCHECK(Parse2BitPixelData(is_top_fields, reader, image));
301 reader->SkipToNextByte();
304 RCHECK(Parse4BitPixelData(is_top_fields, reader, image));
305 reader->SkipToNextByte();
308 RCHECK(Parse8BitPixelData(is_top_fields, reader, image));
311 for (
int i = 0; i < 4; i++) {
312 RCHECK(reader->ReadBits(4, &temp[i]));
314 color_space->Set2To4BitDepthMap(temp);
317 for (
int i = 0; i < 4; i++) {
318 RCHECK(reader->ReadBits(8, &temp[i]));
320 color_space->Set2To8BitDepthMap(temp);
323 for (
int i = 0; i < 16; i++) {
324 RCHECK(reader->ReadBits(8, &temp[i]));
326 color_space->Set4To8BitDepthMap(temp);
329 image->NewRow(is_top_fields);
332 LOG(ERROR) <<
"Unsupported DVB-sub pixel data format: 0x" << std::hex
333 <<
static_cast<int>(data_type);
340 bool DvbSubParser::Parse2BitPixelData(
bool is_top_fields,
342 DvbImageBuilder* image) {
346 RCHECK(reader->ReadBits(2, &peek));
348 RCHECK(image->AddPixel(BitDepth::k2Bit, peek, is_top_fields));
351 RCHECK(reader->ReadBits(1, &switch_1));
353 uint8_t count_minus_3;
354 RCHECK(reader->ReadBits(3, &count_minus_3));
355 RCHECK(reader->ReadBits(2, &peek));
356 for (uint8_t i = 0; i < count_minus_3 + 3; i++)
357 RCHECK(image->AddPixel(BitDepth::k2Bit, peek, is_top_fields));
360 RCHECK(reader->ReadBits(1, &switch_2));
362 RCHECK(image->AddPixel(BitDepth::k2Bit, 0, is_top_fields));
365 RCHECK(reader->ReadBits(2, &switch_3));
368 }
else if (switch_3 == 1) {
369 RCHECK(image->AddPixel(BitDepth::k2Bit, 0, is_top_fields));
370 RCHECK(image->AddPixel(BitDepth::k2Bit, 0, is_top_fields));
371 }
else if (switch_3 == 2) {
372 uint8_t count_minus_12;
373 RCHECK(reader->ReadBits(4, &count_minus_12));
374 RCHECK(reader->ReadBits(2, &peek));
375 for (uint8_t i = 0; i < count_minus_12 + 12; i++)
376 RCHECK(image->AddPixel(BitDepth::k2Bit, peek, is_top_fields));
377 }
else if (switch_3 == 3) {
378 uint8_t count_minus_29;
379 RCHECK(reader->ReadBits(8, &count_minus_29));
380 RCHECK(reader->ReadBits(2, &peek));
381 for (uint8_t i = 0; i < count_minus_29 + 29; i++)
382 RCHECK(image->AddPixel(BitDepth::k2Bit, peek, is_top_fields));
392 bool DvbSubParser::Parse4BitPixelData(
bool is_top_fields,
394 DvbImageBuilder* image) {
396 DCHECK(reader->bits_available() % 8 == 0);
399 RCHECK(reader->ReadBits(4, &peek));
401 RCHECK(image->AddPixel(BitDepth::k4Bit, peek, is_top_fields));
404 RCHECK(reader->ReadBits(1, &switch_1));
406 RCHECK(reader->ReadBits(3, &peek));
408 for (
int i = 0; i < peek + 2; i++)
409 RCHECK(image->AddPixel(BitDepth::k4Bit, 0, is_top_fields));
415 RCHECK(reader->ReadBits(1, &switch_2));
417 RCHECK(reader->ReadBits(2, &peek));
419 RCHECK(reader->ReadBits(4, &code));
420 for (
int i = 0; i < peek + 4; i++)
421 RCHECK(image->AddPixel(BitDepth::k4Bit, code, is_top_fields));
424 RCHECK(reader->ReadBits(2, &switch_3));
426 RCHECK(image->AddPixel(BitDepth::k4Bit, 0, is_top_fields));
427 }
else if (switch_3 == 1) {
428 RCHECK(image->AddPixel(BitDepth::k4Bit, 0, is_top_fields));
429 RCHECK(image->AddPixel(BitDepth::k4Bit, 0, is_top_fields));
430 }
else if (switch_3 == 2) {
431 RCHECK(reader->ReadBits(4, &peek));
433 RCHECK(reader->ReadBits(4, &code));
434 for (
int i = 0; i < peek + 9; i++)
435 RCHECK(image->AddPixel(BitDepth::k4Bit, code, is_top_fields));
438 RCHECK(reader->ReadBits(8, &peek));
440 RCHECK(reader->ReadBits(4, &code));
441 for (
int i = 0; i < peek + 25; i++)
442 RCHECK(image->AddPixel(BitDepth::k4Bit, code, is_top_fields));
451 bool DvbSubParser::Parse8BitPixelData(
bool is_top_fields,
453 DvbImageBuilder* image) {
457 RCHECK(reader->ReadBits(8, &peek));
459 RCHECK(image->AddPixel(BitDepth::k8Bit, peek, is_top_fields));
462 RCHECK(reader->ReadBits(1, &switch_1));
464 RCHECK(reader->ReadBits(7, &peek));
466 for (uint8_t i = 0; i < peek; i++)
467 RCHECK(image->AddPixel(BitDepth::k8Bit, 0, is_top_fields));
473 RCHECK(reader->ReadBits(7, &count));
474 RCHECK(reader->ReadBits(8, &peek));
475 for (uint8_t i = 0; i < count; i++)
476 RCHECK(image->AddPixel(BitDepth::k8Bit, peek, is_top_fields));
All the methods that are virtual are virtual for mocking.