Work on teletext

This commit is contained in:
Nikolaj Olsson 2019-10-31 19:53:32 +01:00
parent 36faf4b8a6
commit f0cd90cd63
5 changed files with 70 additions and 54 deletions

View File

@ -115,11 +115,11 @@ namespace Nikse.SubtitleEdit.Core.TransportStream
Buffer.BlockCopy(buffer, dataIndex - 1, _dataBuffer, 0, _dataBuffer.Length); // why subtract one from dataIndex???
}
public Dictionary<int, string> GetTeletext(int packetId, TeletextRunSettings teletextRunSettings)
public Dictionary<int, Paragraph> GetTeletext(int packetId, TeletextRunSettings teletextRunSettings)
{
if (!IsTeletext)
{
return new Dictionary<int, string>();
return new Dictionary<int, Paragraph>();
}
var pts = PresentationTimestamp.HasValue ? (long)PresentationTimestamp.Value / 90 : 0;
@ -167,7 +167,7 @@ namespace Nikse.SubtitleEdit.Core.TransportStream
pages.Add(888); // default
}
var teletextPages = new Dictionary<int, string>();
var teletextPages = new Dictionary<int, Paragraph>();
Teletext.Fout.Clear();
Teletext._lastTimestamp = 0;
Teletext._globalTimestamp = 0;
@ -180,53 +180,52 @@ namespace Nikse.SubtitleEdit.Core.TransportStream
// pages = new List<int> { pageNumBcd };
var page = pageNum;
int teletextPackageNo = 0;
//foreach (var page in pages)
Teletext.Fout.Clear();
Teletext.config.Page = page; // ((page / 100) << 8) | ((page / 10 % 10) << 4) | (page % 10); ;
Teletext.config.Tid = packetId;
i = 1;
while (i <= _dataBuffer.Length - 6)
{
Teletext.Fout.Clear();
Teletext.config.Page = page; // ((page / 100) << 8) | ((page / 10 % 10) << 4) | (page % 10); ;
Teletext.config.Tid = packetId;
i = 1;
while (i <= _dataBuffer.Length - 6)
{
teletextPackageNo++;
sb.AppendLine("----------------");
sb.AppendLine($"{teletextPackageNo} for packet-id {packetId}");
teletextPackageNo++;
sb.AppendLine("----------------");
sb.AppendLine($"{teletextPackageNo} for packet-id {packetId}");
var dataUnitId = _dataBuffer[i++];
var dataUnitLen = _dataBuffer[i++];
sb.AppendLine($"dataUnitId: {dataUnitId}");
sb.AppendLine($"dataUnitLen: {dataUnitLen}");
if (dataUnitId == (int)Teletext.DataUnitT.DataUnitEbuTeletextNonSubtitle || dataUnitId == (int)Teletext.DataUnitT.DataUnitEbuTeletextSubtitle)
var dataUnitId = _dataBuffer[i++];
var dataUnitLen = _dataBuffer[i++];
sb.AppendLine($"dataUnitId: {dataUnitId}");
sb.AppendLine($"dataUnitLen: {dataUnitLen}");
if (dataUnitId == (int)Teletext.DataUnitT.DataUnitEbuTeletextNonSubtitle || dataUnitId == (int)Teletext.DataUnitT.DataUnitEbuTeletextSubtitle)
{
// teletext payload has always size 44 bytes
if (dataUnitLen == 44)
{
// teletext payload has always size 44 bytes
if (dataUnitLen == 44)
// reverse endianness (via lookup table), ETS 300 706, chapter 7.1
for (var j = 0; j < dataUnitLen; j++)
{
// reverse endianness (via lookup table), ETS 300 706, chapter 7.1
for (var j = 0; j < dataUnitLen; j++)
{
_dataBuffer[i + j] = TeletextHamming.Reverse8[_dataBuffer[i + j]];
}
Teletext.ProcessTelxPacket((Teletext.DataUnitT)dataUnitId, new Teletext.TeletextPacketPayload(_dataBuffer, i), lastTimestamp, teletextRunSettings, sb); //TODO: optimize use databuffer
_dataBuffer[i + j] = TeletextHamming.Reverse8[_dataBuffer[i + j]];
}
Teletext.ProcessTelxPacket((Teletext.DataUnitT)dataUnitId, new Teletext.TeletextPacketPayload(_dataBuffer, i), lastTimestamp, teletextRunSettings, sb); //TODO: optimize use databuffer
}
i += dataUnitLen;
}
var text = Teletext.Fout.ToString().Trim();
if (!string.IsNullOrEmpty(text))
i += dataUnitLen;
}
foreach (var pNo in teletextRunSettings.PageNumberAndParagraph.Keys)
{
if (teletextRunSettings.PageNumberAndParagraph.ContainsKey(pNo) && teletextRunSettings.PageNumberAndParagraph[pNo] != null)
{
var p = teletextRunSettings.PageNumber;
if (teletextPages.ContainsKey(p))
if (teletextPages.ContainsKey(pNo))
{
teletextPages[p] += Environment.NewLine + Environment.NewLine + text;
teletextPages[pNo] = teletextRunSettings.PageNumberAndParagraph[pNo];
}
else
{
teletextPages.Add(p, text);
teletextPages.Add(pNo, teletextRunSettings.PageNumberAndParagraph[pNo]);
}
}
}
teletextRunSettings.PageNumberAndParagraph.Clear();
return teletextPages;
}

View File

@ -334,7 +334,7 @@ namespace Nikse.SubtitleEdit.Core.TransportStream
}
static void ProcessPage(TeletextPage page)
static void ProcessPage(TeletextPage page, TeletextRunSettings teletextRunSettings)
{
//#if DEBUG
// for (int row = 1; row < 25; row++)
@ -361,6 +361,8 @@ namespace Nikse.SubtitleEdit.Core.TransportStream
}
if (pageIsEmpty) return;
var paragraph = new Paragraph();
if (page.ShowTimestamp > page.HideTimestamp)
{
page.HideTimestamp = page.ShowTimestamp;
@ -376,8 +378,14 @@ namespace Nikse.SubtitleEdit.Core.TransportStream
var timeCodeShow = TimestampToSrtTime(page.ShowTimestamp);
var timeCodeHide = TimestampToSrtTime(page.HideTimestamp);
Fout.AppendLine($"{++_framesProduced}{Environment.NewLine}{timeCodeShow} --> {timeCodeHide}");
paragraph.Number = _framesProduced;
paragraph.StartTime = new TimeCode(page.ShowTimestamp);
paragraph.EndTime = new TimeCode(page.HideTimestamp);
}
Fout.Clear(); //nixe - now we use paragraph
// process data
for (var row = 1; row < 25; row++)
{
@ -409,7 +417,7 @@ namespace Nikse.SubtitleEdit.Core.TransportStream
if (colStop > 39) continue;
// ETS 300 706, chapter 12.2: Alpha White ("Set-After") - Start-of-row default condition.
// used for colour changes _before_ start box mark
// used for color changes _before_ start box mark
// white is default as stated in ETS 300 706, chapter 12.2
// black(0), red(1), green(2), yellow(3), blue(4), magenta(5), cyan(6), white(7)
var foregroundColor = 0x7;
@ -492,6 +500,15 @@ namespace Nikse.SubtitleEdit.Core.TransportStream
Fout.Append(config.SeMode ? " " : Environment.NewLine);
}
Fout.AppendLine();
paragraph.Text = Fout.ToString().TrimEnd();
if (!teletextRunSettings.PageNumberAndParagraph.ContainsKey(teletextRunSettings.PageNumber))
{
teletextRunSettings.PageNumberAndParagraph.Add(teletextRunSettings.PageNumber, paragraph);
}
else
{
throw new NotImplementedException();
}
}
public static int GetPageNumber(TeletextPacketPayload packet)
@ -576,11 +593,11 @@ namespace Nikse.SubtitleEdit.Core.TransportStream
CcMap[i] |= (byte)(flagSubtitle << (m - 1));
if (config.Page == 0 && flagSubtitle == (int)BoolT.Yes && i < 0xff)
{
config.Page = (m << 8) | (TeletextHamming.UnHamming84(packet.Data[1]) << 4) | TeletextHamming.UnHamming84(packet.Data[0]);
Console.WriteLine($"- No teletext page specified, first received suitable page is {config.Page}, not guaranteed");
}
//if (config.Page == 0 && flagSubtitle == (int)BoolT.Yes && i < 0xff)
//{
// config.Page = (m << 8) | (TeletextHamming.UnHamming84(packet.Data[1]) << 4) | TeletextHamming.UnHamming84(packet.Data[0]);
// Console.WriteLine($"- No teletext page specified, first received suitable page is {config.Page}, not guaranteed");
//}
// Page number and control bits
var pageNumber = (m << 8) | (TeletextHamming.UnHamming84(packet.Data[1]) << 4) | TeletextHamming.UnHamming84(packet.Data[0]);
@ -605,8 +622,8 @@ namespace Nikse.SubtitleEdit.Core.TransportStream
if (_transmissionMode == TransmissionMode.TransmissionModeParallel && dataUnitId != DataUnitT.DataUnitEbuTeletextSubtitle) return;
if (_receivingData && (
_transmissionMode == TransmissionMode.TransmissionModeSerial && Page(pageNumber) != Page(config.Page) ||
_transmissionMode == TransmissionMode.TransmissionModeParallel && Page(pageNumber) != Page(config.Page) && m == Magazine(config.Page)
_transmissionMode == TransmissionMode.TransmissionModeSerial && Page(pageNumber) != Page(teletextRunSettings.PageNumberBcd) ||
_transmissionMode == TransmissionMode.TransmissionModeParallel && Page(pageNumber) != Page(teletextRunSettings.PageNumberBcd) && m == Magazine(teletextRunSettings.PageNumberBcd)
))
{
_receivingData = false;
@ -624,7 +641,7 @@ namespace Nikse.SubtitleEdit.Core.TransportStream
{
// it would be nice, if subtitle hides on previous video frame, so we contract 40 ms (1 frame @25 fps)
PageBuffer.HideTimestamp = timestamp - 40;
ProcessPage(PageBuffer);
ProcessPage(PageBuffer, teletextRunSettings);
}
PageBuffer.ShowTimestamp = timestamp;

View File

@ -11,5 +11,6 @@ namespace Nikse.SubtitleEdit.Core.TransportStream
public int TransmissionMode { get; set; }
public HashSet<int> PageNumbersInt { get; set; } = new HashSet<int>();
public HashSet<int> PageNumbersBcd { get; set; } = new HashSet<int>();
public Dictionary<int, Paragraph> PageNumberAndParagraph { get; set; } = new Dictionary<int, Paragraph>();
}
}

View File

@ -23,7 +23,7 @@ namespace Nikse.SubtitleEdit.Core.TransportStream
public List<Packet> SubtitlePackets { get; private set; }
private Dictionary<int, List<DvbSubPes>> SubtitlesLookup { get; set; }
private Dictionary<int, List<TransportStreamSubtitle>> DvbSubtitlesLookup { get; set; }
public Dictionary<int, Dictionary<int, StringBuilder>> TeletextSubtitlesLookup { get; set; }
public Dictionary<int, Dictionary<int, List<Paragraph>>> TeletextSubtitlesLookup { get; set; }
public bool IsM2TransportStream { get; private set; }
public ulong FirstVideoPts { get; private set; }
@ -57,7 +57,7 @@ namespace Nikse.SubtitleEdit.Core.TransportStream
var m2TsTimeCodeBuffer = new byte[4];
long position = 0;
SubtitlesLookup = new Dictionary<int, List<DvbSubPes>>();
TeletextSubtitlesLookup = new Dictionary<int, Dictionary<int, StringBuilder>>();
TeletextSubtitlesLookup = new Dictionary<int, Dictionary<int, List<Paragraph>>>();
TeletextRunSettings teletextRunSettings = new TeletextRunSettings();
// check for Topfield .rec file
@ -139,24 +139,23 @@ namespace Nikse.SubtitleEdit.Core.TransportStream
var textDictionary = item.GetTeletext(packet.PacketId, teletextRunSettings);
foreach (var dic in textDictionary)
{
if (dic.Value.Length > 0)
if (!string.IsNullOrEmpty(dic.Value.Text))
{
var text = dic.Value + Environment.NewLine + Environment.NewLine;
if (TeletextSubtitlesLookup.ContainsKey(packet.PacketId))
{
var innerDic = TeletextSubtitlesLookup[packet.PacketId];
if (innerDic.ContainsKey(dic.Key))
{
innerDic[dic.Key].Append(text);
innerDic[dic.Key].Add(dic.Value);
}
else
{
innerDic.Add(dic.Key, new StringBuilder(text));
innerDic.Add(dic.Key, new List<Paragraph> { dic.Value });
}
}
else
{
TeletextSubtitlesLookup.Add(packet.PacketId, new Dictionary<int, StringBuilder> { { dic.Key, new StringBuilder(text) } });
TeletextSubtitlesLookup.Add(packet.PacketId, new Dictionary<int, List<Paragraph>> { { dic.Key, new List<Paragraph> { dic.Value } } });
}
}
}

View File

@ -110,7 +110,7 @@ namespace Nikse.SubtitleEdit.Forms
Text = "Teletext program " + program.Key + " page " + kvp.Key + " in " + language,
IsTeletext = true,
Pid = program.Key,
Srt = kvp.Value.ToString()
Srt = new SubRip().ToText(new Subtitle(kvp.Value), null)
});
}
else
@ -120,7 +120,7 @@ namespace Nikse.SubtitleEdit.Forms
Text = "Teletext program " + program.Key + " page " + kvp.Key,
IsTeletext = true,
Pid = program.Key,
Srt = kvp.Value.ToString()
Srt = new SubRip().ToText(new Subtitle(kvp.Value), null)
});
}
}