7#include <packager/media/formats/dvb/dvb_sub_parser.h>
16#include <absl/log/check.h>
17#include <absl/log/log.h>
19#include <packager/media/base/bit_reader.h>
20#include <packager/media/base/text_sample.h>
21#include <packager/media/formats/dvb/dvb_image.h>
22#include <packager/media/formats/mp2t/mp2t_common.h>
29RgbaColor ConvertYuv(uint8_t Y, uint8_t Cr, uint8_t Cb, uint8_t T) {
38 const double y_transform = 255.0 / 219 * (std::max<uint8_t>(Y, 16) - 16);
39 const double cb_transform = 255.0 / 244 * 1.772 * (Cb - 128);
40 const double cr_transform = 255.0 / 244 * 1.402 * (Cr - 128);
41 const double f1 = 0.114 / 0.587;
42 const double f2 = 0.299 / 0.587;
43 color.r =
static_cast<uint8_t
>(y_transform + cr_transform);
45 static_cast<uint8_t
>(y_transform - cb_transform * f1 - cr_transform * f2);
46 color.b =
static_cast<uint8_t
>(y_transform + cb_transform);
47 color.a = Y == 0 ? 0 : (T == 0 ? 255 : 256 - T);
53DvbSubParser::DvbSubParser() : last_pts_(0), timeout_(0) {}
55DvbSubParser::~DvbSubParser() {}
57bool DvbSubParser::Parse(DvbSubSegmentType segment_type,
59 const uint8_t* payload,
61 std::vector<std::shared_ptr<TextSample>>* samples) {
62 switch (segment_type) {
63 case DvbSubSegmentType::kPageComposition:
64 return ParsePageComposition(pts, payload, size, samples);
65 case DvbSubSegmentType::kRegionComposition:
66 return ParseRegionComposition(payload, size);
67 case DvbSubSegmentType::kClutDefinition:
68 return ParseClutDefinition(payload, size);
69 case DvbSubSegmentType::kObjectData:
70 return ParseObjectData(pts, payload, size);
71 case DvbSubSegmentType::kDisplayDefinition:
72 return ParseDisplayDefinition(payload, size);
73 case DvbSubSegmentType::kEndOfDisplay:
78 LOG(WARNING) <<
"Unknown DVB-sub segment_type=0x" << std::hex
79 <<
static_cast<uint32_t
>(segment_type);
84bool DvbSubParser::Flush(std::vector<std::shared_ptr<TextSample>>* samples) {
85 RCHECK(composer_.GetSamples(last_pts_, last_pts_ + timeout_ * kMpeg2Timescale,
87 composer_.ClearObjects();
91const DvbImageColorSpace* DvbSubParser::GetColorSpace(uint8_t clut_id) {
92 return composer_.GetColorSpace(clut_id);
95const DvbImageBuilder* DvbSubParser::GetImageForObject(uint16_t object_id) {
96 return composer_.GetObjectImage(object_id);
99bool DvbSubParser::ParsePageComposition(
103 std::vector<std::shared_ptr<TextSample>>* samples) {
105 BitReader reader(data, size);
108 RCHECK(reader.ReadBits(8, &timeout_));
109 RCHECK(reader.SkipBits(4));
110 RCHECK(reader.ReadBits(2, &page_state));
111 RCHECK(reader.SkipBits(2));
112 if (page_state == 0x1 || page_state == 0x2) {
115 RCHECK(composer_.GetSamples(last_pts_, pts, samples));
116 composer_.ClearObjects();
120 while (reader.bits_available() > 0u) {
123 RCHECK(reader.ReadBits(8, ®ion_id));
124 RCHECK(reader.SkipBits(8));
125 RCHECK(reader.ReadBits(16, &x));
126 RCHECK(reader.ReadBits(16, &y));
128 RCHECK(composer_.SetRegionPosition(region_id, x, y));
134bool DvbSubParser::ParseRegionComposition(
const uint8_t* data,
size_t size) {
136 BitReader reader(data, size);
138 uint8_t region_id, clut_id;
139 uint16_t region_width, region_height;
140 bool region_fill_flag;
141 int background_pixel_code;
142 RCHECK(reader.ReadBits(8, ®ion_id));
143 RCHECK(reader.SkipBits(4));
144 RCHECK(reader.ReadBits(1, ®ion_fill_flag));
145 RCHECK(reader.SkipBits(3));
146 RCHECK(reader.ReadBits(16, ®ion_width));
147 RCHECK(reader.ReadBits(16, ®ion_height));
148 RCHECK(reader.SkipBits(3));
149 RCHECK(reader.SkipBits(3));
150 RCHECK(reader.SkipBits(2));
151 RCHECK(reader.ReadBits(8, &clut_id));
152 RCHECK(reader.ReadBits(8, &background_pixel_code));
153 RCHECK(reader.SkipBits(4));
154 RCHECK(reader.SkipBits(2));
155 RCHECK(reader.SkipBits(2));
157 composer_.SetRegionInfo(region_id, clut_id, region_width, region_height));
158 if (!region_fill_flag)
159 background_pixel_code = -1;
161 while (reader.bits_available() > 0) {
162 uint16_t object_id, x, y;
164 RCHECK(reader.ReadBits(16, &object_id));
165 RCHECK(reader.ReadBits(2, &object_type));
166 RCHECK(reader.SkipBits(2));
167 RCHECK(reader.ReadBits(12, &x));
168 RCHECK(reader.SkipBits(4));
169 RCHECK(reader.ReadBits(12, &y));
171 if (object_type == 0x01 || object_type == 0x02) {
172 RCHECK(reader.SkipBits(8));
173 RCHECK(reader.SkipBits(8));
175 RCHECK(composer_.SetObjectInfo(object_id, region_id, x, y,
176 background_pixel_code));
182bool DvbSubParser::ParseClutDefinition(
const uint8_t* data,
size_t size) {
184 BitReader reader(data, size);
187 RCHECK(reader.ReadBits(8, &clut_id));
188 auto* color_space = composer_.GetColorSpace(clut_id);
189 RCHECK(reader.SkipBits(4));
190 RCHECK(reader.SkipBits(4));
191 while (reader.bits_available() > 0) {
192 uint8_t clut_entry_id;
196 uint8_t full_range_flag;
197 RCHECK(reader.ReadBits(8, &clut_entry_id));
198 RCHECK(reader.ReadBits(1, &has_2_bit));
199 RCHECK(reader.ReadBits(1, &has_4_bit));
200 RCHECK(reader.ReadBits(1, &has_8_bit));
201 RCHECK(reader.SkipBits(4));
202 RCHECK(reader.ReadBits(1, &full_range_flag));
204 if (has_2_bit + has_4_bit + has_8_bit != 1) {
205 LOG(ERROR) <<
"Must specify exactly one bit depth in CLUT definition";
208 const BitDepth bit_depth =
209 has_2_bit ? BitDepth::k2Bit
210 : (has_4_bit ? BitDepth::k4Bit : BitDepth::k8Bit);
212 uint8_t Y, Cr, Cb, T;
213 if (full_range_flag) {
214 RCHECK(reader.ReadBits(8, &Y));
215 RCHECK(reader.ReadBits(8, &Cr));
216 RCHECK(reader.ReadBits(8, &Cb));
217 RCHECK(reader.ReadBits(8, &T));
220 RCHECK(reader.ReadBits(6, &Y));
222 RCHECK(reader.ReadBits(4, &Cr));
224 RCHECK(reader.ReadBits(4, &Cb));
226 RCHECK(reader.ReadBits(2, &T));
229 color_space->SetColor(bit_depth, clut_entry_id, ConvertYuv(Y, Cr, Cb, T));
235bool DvbSubParser::ParseObjectData(int64_t pts,
239 BitReader reader(data, size);
242 uint8_t object_coding_method;
243 RCHECK(reader.ReadBits(16, &object_id));
244 RCHECK(reader.SkipBits(4));
245 RCHECK(reader.ReadBits(2, &object_coding_method));
246 RCHECK(reader.SkipBits(1));
247 RCHECK(reader.SkipBits(1));
249 auto* image = composer_.GetObjectImage(object_id);
250 auto* color_space = composer_.GetColorSpaceForObject(object_id);
251 if (!image || !color_space)
254 if (object_coding_method == 0) {
255 uint16_t top_field_length;
256 uint16_t bottom_field_length;
257 RCHECK(reader.ReadBits(16, &top_field_length));
258 RCHECK(reader.ReadBits(16, &bottom_field_length));
260 RCHECK(ParsePixelDataSubObject(top_field_length,
true, &reader, color_space,
262 RCHECK(ParsePixelDataSubObject(bottom_field_length,
false, &reader,
263 color_space, image));
266 if (bottom_field_length == 0) {
269 image->MirrorToBottomRows();
272 LOG(ERROR) <<
"Unsupported DVB-sub object coding method: "
273 <<
static_cast<int>(object_coding_method);
279bool DvbSubParser::ParseDisplayDefinition(
const uint8_t* data,
size_t size) {
281 BitReader reader(data, size);
283 uint16_t width, height;
284 RCHECK(reader.SkipBits(4));
285 RCHECK(reader.SkipBits(1));
286 RCHECK(reader.SkipBits(3));
287 RCHECK(reader.ReadBits(16, &width));
288 RCHECK(reader.ReadBits(16, &height));
290 composer_.SetDisplaySize(width + 1, height + 1);
295bool DvbSubParser::ParsePixelDataSubObject(
size_t sub_object_length,
298 DvbImageColorSpace* color_space,
299 DvbImageBuilder* image) {
300 const size_t start = reader->bit_position() / 8;
301 while (reader->bit_position() / 8 < start + sub_object_length) {
304 RCHECK(reader->ReadBits(8, &data_type));
308 RCHECK(Parse2BitPixelData(is_top_fields, reader, image));
309 reader->SkipToNextByte();
312 RCHECK(Parse4BitPixelData(is_top_fields, reader, image));
313 reader->SkipToNextByte();
316 RCHECK(Parse8BitPixelData(is_top_fields, reader, image));
319 for (
int i = 0; i < 4; i++) {
320 RCHECK(reader->ReadBits(4, &temp[i]));
322 color_space->Set2To4BitDepthMap(temp);
325 for (
int i = 0; i < 4; i++) {
326 RCHECK(reader->ReadBits(8, &temp[i]));
328 color_space->Set2To8BitDepthMap(temp);
331 for (
int i = 0; i < 16; i++) {
332 RCHECK(reader->ReadBits(8, &temp[i]));
334 color_space->Set4To8BitDepthMap(temp);
337 image->NewRow(is_top_fields);
340 LOG(ERROR) <<
"Unsupported DVB-sub pixel data format: 0x" << std::hex
341 <<
static_cast<int>(data_type);
348bool DvbSubParser::Parse2BitPixelData(
bool is_top_fields,
350 DvbImageBuilder* image) {
354 RCHECK(reader->ReadBits(2, &peek));
356 RCHECK(image->AddPixel(BitDepth::k2Bit, peek, is_top_fields));
359 RCHECK(reader->ReadBits(1, &switch_1));
361 uint8_t count_minus_3;
362 RCHECK(reader->ReadBits(3, &count_minus_3));
363 RCHECK(reader->ReadBits(2, &peek));
364 for (uint8_t i = 0; i < count_minus_3 + 3; i++)
365 RCHECK(image->AddPixel(BitDepth::k2Bit, peek, is_top_fields));
368 RCHECK(reader->ReadBits(1, &switch_2));
370 RCHECK(image->AddPixel(BitDepth::k2Bit, 0, is_top_fields));
373 RCHECK(reader->ReadBits(2, &switch_3));
376 }
else if (switch_3 == 1) {
377 RCHECK(image->AddPixel(BitDepth::k2Bit, 0, is_top_fields));
378 RCHECK(image->AddPixel(BitDepth::k2Bit, 0, is_top_fields));
379 }
else if (switch_3 == 2) {
380 uint8_t count_minus_12;
381 RCHECK(reader->ReadBits(4, &count_minus_12));
382 RCHECK(reader->ReadBits(2, &peek));
383 for (uint8_t i = 0; i < count_minus_12 + 12; i++)
384 RCHECK(image->AddPixel(BitDepth::k2Bit, peek, is_top_fields));
385 }
else if (switch_3 == 3) {
386 uint8_t count_minus_29;
387 RCHECK(reader->ReadBits(8, &count_minus_29));
388 RCHECK(reader->ReadBits(2, &peek));
389 for (uint8_t i = 0; i < count_minus_29 + 29; i++)
390 RCHECK(image->AddPixel(BitDepth::k2Bit, peek, is_top_fields));
400bool DvbSubParser::Parse4BitPixelData(
bool is_top_fields,
402 DvbImageBuilder* image) {
404 DCHECK(reader->bits_available() % 8 == 0);
407 RCHECK(reader->ReadBits(4, &peek));
409 RCHECK(image->AddPixel(BitDepth::k4Bit, peek, is_top_fields));
412 RCHECK(reader->ReadBits(1, &switch_1));
414 RCHECK(reader->ReadBits(3, &peek));
416 for (
int i = 0; i < peek + 2; i++)
417 RCHECK(image->AddPixel(BitDepth::k4Bit, 0, is_top_fields));
423 RCHECK(reader->ReadBits(1, &switch_2));
425 RCHECK(reader->ReadBits(2, &peek));
427 RCHECK(reader->ReadBits(4, &code));
428 for (
int i = 0; i < peek + 4; i++)
429 RCHECK(image->AddPixel(BitDepth::k4Bit, code, is_top_fields));
432 RCHECK(reader->ReadBits(2, &switch_3));
434 RCHECK(image->AddPixel(BitDepth::k4Bit, 0, is_top_fields));
435 }
else if (switch_3 == 1) {
436 RCHECK(image->AddPixel(BitDepth::k4Bit, 0, is_top_fields));
437 RCHECK(image->AddPixel(BitDepth::k4Bit, 0, is_top_fields));
438 }
else if (switch_3 == 2) {
439 RCHECK(reader->ReadBits(4, &peek));
441 RCHECK(reader->ReadBits(4, &code));
442 for (
int i = 0; i < peek + 9; i++)
443 RCHECK(image->AddPixel(BitDepth::k4Bit, code, is_top_fields));
446 RCHECK(reader->ReadBits(8, &peek));
448 RCHECK(reader->ReadBits(4, &code));
449 for (
int i = 0; i < peek + 25; i++)
450 RCHECK(image->AddPixel(BitDepth::k4Bit, code, is_top_fields));
459bool DvbSubParser::Parse8BitPixelData(
bool is_top_fields,
461 DvbImageBuilder* image) {
465 RCHECK(reader->ReadBits(8, &peek));
467 RCHECK(image->AddPixel(BitDepth::k8Bit, peek, is_top_fields));
470 RCHECK(reader->ReadBits(1, &switch_1));
472 RCHECK(reader->ReadBits(7, &peek));
474 for (uint8_t i = 0; i < peek; i++)
475 RCHECK(image->AddPixel(BitDepth::k8Bit, 0, is_top_fields));
481 RCHECK(reader->ReadBits(7, &count));
482 RCHECK(reader->ReadBits(8, &peek));
483 for (uint8_t i = 0; i < count; i++)
484 RCHECK(image->AddPixel(BitDepth::k8Bit, peek, is_top_fields));
All the methods that are virtual are virtual for mocking.