More transport stream

git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@2365 99eadd0c-20b8-1223-b5c4-2a2b2df33de2
This commit is contained in:
niksedk 2014-01-11 20:16:06 +00:00
parent 05bfbcba9c
commit d9123fd6c4
6 changed files with 203 additions and 50 deletions

View File

@ -2243,7 +2243,13 @@ namespace Nikse.SubtitleEdit.Forms
var fi = new FileInfo(fileName);
if ((ext == ".ts" || ext == ".mpeg" || ext == ".mpg") && fi.Length > 10000 && IsTransportStream(fileName)) //TODO: Also check mpg, mpeg - and file header!
if ((ext == ".ts" || ext == ".mpeg" || ext == ".mpg") && fi.Length > 10000 && IsTransportStream(fileName))
{
ImportSubtitleFromTransportStream(fileName);
return;
}
if ((ext == ".m2ts") && fi.Length > 10000 && IsM2TransportStream(fileName))
{
ImportSubtitleFromTransportStream(fileName);
return;
@ -2980,6 +2986,29 @@ namespace Nikse.SubtitleEdit.Forms
}
}
private bool IsM2TransportStream(string fileName)
{
try
{
var buffer = new byte[192 + 192 + 5];
var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) { Position = 0 };
fs.Read(buffer, 0, buffer.Length);
fs.Close();
if (buffer[0] == Nikse.SubtitleEdit.Logic.TransportStream.Packet.SynchronizationByte && buffer[188] == Nikse.SubtitleEdit.Logic.TransportStream.Packet.SynchronizationByte)
return false;
if (buffer[4] == Nikse.SubtitleEdit.Logic.TransportStream.Packet.SynchronizationByte && buffer[192 + 4] == Nikse.SubtitleEdit.Logic.TransportStream.Packet.SynchronizationByte && buffer[192 + 192 + 4] == Nikse.SubtitleEdit.Logic.TransportStream.Packet.SynchronizationByte)
return true;
return false;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return false;
}
}
private void SetUndockedWindowsTitle()
{
string title = Configuration.Settings.Language.General.NoVideoLoaded;

View File

@ -221,7 +221,7 @@ namespace Nikse.SubtitleEdit.Forms
List<XSub> _xSubList;
// DVB (from transport stream)
List<Logic.TransportStream.DvbSubtitle> _dvbSubtitles;
List<Logic.TransportStream.TransportStreamSubtitle> _dvbSubtitles;
Color _dvbSubColor = Color.Transparent;
string _lastLine;
@ -7018,7 +7018,7 @@ namespace Nikse.SubtitleEdit.Forms
}
// TODO: Get language from ts file
internal void Initialize(List<Logic.TransportStream.DvbSubtitle> subtitles, VobSubOcrSettings vobSubOcrSettings, string fileName)
internal void Initialize(List<Logic.TransportStream.TransportStreamSubtitle> subtitles, VobSubOcrSettings vobSubOcrSettings, string fileName)
{
buttonOK.Enabled = false;
buttonCancel.Enabled = false;
@ -7051,6 +7051,7 @@ namespace Nikse.SubtitleEdit.Forms
groupBoxTransportStream.Left = groupBoxImagePalette.Left;
groupBoxTransportStream.Top = groupBoxImagePalette.Top;
groupBoxTransportStream.Visible = true;
checkBoxTransportStreamGetColorAndSplit.Visible = subtitles.Count > 0 && subtitles[0].IsDvbSub;
//SetTesseractLanguageFromLanguageString(languageString);
//_importLanguageString = languageString;
}
@ -7104,14 +7105,14 @@ namespace Nikse.SubtitleEdit.Forms
private void SplitDvbForEachSubImage()
{
var list = new List<Nikse.SubtitleEdit.Logic.TransportStream.DvbSubtitle>();
var list = new List<Nikse.SubtitleEdit.Logic.TransportStream.TransportStreamSubtitle>();
foreach (var dvbSub in _dvbSubtitles)
{
if (dvbSub.ActiveImageIndex == null)
{
for (int i = 0; i < dvbSub.Pes.ObjectDataList.Count; i++)
{
var newDbvSub = new Nikse.SubtitleEdit.Logic.TransportStream.DvbSubtitle();
var newDbvSub = new Nikse.SubtitleEdit.Logic.TransportStream.TransportStreamSubtitle();
newDbvSub.Pes = dvbSub.Pes;
newDbvSub.ActiveImageIndex = i;
newDbvSub.StartMilliseconds = dvbSub.StartMilliseconds;

View File

@ -1,33 +0,0 @@
using System.Drawing;
namespace Nikse.SubtitleEdit.Logic.TransportStream
{
public class DvbSubtitle
{
public ulong StartMilliseconds { get; set; }
public ulong EndMilliseconds { get; set; }
public DvbSubPes Pes { get; set; }
public int? ActiveImageIndex { get; set; }
/// <summary>
/// Gets full image if 'ActiveImageIndex' not set, otherwise only gets image by index
/// </summary>
/// <returns></returns>
public Bitmap GetActiveImage()
{
if (ActiveImageIndex.HasValue && ActiveImageIndex.HasValue && ActiveImageIndex >= 0 && ActiveImageIndex < Pes.ObjectDataList.Count)
return (Bitmap)Pes.GetImage(Pes.ObjectDataList[ActiveImageIndex.Value]).Clone();
return Pes.GetImageFull();
}
public int NumberOfImages
{
get
{
return Pes.ObjectDataList.Count;
}
}
}
}

View File

@ -1,7 +1,9 @@
using Nikse.SubtitleEdit.Logic.VobSub;
using Nikse.SubtitleEdit.Logic.BluRaySup;
using Nikse.SubtitleEdit.Logic.VobSub;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Nikse.SubtitleEdit.Logic.TransportStream
{
@ -18,7 +20,8 @@ namespace Nikse.SubtitleEdit.Logic.TransportStream
public List<Packet> SubtitlePackets { get; private set; }
public List<Packet> ProgramAssociationTables { get; private set; }
private Dictionary<int, List<DvbSubPes>> SubtitlesLookup { get; set; }
private Dictionary<int, List<DvbSubtitle>> DvbSubtitlesLookup { get; set; }
private Dictionary<int, List<TransportStreamSubtitle>> DvbSubtitlesLookup { get; set; }
public bool IsM2TransportStream { get; private set; }
public void ParseTsFile(string fileName)
{
@ -33,6 +36,7 @@ namespace Nikse.SubtitleEdit.Logic.TransportStream
/// <param name="ms">Input stream</param>
public void ParseTsFile(Stream ms)
{
IsM2TransportStream = false;
NumberOfNullPackets = 0;
TotalNumberOfPackets = 0;
TotalNumberOfPrivateStream1 = 0;
@ -41,12 +45,23 @@ namespace Nikse.SubtitleEdit.Logic.TransportStream
SubtitlePackets = new List<Packet>();
ProgramAssociationTables = new List<Packet>();
ms.Position = 0;
int packetLength = DeterminePacketLength(ms);
int packetLength = 188;
DetectFormat(ms);
var packetBuffer = new byte[packetLength];
var m2tsTimeCodeBuffer = new byte[4];
long position = 0;
while (position < ms.Length)
{
ms.Seek(position, SeekOrigin.Begin);
if (IsM2TransportStream)
{
ms.Read(m2tsTimeCodeBuffer, 0, m2tsTimeCodeBuffer.Length);
//var tc = Helper.GetEndianWord(m2tsTimeCodeBuffer, 0);
//System.Windows.Forms.MessageBox.Show("Test" + tc);
position += m2tsTimeCodeBuffer.Length;
}
ms.Read(packetBuffer, 0, packetLength);
byte syncByte = packetBuffer[0];
if (syncByte == Packet.SynchronizationByte)
@ -131,6 +146,55 @@ namespace Nikse.SubtitleEdit.Logic.TransportStream
}
}
if (IsM2TransportStream)
{
DvbSubtitlesLookup = new Dictionary<int, List<TransportStreamSubtitle>>();
foreach (int pid in SubtitlePacketIds)
{
var bdMs = new MemoryStream();
var list = MakeSubtitlePesPackets(pid);
//var startMsList = new List<ulong>();
//var endMsList = new List<ulong>();
foreach (var item in list)
{
bdMs.Write(item.DataBuffer, 0, item.DataBuffer.Length);
//if (item.DataBuffer[0] == 0x16)
//{
// startMsList.Add(item.PresentationTimeStampToMilliseconds() / 90);
//}
//else if (item.DataBuffer[0] == 0x80)
//{
// endMsList.Add(item.PresentationTimeStampToMilliseconds() / 90);
//}
}
bdMs.Position = 0;
var sb = new StringBuilder();
var bdList = BluRaySupParser.ParseBluRaySup(bdMs, sb, true);
if (bdList.Count > 0)
{
var subList = new List<TransportStreamSubtitle>();
for (int k=0; k < bdList.Count; k++)
{
var bdSup = bdList[k];
ulong startMs = 0;
//if (k < startMsList.Count)
// startMs = startMsList[k];
ulong endMs = 0;
//if (k < endMsList.Count)
// endMs = endMsList[k];
subList.Add(new TransportStreamSubtitle(bdSup, startMs, endMs));
}
DvbSubtitlesLookup.Add(pid, subList);
}
}
SubtitlePacketIds.Clear();
foreach (int key in DvbSubtitlesLookup.Keys)
SubtitlePacketIds.Add(key);
SubtitlePacketIds.Sort();
return;
}
// check for SubPictureStreamId = 32
SubtitlesLookup = new Dictionary<int,List<DvbSubPes>>();
foreach (int pid in SubtitlePacketIds)
@ -157,18 +221,17 @@ namespace Nikse.SubtitleEdit.Logic.TransportStream
// Merge packets and set start/end time
DvbSubtitlesLookup = new Dictionary<int, List<DvbSubtitle>>();
DvbSubtitlesLookup = new Dictionary<int, List<TransportStreamSubtitle>>();
foreach (int pid in SubtitlePacketIds)
{
var subtitles = new List<DvbSubtitle>();
var subtitles = new List<TransportStreamSubtitle>();
var list = GetSubtitlePesPackets(pid);
for (int i=0; i<list.Count; i++)
{
var pes = list[i];
pes.ParseSegments();
if (pes.ObjectDataList.Count > 0)
if (IsM2TransportStream)
{
var sub = new DvbSubtitle();
var sub = new TransportStreamSubtitle();
sub.StartMilliseconds = pes.PresentationTimeStampToMilliseconds();
sub.Pes = pes;
if (i + 1 < list.Count && list[i + 1].PresentationTimeStampToMilliseconds() > 25)
@ -177,6 +240,21 @@ namespace Nikse.SubtitleEdit.Logic.TransportStream
sub.EndMilliseconds = sub.StartMilliseconds + 3500;
subtitles.Add(sub);
}
else
{
pes.ParseSegments();
if (pes.ObjectDataList.Count > 0)
{
var sub = new TransportStreamSubtitle();
sub.StartMilliseconds = pes.PresentationTimeStampToMilliseconds();
sub.Pes = pes;
if (i + 1 < list.Count && list[i + 1].PresentationTimeStampToMilliseconds() > 25)
sub.EndMilliseconds = list[i + 1].PresentationTimeStampToMilliseconds() - 25;
if (sub.EndMilliseconds < 1)
sub.EndMilliseconds = sub.StartMilliseconds + 3500;
subtitles.Add(sub);
}
}
}
DvbSubtitlesLookup.Add(pid, subtitles);
}
@ -189,7 +267,7 @@ namespace Nikse.SubtitleEdit.Logic.TransportStream
SubtitlePacketIds.Sort();
}
public List<DvbSubtitle> GetDvbSubtitles(int packetId)
public List<TransportStreamSubtitle> GetDvbSubtitles(int packetId)
{
if (DvbSubtitlesLookup.ContainsKey(packetId))
return DvbSubtitlesLookup[packetId];
@ -257,9 +335,20 @@ namespace Nikse.SubtitleEdit.Logic.TransportStream
list.Add(pes);
}
private int DeterminePacketLength(Stream ms)
private void DetectFormat(Stream ms)
{
return 188;
if (ms.Length > 192 + 192 + 5)
{
ms.Seek(0, SeekOrigin.Begin);
var buffer = new byte[192 + 192 + 5];
ms.Read(buffer, 0, buffer.Length);
if (buffer[0] == Packet.SynchronizationByte && buffer[188] == Packet.SynchronizationByte)
return;
if (buffer[4] == Packet.SynchronizationByte && buffer[192 + 4] == Packet.SynchronizationByte && buffer[192 + 192 + 4] == Packet.SynchronizationByte)
{
IsM2TransportStream = true;
}
}
}
}

View File

@ -0,0 +1,67 @@
using System.Drawing;
namespace Nikse.SubtitleEdit.Logic.TransportStream
{
public class TransportStreamSubtitle
{
public ulong StartMilliseconds { get; set; }
public ulong EndMilliseconds { get; set; }
public DvbSubPes Pes { get; set; }
private BluRaySup.BluRaySupParser.PcsData _bdSup;
public int? ActiveImageIndex { get; set; }
public bool IsBluRaySup
{
get
{
return _bdSup != null;
}
}
public bool IsDvbSub
{
get
{
return Pes != null;
}
}
public TransportStreamSubtitle(BluRaySup.BluRaySupParser.PcsData bdSup, ulong startMilliseconds, ulong endMilliseconds)
{
_bdSup = bdSup;
StartMilliseconds = startMilliseconds;
EndMilliseconds = endMilliseconds;
}
public TransportStreamSubtitle()
{
}
/// <summary>
/// Gets full image if 'ActiveImageIndex' not set, otherwise only gets image by index
/// </summary>
/// <returns></returns>
public Bitmap GetActiveImage()
{
if (_bdSup != null)
return _bdSup.GetBitmap();
if (ActiveImageIndex.HasValue && ActiveImageIndex.HasValue && ActiveImageIndex >= 0 && ActiveImageIndex < Pes.ObjectDataList.Count)
return (Bitmap)Pes.GetImage(Pes.ObjectDataList[ActiveImageIndex.Value]).Clone();
return Pes.GetImageFull();
}
public int NumberOfImages
{
get
{
if (Pes != null)
return Pes.ObjectDataList.Count;
else
return _bdSup.BitmapObjects.Count;
}
}
}
}

View File

@ -1057,7 +1057,7 @@
<Compile Include="Logic\TransportStream\ClutDefinitionSegment.cs" />
<Compile Include="Logic\TransportStream\DisplayDefinitionSegment.cs" />
<Compile Include="Logic\TransportStream\DvbSubPes.cs" />
<Compile Include="Logic\TransportStream\DvbSubtitle.cs" />
<Compile Include="Logic\TransportStream\TransportStreamSubtitle.cs" />
<Compile Include="Logic\TransportStream\Helper.cs" />
<Compile Include="Logic\TransportStream\HummingDecoder.cs" />
<Compile Include="Logic\TransportStream\ObjectDataSegment.cs" />