7 #include <packager/media/formats/mp2t/es_parser_teletext.h>
9 #include <packager/media/base/bit_reader.h>
10 #include <packager/media/base/timestamp.h>
11 #include <packager/media/formats/mp2t/es_parser_teletext_tables.h>
12 #include <packager/media/formats/mp2t/mp2t_common.h>
21 constexpr
const char* kRegionTeletextPrefix =
"ttx_";
23 const uint8_t EBU_TELETEXT_WITH_SUBTITLING = 0x03;
24 const int kPayloadSize = 40;
25 const int kNumTriplets = 13;
28 constexpr T bit(T value,
const size_t bit_pos) {
29 return (value >> bit_pos) & 0x1;
32 uint8_t ReadHamming(BitReader& reader) {
34 RCHECK(reader.ReadBits(8, &bits));
35 return TELETEXT_HAMMING_8_4[bits];
38 bool Hamming_24_18(
const uint32_t value, uint32_t& out_result) {
39 uint32_t result = value;
42 for (uint8_t i = 0; i < 23; i++) {
43 test ^= ((result >> i) & 0x01) * (i + 0x21);
45 test ^= ((result >> 0x17) & 0x01) * 0x20;
47 if ((test & 0x1f) != 0x1f) {
48 if ((test & 0x20) == 0x20) {
51 result ^= 1 << (0x1e - test);
54 out_result = (result & 0x000004) >> 2 | (result & 0x000070) >> 3 |
55 (result & 0x007f00) >> 4 | (result & 0x7f0000) >> 5;
59 bool ParseSubtitlingDescriptor(
60 const uint8_t* descriptor,
62 std::unordered_map<uint16_t, std::string>& result) {
63 BitReader reader(descriptor, size);
64 RCHECK(reader.SkipBits(8));
67 RCHECK(reader.ReadBits(8, &data_size));
68 RCHECK(data_size + 2 <= size);
70 for (
size_t i = 0; i < data_size; i += 8) {
72 RCHECK(reader.ReadBits(24, &lang_code));
73 uint8_t ignored_teletext_type;
74 RCHECK(reader.ReadBits(5, &ignored_teletext_type));
75 uint8_t magazine_number;
76 RCHECK(reader.ReadBits(3, &magazine_number));
77 if (magazine_number == 0) {
81 uint8_t page_number_tens;
82 RCHECK(reader.ReadBits(4, &page_number_tens));
83 uint8_t page_number_units;
84 RCHECK(reader.ReadBits(4, &page_number_units));
85 const uint8_t page_number = page_number_tens * 10 + page_number_units;
87 std::string lang(3,
'\0');
88 lang[0] =
static_cast<char>((lang_code >> 16) & 0xff);
89 lang[1] =
static_cast<char>((lang_code >> 8) & 0xff);
90 lang[2] =
static_cast<char>((lang_code >> 0) & 0xff);
92 const uint16_t index = magazine_number * 100 + page_number;
93 result.emplace(index, std::move(lang));
101 EsParserTeletext::EsParserTeletext(
const uint32_t pid,
102 const NewStreamInfoCB& new_stream_info_cb,
103 const EmitTextSampleCB& emit_sample_cb,
104 const uint8_t* descriptor,
105 const size_t descriptor_length)
107 new_stream_info_cb_(new_stream_info_cb),
108 emit_sample_cb_(emit_sample_cb),
114 if (!ParseSubtitlingDescriptor(descriptor, descriptor_length, languages_)) {
115 LOG(ERROR) <<
"Unable to parse teletext_descriptor";
120 bool EsParserTeletext::Parse(
const uint8_t* buf,
126 auto info = std::make_shared<TextStreamInfo>(pid(), kMpeg2Timescale,
127 kInfiniteDuration, kCodecText,
129 for (
const auto& pair : languages_) {
130 info->AddSubStream(pair.first, {pair.second});
133 new_stream_info_cb_(info);
136 return ParseInternal(buf, size, pts);
139 bool EsParserTeletext::Flush() {
140 std::vector<uint16_t> keys;
141 for (
const auto& entry : page_state_) {
142 keys.push_back(entry.first);
145 for (
const auto key : keys) {
146 SendPending(key, last_pts_);
152 void EsParserTeletext::Reset() {
161 bool EsParserTeletext::ParseInternal(
const uint8_t* data,
164 BitReader reader(data, size);
165 RCHECK(reader.SkipBits(8));
166 std::vector<TextRow> rows;
168 while (reader.bits_available()) {
169 uint8_t data_unit_id;
170 RCHECK(reader.ReadBits(8, &data_unit_id));
172 uint8_t data_unit_length;
173 RCHECK(reader.ReadBits(8, &data_unit_length));
175 if (data_unit_id != EBU_TELETEXT_WITH_SUBTITLING) {
176 RCHECK(reader.SkipBytes(data_unit_length));
180 if (data_unit_length != 44) {
182 LOG(ERROR) <<
"Bad Teletext data length";
187 RCHECK(reader.SkipBits(16));
189 uint16_t address_bits;
190 RCHECK(reader.ReadBits(16, &address_bits));
192 uint8_t magazine = bit(address_bits, 14) + 2 * bit(address_bits, 12) +
193 4 * bit(address_bits, 10);
199 const uint8_t packet_nr =
200 (bit(address_bits, 8) + 2 * bit(address_bits, 6) +
201 4 * bit(address_bits, 4) + 8 * bit(address_bits, 2) +
202 16 * bit(address_bits, 0));
203 const uint8_t* data_block = reader.current_byte_ptr();
204 RCHECK(reader.SkipBytes(40));
207 if (ParseDataBlock(pts, data_block, packet_nr, magazine, row)) {
208 rows.emplace_back(std::move(row));
215 const uint16_t index = magazine_ * 100 + page_number_;
216 auto page_state_itr = page_state_.find(index);
217 if (page_state_itr == page_state_.end()) {
218 page_state_.emplace(index, TextBlock{std::move(rows), {}, last_pts_});
221 for (
auto& row : rows) {
222 auto& page_state_lines = page_state_itr->second.rows;
223 page_state_lines.emplace_back(std::move(row));
231 bool EsParserTeletext::ParseDataBlock(
const int64_t pts,
232 const uint8_t* data_block,
233 const uint8_t packet_nr,
234 const uint8_t magazine,
236 if (packet_nr == 0) {
238 BitReader reader(data_block, 32);
240 const uint8_t page_number_units = ReadHamming(reader);
241 const uint8_t page_number_tens = ReadHamming(reader);
242 if (page_number_units == 0xf || page_number_tens == 0xf) {
243 RCHECK(reader.SkipBits(40));
246 const uint8_t page_number = 10 * page_number_tens + page_number_units;
248 const uint16_t index = magazine * 100 + page_number;
249 SendPending(index, pts);
251 page_number_ = page_number;
252 magazine_ = magazine;
254 RCHECK(reader.SkipBits(40));
255 const uint8_t subcode_c11_c14 = ReadHamming(reader);
256 const uint8_t charset_code = subcode_c11_c14 >> 1;
257 if (charset_code != charset_code_) {
258 charset_code_ = charset_code;
264 }
else if (packet_nr == 26) {
265 ParsePacket26(data_block);
268 }
else if (packet_nr > 26) {
272 row = BuildRow(data_block, packet_nr);
276 void EsParserTeletext::UpdateCharset() {
277 memcpy(current_charset_, TELETEXT_CHARSET_G0_LATIN,
sizeof(TELETEXT_CHARSET_G0_LATIN));
278 if (charset_code_ > 7) {
281 const auto teletext_national_subset =
282 static_cast<TELETEXT_NATIONAL_SUBSET
>(charset_code_);
284 switch (teletext_national_subset) {
285 case TELETEXT_NATIONAL_SUBSET::ENGLISH:
286 UpdateNationalSubset(TELETEXT_NATIONAL_SUBSET_ENGLISH);
288 case TELETEXT_NATIONAL_SUBSET::FRENCH:
289 UpdateNationalSubset(TELETEXT_NATIONAL_SUBSET_FRENCH);
291 case TELETEXT_NATIONAL_SUBSET::SWEDISH_FINNISH_HUNGARIAN:
292 UpdateNationalSubset(TELETEXT_NATIONAL_SUBSET_SWEDISH_FINNISH_HUNGARIAN);
294 case TELETEXT_NATIONAL_SUBSET::CZECH_SLOVAK:
295 UpdateNationalSubset(TELETEXT_NATIONAL_SUBSET_CZECH_SLOVAK);
297 case TELETEXT_NATIONAL_SUBSET::GERMAN:
298 UpdateNationalSubset(TELETEXT_NATIONAL_SUBSET_GERMAN);
300 case TELETEXT_NATIONAL_SUBSET::PORTUGUESE_SPANISH:
301 UpdateNationalSubset(TELETEXT_NATIONAL_SUBSET_PORTUGUESE_SPANISH);
303 case TELETEXT_NATIONAL_SUBSET::ITALIAN:
304 UpdateNationalSubset(TELETEXT_NATIONAL_SUBSET_ITALIAN);
306 case TELETEXT_NATIONAL_SUBSET::NONE:
312 void EsParserTeletext::SendPending(
const uint16_t index,
const int64_t pts) {
313 auto page_state_itr = page_state_.find(index);
315 if (page_state_itr == page_state_.end() ||
316 page_state_itr->second.rows.empty()) {
320 const auto& pending_rows = page_state_itr->second.rows;
321 const auto pending_pts = page_state_itr->second.pts;
323 TextSettings text_settings;
324 std::shared_ptr<TextSample> text_sample;
325 std::vector<TextFragment> sub_fragments;
327 if (pending_rows.size() == 1) {
330 const float line_nr = float(pending_rows[0].row_number) / 2.0;
331 text_settings.line = TextNumber(line_nr, TextUnitType::kLines);
332 text_settings.region = kRegionTeletextPrefix + std::to_string(
int(line_nr));
333 text_settings.text_alignment = pending_rows[0].alignment;
334 text_sample = std::make_shared<TextSample>(
335 "", pending_pts, pts, text_settings, pending_rows[0].fragment);
336 text_sample->set_sub_stream_index(index);
337 emit_sample_cb_(text_sample);
338 page_state_.erase(index);
341 int32_t latest_row_nr = -1;
342 bool last_double_height =
false;
343 bool new_sample =
true;
344 for (
const auto& row : pending_rows) {
345 int row_nr = row.row_number;
346 bool double_height = row.double_height;
347 int row_step = last_double_height ? 2 : 1;
348 if (latest_row_nr != -1) {
349 if (row_nr != latest_row_nr + row_step) {
352 std::make_shared<TextSample>(
"", pending_pts, pts, text_settings,
353 TextFragment({}, sub_fragments));
354 text_sample->set_sub_stream_index(index);
355 emit_sample_cb_(text_sample);
359 sub_fragments.push_back(TextFragment({},
true));
360 sub_fragments.push_back(row.fragment);
365 const float line_nr = float(row.row_number) / 2.0;
366 text_settings.line = TextNumber(line_nr, TextUnitType::kLines);
367 text_settings.region =
368 kRegionTeletextPrefix + std::to_string(
int(line_nr));
369 text_settings.text_alignment = row.alignment;
370 sub_fragments.clear();
371 sub_fragments.push_back(row.fragment);
373 last_double_height = double_height;
374 latest_row_nr = row_nr;
378 text_sample = std::make_shared<TextSample>(
379 "", pending_pts, pts, text_settings, TextFragment({}, sub_fragments));
380 text_sample->set_sub_stream_index(index);
381 emit_sample_cb_(text_sample);
383 page_state_.erase(index);
387 EsParserTeletext::TextRow EsParserTeletext::BuildRow(
const uint8_t* data_block,
388 const uint8_t row)
const {
389 std::string next_string;
390 next_string.reserve(kPayloadSize * 2);
392 const uint16_t index = magazine_ * 100 + page_number_;
393 const auto page_state_itr = page_state_.find(index);
395 const std::unordered_map<uint8_t, std::string>* column_replacement_map =
397 if (page_state_itr != page_state_.cend()) {
399 page_state_itr->second.packet_26_replacements.find(row);
400 if (row_itr != page_state_itr->second.packet_26_replacements.cend()) {
401 column_replacement_map = &(row_itr->second);
405 int32_t start_pos = 0;
407 bool double_height =
false;
408 TextFragmentStyle text_style = TextFragmentStyle();
409 text_style.color =
"white";
410 text_style.backgroundColor =
"black";
413 for (
size_t i = 0; i < kPayloadSize; ++i) {
414 if (column_replacement_map) {
415 const auto column_itr = column_replacement_map->find(i);
416 if (column_itr != column_replacement_map->cend()) {
417 next_string.append(column_itr->second);
423 static_cast<char>(TELETEXT_BITREVERSE_8[data_block[i]] & 0x7f);
425 if (next_char < 0x20) {
434 text_style.color =
"red";
437 text_style.color =
"green";
440 text_style.color =
"yellow";
443 text_style.color =
"blue";
446 text_style.color =
"magenta";
449 text_style.color =
"cyan";
452 text_style.color =
"white";
468 double_height =
true;
473 text_style.backgroundColor = text_style.color;
474 text_style.color =
"black";
484 if (start_pos == 0 || end_pos != 0) {
489 next_string.append(
"&");
492 next_string.append(
"<");
495 const std::string replacement(current_charset_[next_char - 0x20]);
496 next_string.append(replacement);
501 end_pos = kPayloadSize - 1;
506 auto left_right_diff = start_pos - (kPayloadSize - 1 - end_pos);
507 TextAlignment alignment;
508 if (left_right_diff > 4) {
509 alignment = TextAlignment::kRight;
510 }
else if (left_right_diff < -4) {
511 alignment = TextAlignment::kLeft;
513 alignment = TextAlignment::kCenter;
515 const auto text_row = TextRow(
516 {alignment, row, double_height, {TextFragment(text_style, next_string)}});
521 void EsParserTeletext::ParsePacket26(
const uint8_t* data_block) {
522 const uint16_t index = magazine_ * 100 + page_number_;
523 auto page_state_itr = page_state_.find(index);
524 if (page_state_itr == page_state_.end()) {
525 page_state_.emplace(index, TextBlock{{}, {}, last_pts_});
527 auto& replacement_map = page_state_[index].packet_26_replacements;
531 std::vector<uint32_t> x26_triplets;
532 x26_triplets.reserve(kNumTriplets);
533 for (uint8_t i = 1; i < kPayloadSize; i += 3) {
534 const uint32_t bytes = (TELETEXT_BITREVERSE_8[data_block[i + 2]] << 16) |
535 (TELETEXT_BITREVERSE_8[data_block[i + 1]] << 8) |
536 TELETEXT_BITREVERSE_8[data_block[i]];
538 if (Hamming_24_18(bytes, triplet)) {
539 x26_triplets.emplace_back(triplet);
543 for (
const auto triplet : x26_triplets) {
544 const uint8_t mode = (triplet & 0x7c0) >> 6;
545 const uint8_t address = triplet & 0x3f;
546 const uint8_t row_address_group = (address >= 0x28) && (address <= 0x3f);
548 if ((mode == 0x4) && (row_address_group == 0x1)) {
549 row = address - 0x28;
555 if (mode >= 0x11 && mode <= 0x1f && row_address_group == 0x1) {
559 const uint8_t data = (triplet & 0x3f800) >> 11;
561 if (mode == 0x0f && row_address_group == 0x0 && data > 0x1f) {
562 SetPacket26ReplacementString(replacement_map, row, address,
563 reinterpret_cast<const char*
>(
564 TELETEXT_CHARSET_G2_LATIN[data - 0x20]));
567 if (mode == 0x10 && row_address_group == 0x0 && data == 0x40) {
568 SetPacket26ReplacementString(replacement_map, row, address,
"@");
571 if (mode < 0x11 || mode > 0x1f || row_address_group != 0x0) {
575 if (data >= 0x41 && data <= 0x5a) {
576 SetPacket26ReplacementString(
577 replacement_map, row, address,
578 reinterpret_cast<const char*
>(
579 TELETEXT_G2_LATIN_ACCENTS[mode - 0x11][data - 0x41]));
581 }
else if (data >= 0x61 && data <= 0x7a) {
582 SetPacket26ReplacementString(
583 replacement_map, row, address,
584 reinterpret_cast<const char*
>(
585 TELETEXT_G2_LATIN_ACCENTS[mode - 0x11][data - 0x47]));
587 }
else if ((data & 0x7f) >= 0x20) {
588 SetPacket26ReplacementString(
589 replacement_map, row, address,
590 reinterpret_cast<const char*
>(
591 TELETEXT_CHARSET_G0_LATIN[(data & 0x7f) - 0x20]));
596 void EsParserTeletext::UpdateNationalSubset(
597 const uint8_t national_subset[13][3]) {
598 for (
size_t i = 0; i < 13; ++i) {
599 const size_t position = TELETEXT_NATIONAL_CHAR_INDEX_G0[i];
600 memcpy(current_charset_[position], national_subset[i], 3);
604 void EsParserTeletext::SetPacket26ReplacementString(
605 RowColReplacementMap& replacement_map,
607 const uint8_t column,
608 std::string&& replacement_string) {
609 auto replacement_map_itr = replacement_map.find(row);
610 if (replacement_map_itr == replacement_map.cend()) {
611 replacement_map.emplace(row, std::unordered_map<uint8_t, std::string>{});
613 auto& column_map = replacement_map[row];
614 column_map.emplace(column, std::move(replacement_string));
All the methods that are virtual are virtual for mocking.