"Fossies" - the Fresh Open Source Software Archive

Member "MediaInfo_CLI_GNU_FromSource/MediaInfoLib/Source/MediaInfo/Multiple/File_MpegPs.cpp" (10 Sep 2019, 212194 Bytes) of package /linux/misc/MediaInfo_CLI_19.09_GNU_FromSource.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "File_MpegPs.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 19.07_vs_19.09.

    1 /*  Copyright (c) MediaArea.net SARL. All Rights Reserved.
    2  *
    3  *  Use of this source code is governed by a BSD-style license that can
    4  *  be found in the License.html file in the root of the source tree.
    5  */
    6 
    7 //---------------------------------------------------------------------------
    8 // Pre-compilation
    9 #include "MediaInfo/PreComp.h"
   10 #ifdef __BORLANDC__
   11     #pragma hdrstop
   12 #endif
   13 //---------------------------------------------------------------------------
   14 
   15 //---------------------------------------------------------------------------
   16 #include "MediaInfo/Setup.h"
   17 //---------------------------------------------------------------------------
   18 
   19 //---------------------------------------------------------------------------
   20 #if defined(MEDIAINFO_MPEGPS_YES)
   21 //---------------------------------------------------------------------------
   22 
   23 //---------------------------------------------------------------------------
   24 #include "MediaInfo/Multiple/File_MpegPs.h"
   25 #include "MediaInfo/Multiple/File_Mpeg_Psi.h"
   26 #if defined(MEDIAINFO_AVC_YES)
   27     #include "MediaInfo/Video/File_Avc.h"
   28 #endif
   29 #if defined(MEDIAINFO_HEVC_YES)
   30     #include "MediaInfo/Video/File_Hevc.h"
   31 #endif
   32 #if defined(MEDIAINFO_MPEG4V_YES)
   33     #include "MediaInfo/Video/File_Mpeg4v.h"
   34 #endif
   35 #if defined(MEDIAINFO_MPEGV_YES)
   36     #include "MediaInfo/Video/File_Mpegv.h"
   37 #endif
   38 #if defined(MEDIAINFO_VC1_YES)
   39     #include "MediaInfo/Video/File_Vc1.h"
   40 #endif
   41 #if defined(MEDIAINFO_AVSV_YES)
   42     #include "MediaInfo/Video/File_AvsV.h"
   43 #endif
   44 #if defined(MEDIAINFO_DIRAC_YES)
   45     #include "MediaInfo/Video/File_Dirac.h"
   46 #endif
   47 #if defined(MEDIAINFO_AAC_YES)
   48     #include "MediaInfo/Audio/File_Aac.h"
   49 #endif
   50 #if defined(MEDIAINFO_AC3_YES)
   51     #include "MediaInfo/Audio/File_Ac3.h"
   52 #endif
   53 #if defined(MEDIAINFO_AC4_YES)
   54     #include "MediaInfo/Audio/File_Ac4.h"
   55 #endif
   56 #if defined(MEDIAINFO_DTS_YES)
   57     #include "MediaInfo/Audio/File_Dts.h"
   58 #endif
   59 #if defined(MEDIAINFO_MPEGA_YES)
   60     #include "MediaInfo/Audio/File_Mpega.h"
   61 #endif
   62 #if defined(MEDIAINFO_PCM_YES)
   63     #include "MediaInfo/Audio/File_Pcm_M2ts.h"
   64 #endif
   65 #if defined(MEDIAINFO_PCM_YES)
   66     #include "MediaInfo/Audio/File_Pcm_Vob.h"
   67 #endif
   68 #if defined(MEDIAINFO_SMPTEST0302_YES)
   69     #include "MediaInfo/Audio/File_SmpteSt0302.h"
   70 #endif
   71 #if defined(MEDIAINFO_PS2A_YES)
   72     #include "MediaInfo/Audio/File_Ps2Audio.h"
   73 #endif
   74 #if defined(MEDIAINFO_RLE_YES)
   75     #include "MediaInfo/Image/File_Rle.h"
   76 #endif
   77 #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
   78     #include "MediaInfo/Text/File_AribStdB24B37.h"
   79 #endif
   80 #if defined(MEDIAINFO_DVBSUBTITLE_YES)
   81     #include "MediaInfo/Text/File_DvbSubtitle.h"
   82 #endif
   83 #if defined(MEDIAINFO_PGS_YES)
   84     #include "MediaInfo/Text/File_Pgs.h"
   85 #endif
   86 #if defined(MEDIAINFO_TELETEXT_YES)
   87     #include "MediaInfo/Text/File_Teletext.h"
   88 #endif
   89 #include "MediaInfo/MediaInfo_Config_MediaInfo.h"
   90 #include "MediaInfo/File_Unknown.h"
   91 #include <ZenLib/Utils.h>
   92 #include <algorithm>
   93 #if MEDIAINFO_EVENTS
   94     #include "MediaInfo/MediaInfo_Events_Internal.h"
   95 #endif //MEDIAINFO_EVENTS
   96 #if MEDIAINFO_IBIUSAGE && MEDIAINFO_SEEK
   97     #include "MediaInfo/Multiple/File_Ibi.h"
   98 #endif //MEDIAINFO_IBIUSAGE && MEDIAINFO_SEEK
   99 using namespace ZenLib;
  100 using namespace std;
  101 //---------------------------------------------------------------------------
  102 
  103 namespace MediaInfoLib
  104 {
  105 
  106 //***************************************************************************
  107 // Constants
  108 //***************************************************************************
  109 
  110 //---------------------------------------------------------------------------
  111 static const char* MpegPs_System_Fixed[]=
  112 {
  113     "CBR",
  114     "VBR",
  115 };
  116 
  117 //---------------------------------------------------------------------------
  118 static const char* MpegPs_stream_id(int8u Element_Name)
  119 {
  120          if (Element_Name>=0xC0
  121           && Element_Name<=0xDF) return "MPEG Audio";
  122     else if (Element_Name>=0xE0
  123           && Element_Name<=0xEF) return "MPEG Video";
  124     else if (Element_Name==0xB8) return "For all MPEG Audio streams";
  125     else if (Element_Name==0xB9) return "For all MPEG Video streams";
  126     else if (Element_Name==0xBD) return "Private 1";
  127     else if (Element_Name==0xBF) return "Private 2";
  128     else if (Element_Name==0xFD) return "Private HD";
  129     else                         return "";
  130 }
  131 
  132 //---------------------------------------------------------------------------
  133 static const char* MpegPs_trick_mode_control_values[8]=
  134 {
  135     "Fast forward",
  136     "Slow motion",
  137     "Freeze frame",
  138     "Fast reverse",
  139     "Slow reverse",
  140     "Reserved",
  141     "Reserved",
  142     "Reserved"
  143 };
  144 
  145 //---------------------------------------------------------------------------
  146 static const char* MpegPs_stream_id_extension(int8u stream_id_extension)
  147 {
  148     switch (stream_id_extension)
  149     {
  150         case 0x00 : return "IPMP Control Information Streams"; //ISO/IEC 13818-11
  151         case 0x01 : return "IPMP Streams";                     //ISO/IEC 13818-11
  152         default :
  153                  if (stream_id_extension>=0x02
  154                   && stream_id_extension<=0x11) return "ISO/IEC 14496-17 text Streams";
  155             else if (stream_id_extension>=0x12
  156                   && stream_id_extension<=0x21) return "ISO/IEC 23002-3 auxiliary video data Streams";
  157             else if (stream_id_extension>=0x55
  158                   && stream_id_extension<=0x5F) return "VC-1";
  159             else if (stream_id_extension>=0x60
  160                   && stream_id_extension<=0x6F) return "Dirac";
  161             else if (stream_id_extension==0x71) return "Audio";
  162             else if (stream_id_extension==0x72) return "Audio Ext";
  163             else if (stream_id_extension==0x76) return "Audio";
  164             else if (stream_id_extension>=0x75
  165                   && stream_id_extension<=0x7F) return "VC-1";
  166             else                                return "";
  167     }
  168 }
  169 
  170 //---------------------------------------------------------------------------
  171 extern const char* Mpeg_Psi_stream_type_Format(int8u stream_type, int32u format_identifier);
  172 extern const char* Mpeg_Psi_stream_type_Codec(int8u stream_type, int32u format_identifier);
  173 extern stream_t    Mpeg_Psi_stream_type_StreamKind(int32u stream_type, int32u format_identifier);
  174 extern const char* Mpeg_Psi_stream_type_Info(int8u stream_type, int32u format_identifier);
  175 
  176 //***************************************************************************
  177 // Constructor/Destructor
  178 //***************************************************************************
  179 
  180 //---------------------------------------------------------------------------
  181 File_MpegPs::File_MpegPs()
  182 :File__Analyze()
  183 {
  184     //Configuration
  185     ParserName="MpegPs";
  186     #if MEDIAINFO_EVENTS
  187         ParserIDs[0]=MediaInfo_Parser_MpegPs;
  188         StreamIDs_Width[0]=2;
  189     #endif //MEDIAINFO_EVENTS
  190     #if MEDIAINFO_DEMUX
  191         Demux_Level=2; //Container
  192     #endif //MEDIAINFO_DEMUX
  193     #if MEDIAINFO_TRACE
  194         Trace_Layers_Update(0); //Container1
  195     #endif //MEDIAINFO_TRACE
  196     MustSynchronize=true;
  197     Buffer_TotalBytes_FirstSynched_Max=64*1024;
  198     Buffer_TotalBytes_Fill_Max=(int64u)-1; //Disabling this feature for this format, this is done in the parser
  199     Trusted_Multiplier=2;
  200 
  201     //In
  202     FromTS=false;
  203     FromTS_stream_type=0x00; //No info
  204     FromTS_program_format_identifier=0x00000000; //No info
  205     FromTS_format_identifier=0x00000000; //No info
  206     FromTS_descriptor_tag=0x00; //No info
  207     MPEG_Version=0; //No info
  208     Searching_TimeStamp_Start=true;
  209     #ifdef MEDIAINFO_MPEG4_YES
  210         ParserFromTs=NULL;
  211         SLConfig=NULL;
  212     #endif
  213     #if MEDIAINFO_DEMUX
  214         SubStream_Demux=NULL;
  215         Demux_StreamIsBeingParsed_type=(int8u)-1;
  216     #endif //MEDIAINFO_DEMUX
  217     #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
  218         FromAribStdB24B37=false;
  219     #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
  220 
  221     //Out
  222     HasTimeStamps=false;
  223 
  224     //Temp
  225     SizeToAnalyze=8*1024*1024;
  226     video_stream_Unlimited=false;
  227     Buffer_DataSizeToParse=0;
  228     #if MEDIAINFO_SEEK
  229         Seek_Value=(int64u)-1;
  230         Seek_ID=(int64u)-1;
  231         Duration_Detected=false;
  232     #endif //MEDIAINFO_SEEK
  233 
  234     //StreamOrder
  235     StreamOrder_CountOfPrivateStreams_Minus1=0;
  236 
  237     //From packets
  238     program_mux_rate=(int32u)-1;
  239 
  240     BookMark_Set(); //for stream parsing in phase 2
  241 }
  242 
  243 //---------------------------------------------------------------------------
  244 File_MpegPs::~File_MpegPs()
  245 {
  246     #if MEDIAINFO_DEMUX
  247         if (FromTS_stream_type==0x20) //If SubStream, this object owns the demux handler
  248             delete SubStream_Demux; //SubStream_Demux=NULL;
  249     #endif //MEDIAINFO_DEMUX
  250     #ifdef MEDIAINFO_MPEG4_YES
  251         delete ParserFromTs; //ParserFromTs=NULL;
  252         delete SLConfig; //SLConfig=NULL;
  253     #endif
  254 }
  255 
  256 //***************************************************************************
  257 // Streams management
  258 //***************************************************************************
  259 
  260 //---------------------------------------------------------------------------
  261 void File_MpegPs::Streams_Fill()
  262 {
  263     //For each Streams
  264     for (size_t StreamID=0; StreamID<0x100; StreamID++)
  265         Streams_Fill_PerStream(StreamID, Streams[StreamID], KindOfStream_Main);
  266 
  267     //For each private Streams
  268     for (size_t StreamID=0; StreamID<0x100; StreamID++)
  269         Streams_Fill_PerStream(StreamID, Streams_Private1[StreamID], KindOfStream_Private);
  270 
  271     //For each extension Streams
  272     for (size_t StreamID=0; StreamID<0x100; StreamID++)
  273     {
  274         Streams_Fill_PerStream(StreamID, Streams_Extension[StreamID], KindOfStream_Extension);
  275 
  276         //Special cases
  277         if ((StreamID==0x71 || StreamID==0x76) && !Streams_Extension[StreamID].Parsers.empty() && Streams_Extension[0x72].StreamRegistration_Count) //DTS-HD and TrueHD
  278         {
  279             Fill(Stream_Audio, StreamPos_Last, Audio_MuxingMode, "Stream extension");
  280             if (!IsSub)
  281                 Fill(Stream_Audio, StreamPos_Last, Audio_MuxingMode_MoreInfo, "HD part is in stream extension 114 (0x72)");
  282         }
  283     }
  284 
  285     //Tags in MPEG Video
  286     if (Count_Get(Stream_Video)>0)
  287         Fill(Stream_General, 0, General_Encoded_Library, Retrieve(Stream_Video, 0, Video_Encoded_Library));
  288 
  289     //Special case: Video PTS
  290     if (video_stream_PTS.size()>=2+4*2+1*2 && Retrieve(Stream_Video, 0, Video_FrameRate).To_float64()>30.000) //TODO: Parser all kind of files
  291     {
  292         sort(video_stream_PTS.begin(), video_stream_PTS.end());
  293         video_stream_PTS.erase(video_stream_PTS.begin(), video_stream_PTS.begin()+4); //Removing first frames, they may lack of B/P frames
  294         video_stream_PTS.resize(video_stream_PTS.size()-4); //Removing last frames, they may lack of B/P frames
  295 
  296         //Trying to detect container FPS
  297         std::vector<int64u> video_stream_PTS_Between;
  298         for (size_t Pos=1; Pos<video_stream_PTS.size(); Pos++)
  299             video_stream_PTS_Between.push_back(video_stream_PTS[Pos]-video_stream_PTS[Pos-1]);
  300         std::sort(video_stream_PTS_Between.begin(), video_stream_PTS_Between.end());
  301         video_stream_PTS_Between.erase(video_stream_PTS_Between.begin(), video_stream_PTS_Between.begin()+1); //Removing first timec, they may be wrong value due to missing frame
  302         video_stream_PTS_Between.resize(video_stream_PTS_Between.size()-1); //Removing last frames, they may be wrong value due to missing frame
  303         if (video_stream_PTS_Between[0]*0.9<video_stream_PTS_Between[video_stream_PTS_Between.size()-1]
  304          && video_stream_PTS_Between[0]*1.1>video_stream_PTS_Between[video_stream_PTS_Between.size()-1])
  305         {
  306             float64 Time=(float)(video_stream_PTS[video_stream_PTS.size()-1]-video_stream_PTS[0])/(video_stream_PTS.size()-1)/90;
  307             if (Time)
  308             {
  309                 float64 FrameRate_Container=1000/Time;
  310                 if (Retrieve(Stream_Video, 0, Video_ScanType)==__T("Interlaced"))
  311                     FrameRate_Container/=2; //PTS is per field
  312                 float64 FrameRate_Original=Retrieve(Stream_Video, 0, Video_FrameRate).To_float64();
  313                 if (!(FrameRate_Original>=FrameRate_Container*0.9 && FrameRate_Original<=FrameRate_Container*1.1)
  314                  && !(FrameRate_Container>=FrameRate_Original*0.9 && FrameRate_Container<=FrameRate_Original*1.1))
  315                 {
  316                     Clear(Stream_Video, 0, Video_FrameRate); //Or automatic filling thinks current FrameRate is the container FrameRate (usaly Conatainer FrameRate is filled first, not here)
  317                     Fill(Stream_Video, 0, Video_FrameRate, FrameRate_Container, 3, true);
  318                     if (FrameRate_Original)
  319                         Fill(Stream_Video, 0, Video_FrameRate_Original, FrameRate_Original);
  320                 }
  321             }
  322         }
  323     }
  324 
  325     if (Count_Get(Stream_Video)==1 && Retrieve(Stream_Video, 0, Video_Format_Version)==__T("Version 1"))
  326         Fill(Stream_General, 0, General_InternetMediaType, "video/mpeg", Unlimited, true, true);
  327 }
  328 
  329 //---------------------------------------------------------------------------
  330 void File_MpegPs::Streams_Fill_PerStream(size_t StreamID, ps_stream &Temp, kindofstream KindOfStream)
  331 {
  332     size_t Counts[Stream_Max];
  333     for (size_t StreamKind=Stream_General+1; StreamKind<Stream_Max; StreamKind++)
  334         Counts[StreamKind]=Count_Get((stream_t)StreamKind);
  335 
  336     //By the parser
  337     StreamKind_Last=Stream_Max;
  338     size_t Count=0;
  339     if (!Temp.Parsers.empty() && Temp.Parsers[0] && Temp.Parsers[0]->Status[IsAccepted])
  340     {
  341         Fill(Temp.Parsers[0]);
  342 
  343         if (Temp.Parsers[0]->Count_Get(Stream_Video) && Temp.Parsers[0]->Count_Get(Stream_Text))
  344         {
  345             //Special case: Video and Text are together
  346             Stream_Prepare(Stream_Video);
  347             Count=Merge(*Temp.Parsers[0], Stream_Video, 0, StreamPos_Last);
  348         }
  349         else
  350             Count=Merge(*Temp.Parsers[0]);
  351 
  352         Ztring LawRating=Temp.Parsers[0]->Retrieve(Stream_General, 0, General_LawRating);
  353         if (!LawRating.empty())
  354             Fill(Stream_General, 0, General_LawRating, LawRating, true);
  355         Ztring Title=Temp.Parsers[0]->Retrieve(Stream_General, 0, General_Title);
  356         if (!Title.empty() && Retrieve(Stream_General, 0, General_Title).empty())
  357             Fill(Stream_General, 0, General_Title, Title);
  358     }
  359 
  360     //By the TS stream_type
  361     if (StreamKind_Last==Stream_Max)
  362     {
  363         //Disabling stream_private_1 if needed (will be done by Streams_Private1 object)
  364         if (Temp.stream_type!=0 && (StreamID==0xBD /*|| StreamID==0xBF*/))
  365         {
  366             bool StreamIsDetected=false;
  367             for (size_t Pos=0; Pos<Streams_Private1.size(); Pos++)
  368                 if (!Streams_Private1[Pos].Parsers.empty() && Streams_Private1[Pos].Parsers[0])
  369                     StreamIsDetected=true;
  370             if (StreamIsDetected)
  371                 Temp.stream_type=0;
  372         }
  373 
  374         if (Temp.stream_type!=0)
  375         {
  376             Stream_Prepare(Mpeg_Psi_stream_type_StreamKind(Temp.stream_type, 0x00000000));
  377             Count=1;
  378         }
  379     }
  380 
  381     //By StreamRegistration_Count
  382     if (StreamKind_Last==Stream_Max)
  383     {
  384         if (Temp.StreamRegistration_Count>16)
  385         {
  386             if (StreamID>=0xC0 && StreamID<=0xDF)
  387             {
  388                 Stream_Prepare(Stream_Audio);
  389                 Count=1;
  390             }
  391             if (StreamID>=0xE0 && StreamID<=0xEF)
  392             {
  393                 Stream_Prepare(Stream_Video);
  394                 Count=1;
  395             }
  396         }
  397     }
  398     Temp.Count=Count;
  399 
  400     #ifdef MEDIAINFO_MPEG4_YES
  401         if (StreamKind_Last==Stream_Audio && SLConfig)
  402             Fill(Stream_Audio, StreamPos_Last, Audio_MuxingMode, "SL");
  403     #endif //MEDIAINFO_MPEG4_YES
  404 
  405     if (Counts[StreamKind_Last]+Count==Count_Get(StreamKind_Last)) //Old method
  406         Streams_Fill_PerStream_PerKind(StreamID, Temp, KindOfStream, Count);
  407     else
  408     {
  409         //Several kinds of streams at the same time. TODO: more generic code
  410         stream_t StreamKind_Last_Before=StreamKind_Last;
  411         for (size_t StreamKind=Stream_General+1; StreamKind<Stream_Max; StreamKind++)
  412         {
  413             StreamKind_Last=(stream_t)StreamKind;
  414             Streams_Fill_PerStream_PerKind(StreamID, Temp, KindOfStream, Count_Get((stream_t)StreamKind)-Counts[StreamKind]);
  415         }
  416         StreamKind_Last=StreamKind_Last_Before;
  417     }
  418 }
  419 
  420 //---------------------------------------------------------------------------
  421 void File_MpegPs::Streams_Fill_PerStream_PerKind(size_t StreamID, ps_stream &Temp, kindofstream KindOfStream, size_t Count)
  422 {
  423 
  424     //More info
  425     for (size_t StreamPos=Count_Get(StreamKind_Last)-Count; StreamPos<Count_Get(StreamKind_Last); StreamPos++)
  426     {
  427         ///Saving StreamKind and Stream_Pos
  428         Temp.StreamKind=StreamKind_Last;
  429         Temp.StreamPos=Count_Get(StreamKind_Last)-Count;
  430 
  431         //Common
  432         if (KindOfStream==KindOfStream_Main)
  433         {
  434             Ztring ID; ID.From_Number(StreamID);
  435             Ztring ID_String = Get_Hex_ID(StreamID);
  436             if (!Retrieve(StreamKind_Last, StreamPos, General_ID).empty())
  437             {
  438                 Fill(StreamKind_Last, StreamPos, General_ID, StreamID);
  439                 Ztring ID_String = Get_Hex_ID(StreamID);
  440                 Fill(StreamKind_Last, StreamPos, General_ID_String, ID_String, true); //TODO: merge with Decimal_Hexa in file_MpegTs
  441             }
  442             Fill(StreamKind_Last, StreamPos, General_ID, ID, true);
  443             Fill(StreamKind_Last, StreamPos, General_ID_String, ID_String, true); //TODO: merge with Decimal_Hexa in file_MpegTs
  444         }
  445         else if (KindOfStream==KindOfStream_Private)
  446         {
  447             Ztring ID=__T("189");
  448             if (StreamID)
  449                 ID+=__T("-")+Ztring::ToZtring(StreamID);
  450             if (!Temp.Parsers[0]->Retrieve(StreamKind_Last, StreamPos, General_ID).empty())
  451                 ID+=__T("-")+Temp.Parsers[0]->Retrieve(StreamKind_Last, StreamPos, General_ID);
  452             Fill(StreamKind_Last, StreamPos, General_ID, ID, true);
  453             Ztring ID_String=__T("189 (0xBD)");
  454             if (StreamID)
  455                 ID_String+=__T("-")+ Get_Hex_ID(StreamID);
  456             if (!Temp.Parsers[0]->Retrieve(StreamKind_Last, StreamPos, General_ID_String).empty())
  457                 ID_String+=__T("-")+Temp.Parsers[0]->Retrieve(StreamKind_Last, StreamPos, General_ID_String);
  458             else if (!Temp.Parsers[0]->Retrieve(StreamKind_Last, StreamPos, General_ID).empty())
  459                 ID_String+=__T("-")+Temp.Parsers[0]->Retrieve(StreamKind_Last, StreamPos, General_ID);
  460             Fill(StreamKind_Last, StreamPos, General_ID_String, ID_String, true); //TODO: merge with Decimal_Hexa in file_MpegTs
  461             if (StreamID)
  462                 Fill(StreamKind_Last, StreamPos, "MuxingMode", "DVD-Video", Unlimited, true, true);
  463         }
  464         else if (KindOfStream==KindOfStream_Extension)
  465         {
  466             if (!IsSub) //Not providing the StreamID if it is e.g. MPEG-TS, useless
  467             {
  468             Ztring ID=__T("253");
  469             if (StreamID)
  470                 ID+=__T("-")+Ztring::ToZtring(StreamID);
  471             Fill(StreamKind_Last, StreamPos, General_ID, ID, true);
  472             Ztring ID_String=__T("253 (0xFD)");
  473             if (StreamID)
  474                 ID_String+=__T("-")+ Get_Hex_ID(StreamID);
  475             Fill(StreamKind_Last, StreamPos, General_ID_String, ID_String, true); //TODO: merge with Decimal_Hexa in file_MpegTs
  476             }
  477         }
  478 
  479         if (Retrieve(StreamKind_Last, StreamPos, Fill_Parameter(StreamKind_Last, Generic_Format)).empty() && Temp.stream_type!=0)
  480             Fill(StreamKind_Last, StreamPos, Fill_Parameter(StreamKind_Last, Generic_Format), Mpeg_Psi_stream_type_Format(Temp.stream_type, 0x0000));
  481         if (Retrieve(StreamKind_Last, StreamPos, Fill_Parameter(StreamKind_Last, Generic_Codec)).empty() && Temp.stream_type!=0)
  482             Fill(StreamKind_Last, StreamPos, Fill_Parameter(StreamKind_Last, Generic_Codec), Mpeg_Psi_stream_type_Codec(Temp.stream_type, 0x0000));
  483 
  484         if (Temp.TimeStamp_Start.PTS.TimeStamp!=(int64u)-1)
  485         {
  486             Fill(StreamKind_Last, StreamPos, Fill_Parameter(StreamKind_Last, Generic_Delay_Original), Retrieve(StreamKind_Last, StreamPos, Fill_Parameter(StreamKind_Last, Generic_Delay)), true);
  487             Clear(StreamKind_Last, StreamPos, Fill_Parameter(StreamKind_Last, Generic_Delay));
  488             Fill(StreamKind_Last, StreamPos, Fill_Parameter(StreamKind_Last, Generic_Delay_Original_Source), Retrieve(StreamKind_Last, StreamPos, Fill_Parameter(StreamKind_Last, Generic_Delay_Source)), true);
  489             Clear(StreamKind_Last, StreamPos, Fill_Parameter(StreamKind_Last, Generic_Delay_Source));
  490             Fill(StreamKind_Last, StreamPos, Fill_Parameter(StreamKind_Last, Generic_Delay_Original_Settings), Retrieve(StreamKind_Last, StreamPos, Fill_Parameter(StreamKind_Last, Generic_Delay_Settings)), true);
  491             Clear(StreamKind_Last, StreamPos, Fill_Parameter(StreamKind_Last, Generic_Delay_Settings));
  492 
  493             Fill(StreamKind_Last, StreamPos, Fill_Parameter(StreamKind_Last, Generic_Delay), ((float64)Temp.TimeStamp_Start.PTS.TimeStamp)/90, 6, true);
  494             Fill(StreamKind_Last, StreamPos, Fill_Parameter(StreamKind_Last, Generic_Delay_Source), "Container");
  495         }
  496 
  497         Bitrate_Calc();
  498     }
  499 }
  500 
  501 //---------------------------------------------------------------------------
  502 void File_MpegPs::Streams_Update()
  503 {
  504     //For each Streams
  505     for (size_t StreamID=0; StreamID<0x100; StreamID++)
  506         Streams[StreamID].Streams_Update();
  507 
  508     //For each private Streams
  509     for (size_t StreamID=0; StreamID<0x100; StreamID++)
  510             Streams_Private1[StreamID].Streams_Update();
  511 
  512     //For each extension Streams
  513     for (size_t StreamID=0; StreamID<0x100; StreamID++)
  514         Streams_Extension[StreamID].Streams_Update();
  515 }
  516 
  517 //---------------------------------------------------------------------------
  518 void File_MpegPs::Streams_Finish()
  519 {
  520     if (Streams.empty())
  521         return; //Parsing already done. ToDo: real time
  522 
  523     FrameInfo.PTS=0; //Will be used for BitRate calculation
  524     FrameInfo.DTS=0; //Will be used for Duration calculation
  525 
  526     //For each Streams
  527     for (size_t StreamID=0; StreamID<0x100; StreamID++)
  528         Streams_Finish_PerStream(StreamID, Streams[StreamID], KindOfStream_Main);
  529 
  530     //For each private Streams
  531     StreamOrder_CountOfPrivateStreams_Temp=0;
  532     for (size_t StreamID=0; StreamID<0x100; StreamID++)
  533         Streams_Finish_PerStream(StreamID, Streams_Private1[StreamID], KindOfStream_Private);
  534 
  535     //For each extesnion Streams
  536     for (size_t StreamID=0; StreamID<0x100; StreamID++)
  537         Streams_Finish_PerStream(StreamID, Streams_Extension[StreamID], KindOfStream_Extension);
  538 
  539     //Bitrate coherancy
  540     if (!IsSub && FrameInfo.PTS>0 && FrameInfo.PTS!=(int64u)-1 && FrameInfo.DTS!=0 && File_Size!=(int64u)-1)
  541     {
  542         int64u BitRate_FromDuration=File_Size*8000*90/FrameInfo.DTS;
  543         int64u BitRate_FromBitRates=FrameInfo.PTS;
  544 
  545         if (BitRate_FromDuration>=BitRate_FromBitRates*3
  546          || BitRate_FromDuration<=BitRate_FromBitRates/20)
  547         {
  548             //Clearing durations
  549             for (size_t StreamKind=0; StreamKind<=Stream_Text; StreamKind++)
  550                 for (size_t StreamPos=0; StreamPos<Count_Get((stream_t)StreamKind); StreamPos++)
  551                     Clear((stream_t)StreamKind, StreamPos, (stream_t)Fill_Parameter((stream_t)StreamKind, Generic_Duration));
  552             if (Count_Get(Stream_Video)==1)
  553                 Clear(Stream_Video, 0, Video_Duration);
  554         }
  555     }
  556 
  557     #if MEDIAINFO_IBIUSAGE
  558         if (!IsSub && Config_Ibi_Create)
  559         {
  560             for (ibi::streams::iterator IbiStream_Temp=Ibi.Streams.begin(); IbiStream_Temp!=Ibi.Streams.end(); ++IbiStream_Temp)
  561             {
  562                 if (IbiStream_Temp->second && IbiStream_Temp->second->DtsFrequencyNumerator==1000000000 && IbiStream_Temp->second->DtsFrequencyDenominator==1)
  563                 {
  564                     bool IsOk=true;
  565                     for (size_t Pos=0; Pos<IbiStream_Temp->second->Infos.size(); Pos++)
  566                         if (!IbiStream_Temp->second->Infos[Pos].IsContinuous && Pos+1!=IbiStream_Temp->second->Infos.size())
  567                             IsOk=false;
  568                     if (IsOk) //Only is all items are continuous (partial IBI not yet supported)
  569                     {
  570                         IbiStream_Temp->second->DtsFrequencyNumerator=90000;
  571                         for (size_t Pos=0; Pos<IbiStream_Temp->second->Infos.size(); Pos++)
  572                         {
  573                             int64u Temp=IbiStream_Temp->second->Infos[Pos].Dts*90/1000000;
  574                             IbiStream_Temp->second->Infos[Pos].Dts=Temp;
  575                         }
  576                     }
  577                 }
  578             }
  579         }
  580     #endif //MEDIAINFO_IBIUSAGE
  581 }
  582 
  583 //---------------------------------------------------------------------------
  584 void File_MpegPs::Streams_Finish_PerStream(size_t StreamID, ps_stream &Temp, kindofstream KindOfStream)
  585 {
  586     //By the parser
  587     if (Temp.StreamKind==Stream_Max && !Temp.Parsers.empty() && Temp.Parsers[0])
  588         Streams_Fill_PerStream(StreamID, Temp, KindOfStream);
  589 
  590     //Init
  591     if (Temp.StreamKind==Stream_Max)
  592         return;
  593     StreamKind_Last=Temp.StreamKind;
  594     StreamPos_Last=Temp.StreamPos;
  595 
  596     //By the parser
  597     if (!Temp.Parsers.empty() && Temp.Parsers[0])
  598     {
  599         if (!Temp.Parsers[0]->Status[IsFinished])
  600         {
  601             Temp.Parsers[0]->ShouldContinueParsing=false;
  602             int64u File_Size_Temp=File_Size;
  603             File_Size=File_Offset+Buffer_Offset+Element_Offset;
  604             #if MEDIAINFO_EVENTS
  605                 Temp.Parsers[0]->PES_FirstByte_IsAvailable=false;
  606             #endif //MEDIAINFO_EVENTS
  607             Open_Buffer_Continue(Temp.Parsers[0], Buffer, 0, false);
  608             File_Size=File_Size_Temp;
  609             Finish(Temp.Parsers[0]);
  610             #if MEDIAINFO_DEMUX
  611                 if (Config->Demux_EventWasSent)
  612                     return;
  613             #endif //MEDIAINFO_DEMUX
  614         }
  615         for (size_t Pos=0; Pos<Temp.Count; Pos++)
  616         {
  617             Ztring ID=Retrieve(StreamKind_Last, Temp.StreamPos+Pos, General_ID);
  618             Ztring ID_String=Retrieve(StreamKind_Last, Temp.StreamPos+Pos, General_ID_String);
  619             Merge(*Temp.Parsers[0], StreamKind_Last, Pos, Temp.StreamPos+Pos);
  620             Fill(StreamKind_Last, Temp.StreamPos+Pos, General_ID, ID, true);
  621             Fill(StreamKind_Last, Temp.StreamPos+Pos, General_ID_String, ID_String, true);
  622         }
  623         if (!IsSub)
  624         {
  625             switch (KindOfStream)
  626             {
  627                 case KindOfStream_Private   :
  628                                                 if (Streams[0xBD].StreamOrder!=(size_t)-1)
  629                                                     Fill(StreamKind_Last, StreamPos_Last, General_StreamOrder, Streams[0xBD].StreamOrder+StreamOrder_CountOfPrivateStreams_Temp);
  630                                                 if (StreamOrder_CountOfPrivateStreams_Minus1 && StreamOrder_CountOfPrivateStreams_Temp<StreamOrder_CountOfPrivateStreams_Minus1)
  631                                                     StreamOrder_CountOfPrivateStreams_Temp++;
  632                                                 break;
  633                 case KindOfStream_Extension :
  634                                                 if (Streams[0xFD].StreamOrder!=(size_t)-1)
  635                                                     Fill(StreamKind_Last, StreamPos_Last, General_StreamOrder, Streams[0xFD].StreamOrder);
  636                                                 break;
  637                 default                     :
  638                                                 if (Temp.StreamOrder!=(size_t)-1)
  639                                                     Fill(StreamKind_Last, StreamPos_Last, General_StreamOrder, Temp.StreamOrder);
  640             }
  641             Fill(StreamKind_Last, StreamPos_Last, General_FirstPacketOrder, Temp.FirstPacketOrder);
  642         }
  643 
  644         //Special cases
  645         if (Temp.Parsers[0]->Count_Get(Stream_Video) && Temp.Parsers[0]->Count_Get(Stream_Text))
  646         {
  647             //Video and Text are together
  648             size_t Text_Count=Temp.Parsers[0]->Count_Get(Stream_Text);
  649             for (size_t Parser_Pos=0; Parser_Pos<Text_Count; Parser_Pos++)
  650             {
  651                 Ztring ID=Retrieve(Stream_Video, Temp.StreamPos, Video_ID)+__T("-")+Temp.Parsers[0]->Retrieve(Stream_Text, Parser_Pos, Text_ID);
  652                 StreamPos_Last=(size_t)-1;
  653                 for (size_t Pos=0; Pos<Count_Get(Stream_Text); Pos++)
  654                     if (Retrieve(Stream_Text, Pos, Text_ID)==ID && Retrieve(Stream_Video, Temp.StreamPos, "MuxingMode")==Temp.Parsers[0]->Retrieve(Stream_Text, Parser_Pos, "MuxingMode"))
  655                     {
  656                         StreamPos_Last=Pos;
  657                         break;
  658                     }
  659                 if (StreamPos_Last==(size_t)-1)
  660                     Stream_Prepare(Stream_Text, StreamPos_Last);
  661                 Merge(*Temp.Parsers[0], Stream_Text, Parser_Pos, StreamPos_Last);
  662 
  663                 if (!IsSub)
  664                     Fill(Stream_Text, StreamPos_Last, "MuxingMode_MoreInfo", __T("Muxed in Video #")+Ztring().From_Number(Temp.StreamPos+1));
  665                 Fill(Stream_Text, StreamPos_Last, Text_ID, ID, true);
  666                 Fill(Stream_Text, StreamPos_Last, Text_ID_String, Retrieve(Stream_Video, Temp.StreamPos, Video_ID_String)+__T("-")+Temp.Parsers[0]->Retrieve(Stream_Text, Parser_Pos, Text_ID), true);
  667                 Fill(Stream_Text, StreamPos_Last, Text_Delay, Retrieve(Stream_Video, Temp.StreamPos, Video_Delay), true);
  668                 if (!IsSub)
  669                 {
  670                     switch (KindOfStream)
  671                     {
  672                         case KindOfStream_Private   :
  673                                                         if (Streams[0xBD].StreamOrder!=(size_t)-1)
  674                                                             Fill(Stream_Text, StreamPos_Last, General_StreamOrder, Streams[0xBD].StreamOrder);
  675                                                         break;
  676                         case KindOfStream_Extension :
  677                                                         if (Streams[0xFD].StreamOrder!=(size_t)-1)
  678                                                             Fill(Stream_Text, StreamPos_Last, General_StreamOrder, Streams[0xFD].StreamOrder);
  679                                                         break;
  680                         default                     :
  681                                                         if (Temp.StreamOrder!=(size_t)-1)
  682                                                             Fill(Stream_Text, StreamPos_Last, General_StreamOrder, Temp.StreamOrder);
  683                     }
  684                     Fill(StreamKind_Last, StreamPos_Last, General_FirstPacketOrder, Temp.FirstPacketOrder);
  685                 }
  686             }
  687 
  688             StreamKind_Last=Temp.StreamKind;
  689             StreamPos_Last=Temp.StreamPos;
  690         }
  691 
  692         //From parser General part
  693         MergeGeneral(Temp.Parsers[0], General_LawRating);
  694         MergeGeneral(Temp.Parsers[0], General_Title);
  695         MergeGeneral(Temp.Parsers[0], General_Recorded_Date);
  696         MergeGeneral(Temp.Parsers[0], General_Encoded_Application);
  697         MergeGeneral(Temp.Parsers[0], General_Encoded_Application_CompanyName);
  698         MergeGeneral(Temp.Parsers[0], General_Encoded_Application_Name);
  699     }
  700 
  701     //Duration if it is missing from the parser
  702     int64u Duration=0;
  703     if (Temp.StreamKind!=Stream_Max && Retrieve(Temp.StreamKind, Temp.StreamPos, Fill_Parameter(Temp.StreamKind, Generic_Duration)).empty())
  704     {
  705          StreamKind_Last=Temp.StreamKind;
  706          StreamPos_Last=Temp.StreamPos;
  707 
  708         int64u Start=(int64u)-1, End=(int64u)-1, ByteDifference=(int64u)-1;
  709         if (Temp.TimeStamp_Start.DTS.TimeStamp!=(int64u)-1 && Temp.TimeStamp_End.DTS.TimeStamp!=(int64u)-1)
  710         {
  711             Start=Temp.TimeStamp_Start.DTS.TimeStamp;
  712             End=Temp.TimeStamp_End.DTS.TimeStamp;
  713         }
  714         else if (Temp.TimeStamp_Start.PTS.TimeStamp!=(int64u)-1 && Temp.TimeStamp_End.PTS.TimeStamp!=(int64u)-1)
  715         {
  716             Start=Temp.TimeStamp_Start.PTS.TimeStamp;
  717             End=Temp.TimeStamp_End.PTS.TimeStamp;
  718             if (Temp.TimeStamp_Start.PTS.File_Pos<Temp.TimeStamp_End.PTS.File_Pos)
  719                 ByteDifference=Temp.TimeStamp_End.PTS.File_Pos-Temp.TimeStamp_Start.PTS.File_Pos;
  720         }
  721         if (Start!=(int64u)-1 && End!=(int64u)-1)
  722         {
  723             //TimeStamp
  724             if (End<0x100000000LL && Start>0x100000000LL) //Testing coherancy: no 13 hours long files.
  725                 End+=0x200000000LL; //33 bits, cyclic
  726             if (Start<End)
  727             {
  728                 Duration=End-Start;
  729                 if (ByteDifference!=(int64u)-1)
  730                 {
  731                     float BitRate=(ByteDifference*8)/(((float)Duration)/9000);
  732                     if (BitRate>10000000000LL)
  733                         Duration=0;
  734                 }
  735                 if (Duration)
  736                 {
  737                     if (StreamKind_Last==Stream_Video)
  738                     {
  739                         float64 FrameRate=Retrieve(Stream_Video, StreamPos_Last, Video_FrameRate).To_float64();
  740                         if (FrameRate!=0)
  741                             Duration+=Ztring::ToZtring(90*1000/FrameRate, 0).To_int64u(); //We imagine that there is one frame in it
  742                     }
  743                     Duration/=90;
  744 
  745                     Fill(Temp.StreamKind, Temp.StreamPos, Fill_Parameter(Temp.StreamKind, Generic_Duration), Duration);
  746                 }
  747             }
  748         }
  749     }
  750     if (!Duration && !Retrieve(Temp.StreamKind, Temp.StreamPos, Fill_Parameter(Temp.StreamKind, Generic_Duration)).empty())
  751         Duration=Retrieve(Temp.StreamKind, Temp.StreamPos, Fill_Parameter(Temp.StreamKind, Generic_Duration)).To_int64u();
  752     if (Duration)
  753     {
  754         for (size_t StreamKind=Stream_General+1; StreamKind<Stream_Max; StreamKind++)
  755             for (size_t StreamPos=0; StreamPos<Count_Get((stream_t)StreamKind); StreamPos++)
  756                 if ((IsSub || (KindOfStream==KindOfStream_Main && Retrieve((stream_t)StreamKind, StreamPos, General_ID).To_int64u()==StreamID)) && Retrieve((stream_t)StreamKind, StreamPos, Fill_Parameter((stream_t)StreamKind, Generic_Duration)).empty())
  757                     Fill((stream_t)StreamKind, StreamPos, Fill_Parameter((stream_t)StreamKind, Generic_Duration), Duration);
  758     }
  759 
  760     Bitrate_Calc();
  761 }
  762 
  763 //---------------------------------------------------------------------------
  764 void File_MpegPs::Bitrate_Calc()
  765 {
  766     if (FrameInfo.PTS!=(int64u)-1 && (StreamKind_Last==Stream_Video || StreamKind_Last==Stream_Audio))
  767     {
  768         int64u BitRate=Retrieve(StreamKind_Last, StreamPos_Last, "BitRate").To_int64u();
  769         if (BitRate==0)
  770             BitRate=Retrieve(StreamKind_Last, StreamPos_Last, "BitRate_Nominal").To_int64u();
  771         if (BitRate==0)
  772             FrameInfo.PTS=(int64u)-1;
  773         else
  774             FrameInfo.PTS+=BitRate; //Saving global BitRate
  775     }
  776 }
  777 
  778 //***************************************************************************
  779 // Buffer - Synchro
  780 //***************************************************************************
  781 
  782 //---------------------------------------------------------------------------
  783 bool File_MpegPs::Synchronize()
  784 {
  785     //Synchronizing
  786     while (Buffer_Offset+4<=Buffer_Size && (Buffer[Buffer_Offset  ]!=0x00
  787                                          || Buffer[Buffer_Offset+1]!=0x00
  788                                          || Buffer[Buffer_Offset+2]!=0x01
  789                                          || Buffer[Buffer_Offset+3]< 0xB9))
  790     {
  791         Buffer_Offset+=2;
  792         while(Buffer_Offset<Buffer_Size && Buffer[Buffer_Offset]!=0x00)
  793             Buffer_Offset+=2;
  794         if (Buffer_Offset>=Buffer_Size || Buffer[Buffer_Offset-1]==0x00)
  795             Buffer_Offset--;
  796     }
  797 
  798     //Parsing last bytes if needed
  799     if (Buffer_Offset+4==Buffer_Size && (Buffer[Buffer_Offset  ]!=0x00
  800                                       || Buffer[Buffer_Offset+1]!=0x00
  801                                       || Buffer[Buffer_Offset+2]!=0x01
  802                                       || Buffer[Buffer_Offset+3]< 0xB9))
  803         Buffer_Offset++;
  804     if (Buffer_Offset+3==Buffer_Size && (Buffer[Buffer_Offset  ]!=0x00
  805                                       || Buffer[Buffer_Offset+1]!=0x00
  806                                       || Buffer[Buffer_Offset+2]!=0x01))
  807         Buffer_Offset++;
  808     if (Buffer_Offset+2==Buffer_Size && (Buffer[Buffer_Offset  ]!=0x00
  809                                       || Buffer[Buffer_Offset+1]!=0x00))
  810         Buffer_Offset++;
  811     if (Buffer_Offset+1==Buffer_Size &&  Buffer[Buffer_Offset  ]!=0x00)
  812         Buffer_Offset++;
  813 
  814     if (Buffer_Offset+3>Buffer_Size)
  815         return false;
  816 
  817     //Synched is OK
  818     return true;
  819 }
  820 
  821 //---------------------------------------------------------------------------
  822 bool File_MpegPs::Synched_Test()
  823 {
  824     //Trailing 0xFF
  825     while(Buffer_Offset<Buffer_Size && Buffer[Buffer_Offset]==0xFF)
  826         Buffer_Offset++;
  827 
  828     //Trailing 0x00
  829     while(Buffer_Offset+3<=Buffer_Size
  830        && Buffer[Buffer_Offset+2]==0x00
  831        && Buffer[Buffer_Offset+1]==0x00
  832        && Buffer[Buffer_Offset  ]==0x00)
  833         Buffer_Offset++;
  834 
  835     //Must have enough buffer for having header
  836     if (Buffer_Offset+3>Buffer_Size)
  837         return false;
  838 
  839     //Quick test of synchro
  840     if (Buffer[Buffer_Offset  ]!=0x00
  841      || Buffer[Buffer_Offset+1]!=0x00
  842      || Buffer[Buffer_Offset+2]!=0x01)
  843         Synched=false;
  844 
  845     //Quick search
  846     if (Synched && !Header_Parser_QuickSearch())
  847         return false;
  848 
  849     //We continue
  850     return true;
  851 }
  852 
  853 //---------------------------------------------------------------------------
  854 void File_MpegPs::Synched_Init()
  855 {
  856     //private_stream_1 specific
  857     private_stream_1_ID=0x00;
  858     private_stream_1_Offset=0;
  859     private_stream_1_IsDvdVideo=false;
  860 
  861     //Count
  862     video_stream_Count=(int8u)-1;
  863     audio_stream_Count=(int8u)-1;
  864     private_stream_1_Count=(int8u)-1;
  865     private_stream_2_Count=(int8u)-1;
  866     extension_stream_Count=(int8u)-1;
  867     SL_packetized_stream_Count=(int8u)-1;
  868 
  869     //From packets
  870     program_mux_rate=0;
  871 
  872     //Default values
  873     Streams.resize(0x100);
  874     Streams_Private1.resize(0x100);
  875     Streams_Extension.resize(0x100);
  876     Streams[0xBA].Searching_Payload=true;
  877 
  878     //Temp
  879     stream_id_extension=0x55; //Default is set to VC-1, should never happens, but happens sometimes
  880     FirstPacketOrder_Last=0;
  881 
  882     //Case of extraction from MPEG-TS files
  883     if (File_Offset==0 && Buffer_Size>=4 && ((CC4(Buffer)&0xFFFFFFF0)==0x000001E0 || (CC4(Buffer)&0xFFFFFFE0)==0x000001C0 || CC4(Buffer)==0x000001BD || CC4(Buffer)==0x000001FA || CC4(Buffer)==0x000001FD || CC4(Buffer)==0x000001FE))
  884     {
  885         FromTS=true; //We want to anlyze this kind of file
  886         MPEG_Version=2; //By default, MPEG-TS is version 2
  887         Streams[Buffer[3]].Searching_Payload=true; //Activating the Streams
  888     }
  889 
  890     //TS specific
  891     if (FromTS)
  892     {
  893         Streams[0xBD].Init_Stream(true);            //private_stream_1
  894         Streams[0xBF].Init_Stream(true);            //private_stream_2
  895         for (int8u Pos=0xC0; Pos<=0xEF; Pos++)
  896         {
  897             Streams[Pos].Init_Stream(true);         //audio_stream or video_stream
  898         }
  899         Streams[0xFA].Init_Stream(true);      //LATM
  900         Streams[0xFD].Init_Stream(true);      //extension_stream
  901         Streams[0xFE].Init_Stream(true);      //extension_stream?
  902     }
  903 
  904     //
  905     Frequency_c=90000;
  906 }
  907 
  908 //***************************************************************************
  909 // Buffer - Global
  910 //***************************************************************************
  911 
  912 //---------------------------------------------------------------------------
  913 void File_MpegPs::Read_Buffer_Init()
  914 {
  915     #if MEDIAINFO_DEMUX
  916     //     Demux_UnpacketizeContainer=Config->Demux_Unpacketize_Get();
  917     #endif //MEDIAINFO_DEMUX
  918 }
  919 
  920 //---------------------------------------------------------------------------
  921 #if MEDIAINFO_ADVANCED2
  922 void File_MpegPs::Read_Buffer_SegmentChange()
  923 {
  924     if (!Streams.empty())
  925         for (size_t StreamID=0; StreamID<0x100; StreamID++)
  926         {
  927             for (size_t Pos=0; Pos<Streams[StreamID].Parsers.size(); Pos++)
  928                 if (Streams[StreamID].Parsers[Pos])
  929                     Streams[StreamID].Parsers[Pos]->Open_Buffer_SegmentChange();
  930             for (size_t Pos=0; Pos<Streams_Private1[StreamID].Parsers.size(); Pos++)
  931                 if (Streams_Private1[StreamID].Parsers[Pos])
  932                     Streams_Private1[StreamID].Parsers[Pos]->Open_Buffer_SegmentChange();
  933             for (size_t Pos=0; Pos<Streams_Extension[StreamID].Parsers.size(); Pos++)
  934                 if (Streams_Extension[StreamID].Parsers[Pos])
  935                     Streams_Extension[StreamID].Parsers[Pos]->Open_Buffer_SegmentChange();
  936         }
  937 }
  938 #endif //MEDIAINFO_ADVANCED2
  939 
  940 //---------------------------------------------------------------------------
  941 void File_MpegPs::Read_Buffer_Unsynched()
  942 {
  943     Searching_TimeStamp_Start=false;
  944 
  945     if (Streams.empty())
  946        return;
  947 
  948     //No need anymore of this Streams
  949     Streams[0xBB].Searching_Payload=false; //system_start
  950 
  951     //Reactivating interessant PS streams
  952     for (size_t StreamID=0; StreamID<0x100; StreamID++)
  953     {
  954         //End timestamp is out of date
  955         Streams[StreamID].Set_Unsynch_Frame_Count(Frame_Count_NotParsedIncluded, IsSub);
  956         Streams_Private1[StreamID].Set_Unsynch_Frame_Count(Unsynch_Frame_Count,true);
  957         Streams_Extension[StreamID].Set_Unsynch_Frame_Count(Unsynch_Frame_Count,true);
  958     }
  959     #if MEDIAINFO_SEEK
  960         Unsynch_Frame_Count=(int64u)-1; //We do not use it
  961     #endif //MEDIAINFO_SEEK
  962     video_stream_Unlimited=false;
  963     Buffer_DataSizeToParse=0;
  964     PES_FirstByte_IsAvailable=false;
  965 }
  966 
  967 //---------------------------------------------------------------------------
  968 #if MEDIAINFO_SEEK
  969 size_t File_MpegPs::Read_Buffer_Seek (size_t Method, int64u Value, int64u ID)
  970 {
  971     //Reset
  972     Seek_Value=(int64u)-1;
  973     Seek_ID=(int64u)-1;
  974 
  975     //Init
  976     if (!Duration_Detected)
  977     {
  978         //External IBI
  979         #if MEDIAINFO_IBIUSAGE
  980             std::string IbiFile=Config->Ibi_Get();
  981             if (!IbiFile.empty())
  982             {
  983                 Ibi.Streams.clear(); //TODO: support IBI data from different inputs
  984 
  985                 File_Ibi MI;
  986                 Open_Buffer_Init(&MI, IbiFile.size());
  987                 MI.Ibi=&Ibi;
  988                 MI.Open_Buffer_Continue((const int8u*)IbiFile.c_str(), IbiFile.size());
  989             }
  990             //Creating base IBI from a quick analysis of the file
  991             else
  992             {
  993                 MediaInfo_Internal MI;
  994                 MI.Option(__T("File_KeepInfo"), __T("1"));
  995                 Ztring ParseSpeed_Save=MI.Option(__T("ParseSpeed_Get"), __T(""));
  996                 Ztring Demux_Save=MI.Option(__T("Demux_Get"), __T(""));
  997                 MI.Option(__T("ParseSpeed"), __T("0"));
  998                 MI.Option(__T("Demux"), Ztring());
  999                 size_t MiOpenResult=MI.Open(File_Name);
 1000                 MI.Option(__T("ParseSpeed"), ParseSpeed_Save); //This is a global value, need to reset it. TODO: local value
 1001                 MI.Option(__T("Demux"), Demux_Save); //This is a global value, need to reset it. TODO: local value
 1002                 if (!MiOpenResult)
 1003                     return 0;
 1004                 for (ibi::streams::iterator IbiStream_Temp=((File_MpegPs*)MI.Info)->Ibi.Streams.begin(); IbiStream_Temp!=((File_MpegPs*)MI.Info)->Ibi.Streams.end(); ++IbiStream_Temp)
 1005                 {
 1006                     if (Ibi.Streams[IbiStream_Temp->first]==NULL)
 1007                         Ibi.Streams[IbiStream_Temp->first]=new ibi::stream(*IbiStream_Temp->second);
 1008                     Ibi.Streams[IbiStream_Temp->first]->Unsynch();
 1009                     for (size_t Pos=0; Pos<IbiStream_Temp->second->Infos.size(); Pos++)
 1010                     {
 1011                         Ibi.Streams[IbiStream_Temp->first]->Add(IbiStream_Temp->second->Infos[Pos]);
 1012                         if (!IbiStream_Temp->second->Infos[Pos].IsContinuous)
 1013                             Ibi.Streams[IbiStream_Temp->first]->Unsynch();
 1014                     }
 1015                     Ibi.Streams[IbiStream_Temp->first]->Unsynch();
 1016                 }
 1017                 if (Ibi.Streams.empty())
 1018                     return 4; //Problem during IBI file parsing
 1019             }
 1020         #endif //#if MEDIAINFO_IBIUSAGE
 1021 
 1022         Duration_Detected=true;
 1023     }
 1024 
 1025     //Parsing
 1026     switch (Method)
 1027     {
 1028         case 0  :
 1029                     GoTo(Value);
 1030                     Open_Buffer_Unsynch();
 1031                     return 1;
 1032         case 1  :
 1033                     GoTo(File_Size*Value/10000);
 1034                     Open_Buffer_Unsynch();
 1035                     return 1;
 1036         case 2  :   //Timestamp
 1037                     #if MEDIAINFO_IBIUSAGE
 1038                     {
 1039                     ibi::streams::iterator IbiStream_Temp;
 1040                     if (ID==(int64u)-1)
 1041                         IbiStream_Temp=Ibi.Streams.begin();
 1042                     else
 1043                         IbiStream_Temp=Ibi.Streams.find(ID);
 1044                     if (IbiStream_Temp==Ibi.Streams.end() || IbiStream_Temp->second->Infos.empty())
 1045                         return 5; //Invalid ID
 1046 
 1047                     if (!(IbiStream_Temp->second->DtsFrequencyNumerator==1000000000 && IbiStream_Temp->second->DtsFrequencyDenominator==1))
 1048                     {
 1049                         float64 ValueF=(float64)Value;
 1050                         ValueF/=1000000000; //Value is in ns
 1051                         ValueF/=IbiStream_Temp->second->DtsFrequencyDenominator;
 1052                         ValueF*=IbiStream_Temp->second->DtsFrequencyNumerator;
 1053                         Value=float64_int64s(ValueF);
 1054                     }
 1055 
 1056                     for (size_t Pos=0; Pos<IbiStream_Temp->second->Infos.size(); Pos++)
 1057                     {
 1058                         if (Value<=IbiStream_Temp->second->Infos[Pos].Dts)
 1059                         {
 1060                             if (Value<IbiStream_Temp->second->Infos[Pos].Dts && Pos)
 1061                                 Pos--;
 1062 
 1063                             //Checking continuity of Ibi
 1064                             if (!IbiStream_Temp->second->Infos[Pos].IsContinuous && Pos+1<IbiStream_Temp->second->Infos.size())
 1065                             {
 1066                                 Config->Demux_IsSeeking=true;
 1067                                 Seek_Value=Value;
 1068                                 Seek_Value_Maximal=IbiStream_Temp->second->Infos[Pos+1].StreamOffset;
 1069                                 Seek_ID=IbiStream_Temp->first;
 1070                                 GoTo((IbiStream_Temp->second->Infos[Pos].StreamOffset+IbiStream_Temp->second->Infos[Pos+1].StreamOffset)/2);
 1071                                 Open_Buffer_Unsynch();
 1072 
 1073                                 return 1;
 1074                             }
 1075 
 1076                             Config->Demux_IsSeeking=false;
 1077                             if (!Streams[(size_t)IbiStream_Temp->first].Parsers.empty())
 1078                                 for (size_t Parser_Pos=0; Parser_Pos<Streams[(size_t)IbiStream_Temp->first].Parsers.size(); Parser_Pos++)
 1079                                     Streams[(size_t)IbiStream_Temp->first].Parsers[Parser_Pos]->Unsynch_Frame_Count=IbiStream_Temp->second->Infos[Pos].FrameNumber;
 1080                             else
 1081                                 Unsynch_Frame_Counts[(int16u)IbiStream_Temp->first]=IbiStream_Temp->second->Infos[Pos].FrameNumber;
 1082 
 1083                             GoTo(IbiStream_Temp->second->Infos[Pos].StreamOffset);
 1084                             Open_Buffer_Unsynch();
 1085 
 1086                             return 1;
 1087                         }
 1088                     }
 1089 
 1090                     return 2; //Invalid value
 1091                     }
 1092                     #else //MEDIAINFO_IBIUSAGE
 1093                     return (size_t)-2; //Not supported / IBI disabled
 1094                     #endif //MEDIAINFO_IBIUSAGE
 1095         case 3  :   //FrameNumber
 1096                     #if MEDIAINFO_IBIUSAGE
 1097                     {
 1098                     ibi::streams::iterator IbiStream_Temp;
 1099                     if (ID==(int64u)-1)
 1100                         IbiStream_Temp=Ibi.Streams.begin();
 1101                     else
 1102                         IbiStream_Temp=Ibi.Streams.find(ID);
 1103                     if (IbiStream_Temp==Ibi.Streams.end() || IbiStream_Temp->second->Infos.empty())
 1104                         return 5; //Invalid ID
 1105 
 1106                     for (size_t Pos=0; Pos<IbiStream_Temp->second->Infos.size(); Pos++)
 1107                     {
 1108                         if (Value<=IbiStream_Temp->second->Infos[Pos].FrameNumber)
 1109                         {
 1110                             if (Value<IbiStream_Temp->second->Infos[Pos].FrameNumber && Pos)
 1111                                 Pos--;
 1112 
 1113                             if (!Streams[(size_t)IbiStream_Temp->first].Parsers.empty())
 1114                                 for (size_t Parser_Pos=0; Parser_Pos<Streams[(size_t)IbiStream_Temp->first].Parsers.size(); Parser_Pos++)
 1115                                     Streams[(size_t)IbiStream_Temp->first].Parsers[Parser_Pos]->Unsynch_Frame_Count=IbiStream_Temp->second->Infos[Pos].FrameNumber;
 1116                             else
 1117                                 Unsynch_Frame_Counts[(int16u)IbiStream_Temp->first]=IbiStream_Temp->second->Infos[Pos].FrameNumber;
 1118 
 1119                             GoTo(IbiStream_Temp->second->Infos[Pos].StreamOffset);
 1120                             Open_Buffer_Unsynch();
 1121 
 1122                             return 1;
 1123                         }
 1124                     }
 1125 
 1126                     return 2; //Invalid value
 1127                     }
 1128                     #else //MEDIAINFO_IBIUSAGE
 1129                     return (size_t)-2; //Not supported / IBI disabled
 1130                     #endif //MEDIAINFO_IBIUSAGE
 1131         default :   return (size_t)-1; //Not supported
 1132     }
 1133 }
 1134 #endif //MEDIAINFO_SEEK
 1135 
 1136 //---------------------------------------------------------------------------
 1137 void File_MpegPs::Read_Buffer_Continue()
 1138 {
 1139     #if MEDIAINFO_DEMUX
 1140         if (Demux_StreamIsBeingParsed_type!=(int8u)-1)
 1141         {
 1142             switch (Demux_StreamIsBeingParsed_type) //TODO: transform the switch() case to a enum with a vector of streams
 1143             {
 1144                 case 0 :    Open_Buffer_Continue(Streams[Demux_StreamIsBeingParsed_stream_id].Parsers[0], Buffer, 0, false);
 1145                             if (IsSub && Streams[Demux_StreamIsBeingParsed_stream_id].Parsers[0]->Frame_Count_NotParsedIncluded!=(int64u)-1)
 1146                                 Frame_Count_NotParsedIncluded=Streams[Demux_StreamIsBeingParsed_stream_id].Parsers[0]->Frame_Count_NotParsedIncluded;
 1147                             break;
 1148                 case 1 :    Open_Buffer_Continue(Streams_Private1[Demux_StreamIsBeingParsed_stream_id].Parsers[0], Buffer, 0, false);
 1149                             if (IsSub && Streams_Private1[Demux_StreamIsBeingParsed_stream_id].Parsers[0]->Frame_Count_NotParsedIncluded!=(int64u)-1)
 1150                                 Frame_Count_NotParsedIncluded=Streams_Private1[Demux_StreamIsBeingParsed_stream_id].Parsers[0]->Frame_Count_NotParsedIncluded;
 1151                             break;
 1152                 case 2 :    Open_Buffer_Continue(Streams_Extension[Demux_StreamIsBeingParsed_stream_id].Parsers[0], Buffer, 0, false);
 1153                             if (IsSub && Streams_Extension[Demux_StreamIsBeingParsed_stream_id].Parsers[0]->Frame_Count_NotParsedIncluded!=(int64u)-1)
 1154                                 Frame_Count_NotParsedIncluded=Streams_Extension[Demux_StreamIsBeingParsed_stream_id].Parsers[0]->Frame_Count_NotParsedIncluded;
 1155                             break;
 1156                 default: ;
 1157             }
 1158             if (Config->Demux_EventWasSent)
 1159                 return;
 1160             Demux_StreamIsBeingParsed_type=(int8u)-1;
 1161         }
 1162     #endif //MEDIAINFO_DEMUX
 1163 
 1164     if (!IsSub)
 1165     {
 1166         if (Config->ParseSpeed>=1.0)
 1167             Config->State_Set(((float)Buffer_TotalBytes)/File_Size);
 1168         else if (Buffer_TotalBytes>2*SizeToAnalyze)
 1169             Config->State_Set((float)0.99); //Nearly the end
 1170         else
 1171             Config->State_Set(((float)Buffer_TotalBytes)/(2*SizeToAnalyze));
 1172     }
 1173 
 1174     if (Buffer_DataSizeToParse)
 1175     {
 1176         #if MEDIAINFO_EVENTS
 1177             if (FromTS)
 1178             {
 1179                 PES_FirstByte_IsAvailable=true;
 1180                 PES_FirstByte_Value=false;
 1181             }
 1182         #endif //MEDIAINFO_EVENTS
 1183 
 1184         if (Buffer_Size<=Buffer_DataSizeToParse)
 1185         {
 1186             Element_Size=Buffer_Size; //All the buffer is used
 1187             Buffer_DataSizeToParse-=(int16u)Buffer_Size;
 1188         }
 1189         else
 1190         {
 1191             Element_Size=Buffer_DataSizeToParse;
 1192             Buffer_DataSizeToParse=0;
 1193         }
 1194 
 1195         Element_Begin0();
 1196         Data_Parse();
 1197         Element_Offset=Element_Size;
 1198         Element_End0();
 1199     }
 1200 
 1201     //Video unlimited specific, we didn't wait for the end (because this is... unlimited)
 1202     if (video_stream_Unlimited)
 1203     {
 1204         PES_FirstByte_IsAvailable=true;
 1205         PES_FirstByte_Value=false;
 1206 
 1207         //Look for next Sync word
 1208         size_t Buffer_Offset_Temp=0;
 1209         while (Buffer_Offset_Temp+4<=Buffer_Size
 1210             && (Buffer[Buffer_Offset_Temp  ]!=0x00
 1211              || Buffer[Buffer_Offset_Temp+1]!=0x00
 1212              || Buffer[Buffer_Offset_Temp+2]!=0x01
 1213              || Buffer[Buffer_Offset_Temp+3]< 0xB9))
 1214         {
 1215             Buffer_Offset_Temp+=2;
 1216             while(Buffer_Offset_Temp<Buffer_Size && Buffer[Buffer_Offset_Temp]!=0x00)
 1217                 Buffer_Offset_Temp+=2;
 1218             if (Buffer_Offset_Temp>=Buffer_Size || Buffer[Buffer_Offset_Temp-1]==0x00)
 1219                 Buffer_Offset_Temp--;
 1220         }
 1221 
 1222         //Parsing last bytes if needed
 1223         if (Buffer_Offset_Temp+4==Buffer_Size && (Buffer[Buffer_Offset_Temp  ]!=0x00
 1224                                                || Buffer[Buffer_Offset_Temp+1]!=0x00
 1225                                                || Buffer[Buffer_Offset_Temp+2]!=0x01))
 1226             Buffer_Offset_Temp++;
 1227         if (Buffer_Offset_Temp+3==Buffer_Size && (Buffer[Buffer_Offset_Temp  ]!=0x00
 1228                                                || Buffer[Buffer_Offset_Temp+1]!=0x00
 1229                                                || Buffer[Buffer_Offset_Temp+2]!=0x01))
 1230             Buffer_Offset_Temp++;
 1231         if (Buffer_Offset_Temp+2==Buffer_Size && (Buffer[Buffer_Offset_Temp  ]!=0x00
 1232                                                || Buffer[Buffer_Offset_Temp+1]!=0x00))
 1233             Buffer_Offset_Temp++;
 1234         if (Buffer_Offset_Temp+1==Buffer_Size &&  Buffer[Buffer_Offset_Temp  ]!=0x00)
 1235             Buffer_Offset_Temp++;
 1236 
 1237         if (Buffer_Offset_Temp==Buffer_Size)
 1238         {
 1239             Element_Size=Buffer_Size; //All the buffer is used
 1240         }
 1241         else
 1242         {
 1243             Element_Size=Buffer_Offset_Temp;
 1244             if (Buffer_Offset_Temp+4<=Buffer_Size)
 1245                 video_stream_Unlimited=false;
 1246             else
 1247                 Element_IsWaitingForMoreData(); //We don't know if the next bytes are a stream_id or data
 1248         }
 1249 
 1250         if (Element_Size)
 1251         {
 1252             Element_Begin0();
 1253             Data_Parse();
 1254             Element_Offset=Element_Size;
 1255             Element_End0();
 1256         }
 1257     }
 1258 }
 1259 
 1260 //---------------------------------------------------------------------------
 1261 void File_MpegPs::Read_Buffer_AfterParsing()
 1262 {
 1263     if (!Status[IsFilled])
 1264     {
 1265         //In case of problem with some streams
 1266         if (Buffer_TotalBytes>Buffer_TotalBytes_FirstSynched+SizeToAnalyze)
 1267         {
 1268             if (!Status[IsAccepted])
 1269             {
 1270                 Reject("MPEG-PS");
 1271                 return;
 1272             }
 1273 
 1274             video_stream_Count=0;
 1275             audio_stream_Count=0;
 1276             private_stream_1_Count=0;
 1277             private_stream_2_Count=0;
 1278             extension_stream_Count=0;
 1279             SL_packetized_stream_Count=0;
 1280         }
 1281 
 1282         //Jumping only if needed
 1283         if (Streams.empty() || video_stream_Count || audio_stream_Count || private_stream_1_Count || private_stream_2_Count || extension_stream_Count || SL_packetized_stream_Count)
 1284             return;
 1285 
 1286         //Jumping if needed
 1287         if (!Status[IsAccepted])
 1288         {
 1289             Accept("MPEG-PS");
 1290             if (!IsSub)
 1291                 Fill(Stream_General, 0, General_Format, "MPEG-PS");
 1292         }
 1293         Fill("MPEG-PS");
 1294         if (!ShouldContinueParsing && File_Offset+Buffer_Size+SizeToAnalyze<File_Size && Config->ParseSpeed<1.0)
 1295         {
 1296             //Jumping
 1297             GoToFromEnd(SizeToAnalyze, "MPEG-PS");
 1298             Open_Buffer_Unsynch();
 1299         }
 1300     }
 1301 }
 1302 
 1303 //***************************************************************************
 1304 // Buffer - Par element
 1305 //***************************************************************************
 1306 
 1307 //---------------------------------------------------------------------------
 1308 void File_MpegPs::Header_Parse()
 1309 {
 1310     PES_FirstByte_IsAvailable=true;
 1311     PES_FirstByte_Value=true;
 1312 
 1313     //Reinit
 1314     #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1315         if (!FromAribStdB24B37)
 1316     #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1317         {
 1318             FrameInfo.PTS=(int64u)-1;
 1319             FrameInfo.DTS=(int64u)-1;
 1320         }
 1321 
 1322     #if MEDIAINFO_TRACE
 1323     if (Trace_Activated)
 1324     {
 1325         //Parsing
 1326         Skip_B3(                                                    "synchro");
 1327         Get_B1 (stream_id,                                         "stream_id");
 1328     }
 1329     else
 1330     {
 1331     #endif //MEDIAINFO_TRACE
 1332         //Parsing
 1333         stream_id=Buffer[Buffer_Offset+3];
 1334         Element_Offset+=4;
 1335     #if MEDIAINFO_TRACE
 1336     }
 1337     #endif //MEDIAINFO_TRACE
 1338 
 1339     if (stream_id!=0xB9 && stream_id!=0xBA) //MPEG_program_end or pack_start have no PES
 1340     {
 1341         if (!Header_Parse_PES_packet(stream_id))
 1342         {
 1343             Element_WaitForMoreData();
 1344             return;
 1345         }
 1346     }
 1347     else if (!Header_Parse_Fill_Size()) //MPEG_program_end or pack_start specific
 1348     {
 1349         Element_WaitForMoreData();
 1350         return;
 1351     }
 1352     Header_Fill_Code(stream_id);
 1353 }
 1354 
 1355 //---------------------------------------------------------------------------
 1356 bool File_MpegPs::Header_Parse_Fill_Size()
 1357 {
 1358     //Look for next Sync word
 1359     if (Buffer_Offset_Temp==0) //Buffer_Offset_Temp is not 0 if Header_Parse_Fill_Size() has already parsed first frames
 1360         Buffer_Offset_Temp=Buffer_Offset+4;
 1361     while (Buffer_Offset_Temp+4<=Buffer_Size
 1362         && (Buffer[Buffer_Offset_Temp  ]!=0x00
 1363          || Buffer[Buffer_Offset_Temp+1]!=0x00
 1364          || Buffer[Buffer_Offset_Temp+2]!=0x01
 1365          || Buffer[Buffer_Offset_Temp+3]< 0xB9))
 1366     {
 1367         Buffer_Offset_Temp+=2;
 1368         while(Buffer_Offset_Temp<Buffer_Size && Buffer[Buffer_Offset_Temp]!=0x00)
 1369             Buffer_Offset_Temp+=2;
 1370         if (Buffer_Offset_Temp>=Buffer_Size || Buffer[Buffer_Offset_Temp-1]==0x00)
 1371             Buffer_Offset_Temp--;
 1372     }
 1373 
 1374     //Parsing last bytes if needed
 1375     if (Buffer_Offset_Temp+4==Buffer_Size && (Buffer[Buffer_Offset_Temp  ]!=0x00
 1376                                            || Buffer[Buffer_Offset_Temp+1]!=0x00
 1377                                            || Buffer[Buffer_Offset_Temp+2]!=0x01))
 1378         Buffer_Offset_Temp++;
 1379     if (Buffer_Offset_Temp+3==Buffer_Size && (Buffer[Buffer_Offset_Temp  ]!=0x00
 1380                                            || Buffer[Buffer_Offset_Temp+1]!=0x00
 1381                                            || Buffer[Buffer_Offset_Temp+2]!=0x01))
 1382         Buffer_Offset_Temp++;
 1383     if (Buffer_Offset_Temp+2==Buffer_Size && (Buffer[Buffer_Offset_Temp  ]!=0x00
 1384                                            || Buffer[Buffer_Offset_Temp+1]!=0x00))
 1385         Buffer_Offset_Temp++;
 1386     if (Buffer_Offset_Temp+1==Buffer_Size &&  Buffer[Buffer_Offset_Temp  ]!=0x00)
 1387         Buffer_Offset_Temp++;
 1388 
 1389     if (Buffer_Offset_Temp+4>Buffer_Size)
 1390     {
 1391         if (Config->IsFinishing)
 1392             Buffer_Offset_Temp=Buffer_Size; //We are sure that the next bytes are a start
 1393         else
 1394             return false;
 1395     }
 1396 
 1397     //OK, we continue
 1398     Header_Fill_Size(Buffer_Offset_Temp-Buffer_Offset);
 1399     Buffer_Offset_Temp=0;
 1400     return true;
 1401 }
 1402 
 1403 //---------------------------------------------------------------------------
 1404 bool File_MpegPs::Header_Parse_PES_packet(int8u stream_id)
 1405 {
 1406     //Parsing
 1407     int16u PES_packet_length;
 1408     Get_B2 (PES_packet_length,                                  "PES_packet_length");
 1409     #if MEDIAINFO_DEMUX
 1410         if (Demux_UnpacketizeContainer && Buffer_Offset+6+PES_packet_length>Buffer_Size)
 1411             return false;
 1412     #endif //MEDIAINFO_DEMUX
 1413     if (PES_packet_length && Buffer_Offset+6+PES_packet_length>=Buffer_Size && Config->IsFinishing)
 1414         PES_packet_length=(int16u)(Buffer_Size-(Buffer_Offset+6));
 1415 
 1416     //Parsing
 1417     switch (stream_id)
 1418     {
 1419         //Header is only Size
 1420         case 0xBB : //system_header_start
 1421         case 0xBC : //program_stream_map
 1422         case 0xBE : //padding_stream
 1423         case 0xBF : //private_stream_2
 1424         case 0xF0 : //ECM
 1425         case 0xF1 : //EMM
 1426         case 0xF2 : //DSMCC Streams
 1427         case 0xF8 : //ITU-T Rec. H .222.1 type E
 1428         case 0xFF : //Program Streams directory
 1429             break;
 1430 
 1431         //Element with PES Header
 1432         default :
 1433             switch (MPEG_Version)
 1434             {
 1435                 case 1  : Header_Parse_PES_packet_MPEG1(stream_id); break;
 1436                 case 2  : Header_Parse_PES_packet_MPEG2(stream_id); break;
 1437                 default : ; //We don't know what to parse...
 1438             }
 1439     }
 1440 
 1441     //Video unlimited specific
 1442     if (PES_packet_length==0)
 1443     {
 1444         if (!Header_Parse_Fill_Size())
 1445         {
 1446             //Return directly if we must unpack the elementary stream;
 1447             #if MEDIAINFO_DEMUX
 1448                 if (Demux_UnpacketizeContainer)
 1449                     return false;
 1450             #endif //MEDIAINFO_DEMUX
 1451 
 1452             //Next PS packet is not found, we will use all the buffer
 1453             Header_Fill_Size(Buffer_Size-Buffer_Offset); //All the buffer is used
 1454             video_stream_Unlimited=true;
 1455             Buffer_Offset_Temp=0; //We use the buffer
 1456         }
 1457     }
 1458     else
 1459         //Filling
 1460         Header_Fill_Size(6+PES_packet_length);
 1461 
 1462     //Can be cut in small chunks
 1463     if (Element_IsWaitingForMoreData())
 1464         return false;
 1465     if (PES_packet_length!=0 && Element_Offset<Element_Size && (size_t)(6+PES_packet_length)>Buffer_Size-Buffer_Offset
 1466      && ((stream_id&0xE0)==0xC0 || (stream_id&0xF0)==0xE0))
 1467     {
 1468         //Return directly if we must unpack the elementary stream;
 1469         #if MEDIAINFO_DEMUX
 1470             if (Demux_UnpacketizeContainer)
 1471                 return false;
 1472         #endif //MEDIAINFO_DEMUX
 1473 
 1474         Header_Fill_Size(Buffer_Size-Buffer_Offset); //All the buffer is used
 1475         Buffer_DataSizeToParse=6+PES_packet_length-(int16u)(Buffer_Size-Buffer_Offset);
 1476         Buffer_Offset_Temp=0; //We use the buffer
 1477     }
 1478 
 1479     return true;
 1480 }
 1481 
 1482 //---------------------------------------------------------------------------
 1483 // Packet header data - MPEG-1
 1484 void File_MpegPs::Header_Parse_PES_packet_MPEG1(int8u stream_id)
 1485 {
 1486     int8u stuffing_byte;
 1487     do
 1488     {
 1489         Peek_B1(stuffing_byte);
 1490         if (stuffing_byte==0xFF)
 1491             Skip_B1(                                            "stuffing_byte");
 1492     }
 1493     while(stuffing_byte==0xFF);
 1494 
 1495     if ((stuffing_byte&0xC0)==0x40)
 1496     {
 1497         BS_Begin();
 1498         Mark_0();
 1499         Mark_1();
 1500         Skip_SB(                                                "STD_buffer_scale");
 1501         Skip_S2(13,                                             "STD_buffer_size");
 1502         BS_End();
 1503         Peek_B1(stuffing_byte);
 1504     }
 1505     if ((stuffing_byte&0xF0)==0x20)
 1506     {
 1507         int16u PTS_29, PTS_14;
 1508         int8u  PTS_32;
 1509         Element_Begin1("PTS");
 1510         BS_Begin();
 1511         Mark_0();
 1512         Mark_0();
 1513         Mark_1();
 1514         Mark_0();
 1515         Get_S1 ( 3, PTS_32,                                     "PTS_32");
 1516         Mark_1_NoTrustError(); //Found 0 in one file
 1517         Get_S2 (15, PTS_29,                                     "PTS_29");
 1518         Mark_1();
 1519         Get_S2 (15, PTS_14,                                     "PTS_14");
 1520         Mark_1();
 1521         BS_End();
 1522 
 1523         //Filling
 1524         #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1525             if (!FromAribStdB24B37)
 1526         #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1527                 FrameInfo.PTS=(((int64u)PTS_32)<<30)
 1528                             | (((int64u)PTS_29)<<15)
 1529                             | (((int64u)PTS_14));
 1530 
 1531         //Incoherencies test
 1532         if (!Config->File_MpegPs_PTS_Begin_IsNearZero && Frame_Count<16)
 1533         {
 1534             if (FrameInfo.PTS<90000 || FrameInfo.PTS>0x200000000LL-90000) // 1 second before and after 
 1535                 Config->File_MpegPs_PTS_Begin_IsNearZero=true;
 1536         }
 1537         if (Config->File_MpegPs_PTS_Begin_IsNearZero && FrameInfo.PTS>0x200000000LL-90000)
 1538             FrameInfo.PTS=0; //TODO: find a better method for synchronizing streams, Hack in case DTS is negative (currently not supported by MI). TODO: negative DTS.
 1539 
 1540         if (Streams[stream_id].Searching_TimeStamp_End && stream_id!=0xBD && stream_id!=0xFD) //0xBD and 0xFD can contain multiple streams, TimeStamp management is in Streams management
 1541         {
 1542             if (Streams[stream_id].TimeStamp_End.PTS.TimeStamp==(int64u)-1)
 1543                 Streams[stream_id].TimeStamp_End.PTS.TimeStamp=FrameInfo.PTS;
 1544             #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1545                 if (!FromAribStdB24B37)
 1546             #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1547                     while (FrameInfo.PTS+0x100000000LL<Streams[stream_id].TimeStamp_End.PTS.TimeStamp)
 1548                         FrameInfo.PTS+=0x200000000LL;
 1549             Streams[stream_id].TimeStamp_End.DTS.File_Pos=Streams[stream_id].TimeStamp_End.PTS.File_Pos=File_Offset+Buffer_Offset;
 1550             Streams[stream_id].TimeStamp_End.DTS.TimeStamp=Streams[stream_id].TimeStamp_End.PTS.TimeStamp=FrameInfo.PTS;
 1551         }
 1552         if (Searching_TimeStamp_Start && Streams[stream_id].Searching_TimeStamp_Start && stream_id!=0xBD && stream_id!=0xFD) //0xBD and 0xFD can contain multiple streams, TimeStamp management is in Streams management
 1553         {
 1554             Streams[stream_id].TimeStamp_Start.DTS.File_Pos=Streams[stream_id].TimeStamp_Start.PTS.File_Pos=File_Offset+Buffer_Offset;
 1555             Streams[stream_id].TimeStamp_Start.DTS.TimeStamp=Streams[stream_id].TimeStamp_Start.PTS.TimeStamp=FrameInfo.PTS;
 1556             Streams[stream_id].Searching_TimeStamp_Start=false;
 1557         }
 1558         Element_Info_From_Milliseconds(float64_int64s(((float64)FrameInfo.PTS)/90));
 1559         #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1560             if (!FromAribStdB24B37)
 1561         #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1562                 TS_Set(FrameInfo.PTS);
 1563         HasTimeStamps=true;
 1564         Element_End0();
 1565     }
 1566     else if ((stuffing_byte&0xF0)==0x30)
 1567     {
 1568         int16u PTS_29, PTS_14, DTS_29, DTS_14;
 1569         int8u  PTS_32, DTS_32;
 1570         Element_Begin1("PTS");
 1571         BS_Begin();
 1572         Mark_0();
 1573         Mark_0();
 1574         Mark_1();
 1575         Mark_1();
 1576         Get_S1 ( 3, PTS_32,                                     "PTS_32");
 1577         Mark_1_NoTrustError(); //Found 0 in one file
 1578         Get_S2 (15, PTS_29,                                     "PTS_29");
 1579         Mark_1();
 1580         Get_S2 (15, PTS_14,                                     "PTS_14");
 1581         Mark_1();
 1582         BS_End();
 1583 
 1584         //Filling
 1585         #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1586             if (!FromAribStdB24B37)
 1587         #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1588                 FrameInfo.PTS=(((int64u)PTS_32)<<30)
 1589                             | (((int64u)PTS_29)<<15)
 1590                             | (((int64u)PTS_14));
 1591 
 1592         //Incoherencies test
 1593         if (!Config->File_MpegPs_PTS_Begin_IsNearZero && Frame_Count<16)
 1594         {
 1595             if (FrameInfo.PTS<90000 || FrameInfo.PTS>0x200000000LL-90000) // 1 second before and after 
 1596                 Config->File_MpegPs_PTS_Begin_IsNearZero=true;
 1597         }
 1598         if (Config->File_MpegPs_PTS_Begin_IsNearZero && FrameInfo.PTS>0x200000000LL-90000)
 1599             FrameInfo.PTS=0; //TODO: find a better method for synchronizing streams, Hack in case DTS is negative (currently not supported by MI). TODO: negative DTS.
 1600 
 1601         if (Streams[stream_id].Searching_TimeStamp_End)
 1602         {
 1603             if (Streams[stream_id].TimeStamp_End.PTS.TimeStamp==(int64u)-1)
 1604                 Streams[stream_id].TimeStamp_End.PTS.TimeStamp=FrameInfo.PTS;
 1605             #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1606                 if (!FromAribStdB24B37)
 1607             #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1608                     while (FrameInfo.PTS+0x100000000LL<Streams[stream_id].TimeStamp_End.PTS.TimeStamp)
 1609                         FrameInfo.PTS+=0x200000000LL;
 1610             Streams[stream_id].TimeStamp_End.PTS.File_Pos=File_Offset+Buffer_Offset;
 1611             Streams[stream_id].TimeStamp_End.PTS.TimeStamp=FrameInfo.PTS;
 1612         }
 1613         if (Searching_TimeStamp_Start && Streams[stream_id].Searching_TimeStamp_Start)
 1614         {
 1615             Streams[stream_id].TimeStamp_Start.PTS.File_Pos=File_Offset+Buffer_Offset;
 1616             Streams[stream_id].TimeStamp_Start.PTS.TimeStamp=FrameInfo.PTS;
 1617         }
 1618         Element_Info_From_Milliseconds(float64_int64s(((float64)FrameInfo.PTS)/90));
 1619         #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1620             if (!FromAribStdB24B37)
 1621         #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1622                 TS_Set(FrameInfo.PTS, TS_PTS);
 1623         Element_End0();
 1624 
 1625         Element_Begin1("DTS");
 1626         BS_Begin();
 1627         Mark_0();
 1628         Mark_0();
 1629         Mark_0();
 1630         Mark_1_NoTrustError(); //Is "0" in one sample
 1631         Get_S1 ( 3, DTS_32,                                     "DTS_32");
 1632         Mark_1();
 1633         Get_S2 (15, DTS_29,                                     "DTS_29");
 1634         Mark_1();
 1635         Get_S2 (15, DTS_14,                                     "DTS_14");
 1636         Mark_1();
 1637         BS_End();
 1638 
 1639         //Filling
 1640         FrameInfo.DTS=(((int64u)DTS_32)<<30)
 1641                     | (((int64u)DTS_29)<<15)
 1642                     | (((int64u)DTS_14));
 1643 
 1644         //Incoherencies test
 1645         if (!Config->File_MpegPs_PTS_Begin_IsNearZero && Frame_Count<16)
 1646         {
 1647             if (FrameInfo.DTS<90000 || FrameInfo.DTS>0x200000000LL-90000) // 1 second before and after 
 1648                 Config->File_MpegPs_PTS_Begin_IsNearZero=true;
 1649         }
 1650         if (Config->File_MpegPs_PTS_Begin_IsNearZero && FrameInfo.DTS>0x200000000LL-90000)
 1651             FrameInfo.DTS=0; //TODO: find a better method for synchronizing streams, Hack in case DTS is negative (currently not supported by MI). TODO: negative DTS.
 1652 
 1653         if (Streams[stream_id].Searching_TimeStamp_End)
 1654         {
 1655             if (Streams[stream_id].TimeStamp_End.DTS.TimeStamp==(int64u)-1)
 1656                 Streams[stream_id].TimeStamp_End.DTS.TimeStamp=FrameInfo.DTS;
 1657             #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1658                 if (!FromAribStdB24B37)
 1659             #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1660                     while (FrameInfo.DTS+0x100000000LL<Streams[stream_id].TimeStamp_End.DTS.TimeStamp)
 1661                         FrameInfo.DTS+=0x200000000LL;
 1662             Streams[stream_id].TimeStamp_End.DTS.File_Pos=File_Offset+Buffer_Offset;
 1663             Streams[stream_id].TimeStamp_End.DTS.TimeStamp=FrameInfo.DTS;
 1664         }
 1665         if (Searching_TimeStamp_Start && Streams[stream_id].Searching_TimeStamp_Start)
 1666         {
 1667             Streams[stream_id].TimeStamp_Start.DTS.TimeStamp=FrameInfo.DTS;
 1668             Streams[stream_id].Searching_TimeStamp_Start=false;
 1669         }
 1670         Element_Info_From_Milliseconds(float64_int64s(((float64)FrameInfo.DTS)/90));
 1671         #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1672             if (!FromAribStdB24B37)
 1673         #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1674                 TS_Set(FrameInfo.DTS, TS_DTS);
 1675         Element_End0();
 1676     }
 1677     else
 1678     {
 1679         BS_Begin();
 1680         Mark_0();
 1681         Mark_0();
 1682         Mark_0();
 1683         Mark_0();
 1684         Mark_1();
 1685         Mark_1();
 1686         Mark_1();
 1687         Mark_1();
 1688         BS_End();
 1689 
 1690         if (!FromTS)
 1691             PES_FirstByte_Value=false;
 1692     }
 1693 }
 1694 
 1695 //---------------------------------------------------------------------------
 1696 // Packet header data - MPEG-2
 1697 void File_MpegPs::Header_Parse_PES_packet_MPEG2(int8u stream_id)
 1698 {
 1699     //Parsing
 1700     int8u PTS_DTS_flags, PES_header_data_length;
 1701     bool ESCR_flag, ES_rate_flag, DSM_trick_mode_flag, additional_copy_info_flag, PES_CRC_flag, PES_extension_flag;
 1702     #if MEDIAINFO_TRACE
 1703     if (Trace_Activated)
 1704     {
 1705         BS_Begin();
 1706         Mark_1_NoTrustError();
 1707         Mark_0_NoTrustError();
 1708         Skip_S1(2,                                                  "PES_scrambling_control");
 1709         Skip_SB(                                                    "PES_priority");
 1710         Skip_SB(                                                    "data_alignment_indicator");
 1711         Skip_SB(                                                    "copyright");
 1712         Skip_SB(                                                    "original_or_copy");
 1713         Get_S1 (2, PTS_DTS_flags,                                   "PTS_DTS_flags");
 1714         Get_SB (ESCR_flag,                                          "ESCR_flag");
 1715         Get_SB (ES_rate_flag,                                       "ES_rate_flag");
 1716         Get_SB (DSM_trick_mode_flag,                                "DSM_trick_mode_flag");
 1717         Get_SB (additional_copy_info_flag,                          "additional_copy_info_flag");
 1718         Get_SB (PES_CRC_flag,                                       "PES_CRC_flag");
 1719         Get_SB (PES_extension_flag,                                 "PES_extension_flag");
 1720         BS_End();
 1721         Get_B1 (PES_header_data_length,                             "PES_header_data_length");
 1722     }
 1723     else
 1724     {
 1725     #endif //MEDIAINFO_TRACE
 1726         if (Element_Offset+3>=Element_Size)
 1727         {
 1728             Trusted_IsNot("");
 1729             return;
 1730         }
 1731         size_t Buffer_Pos_Flags=Buffer_Offset+(size_t)Element_Offset;
 1732         if ((Buffer[Buffer_Pos_Flags]&0xC0)!=0x80) //bit 6 and 7 are 01
 1733         {
 1734             Element_DoNotTrust(""); //Mark bits are wrong
 1735             return;
 1736         }
 1737         Buffer_Pos_Flags++;
 1738         PTS_DTS_flags               =Buffer[Buffer_Pos_Flags]>>6;
 1739         ESCR_flag                   =Buffer[Buffer_Pos_Flags]&0x20?true:false;
 1740         ES_rate_flag                =Buffer[Buffer_Pos_Flags]&0x10?true:false;
 1741         DSM_trick_mode_flag         =Buffer[Buffer_Pos_Flags]&0x08?true:false;
 1742         additional_copy_info_flag   =Buffer[Buffer_Pos_Flags]&0x04?true:false;
 1743         PES_CRC_flag                =Buffer[Buffer_Pos_Flags]&0x02?true:false;
 1744         PES_extension_flag          =Buffer[Buffer_Pos_Flags]&0x01?true:false;
 1745         Buffer_Pos_Flags++;
 1746         PES_header_data_length      =Buffer[Buffer_Pos_Flags];
 1747         Element_Offset+=3;
 1748     #if MEDIAINFO_TRACE
 1749     }
 1750     #endif //MEDIAINFO_TRACE
 1751     int64u Element_Pos_After_Data=Element_Offset+PES_header_data_length;
 1752     if (Element_Pos_After_Data>Element_Size)
 1753     {
 1754         Element_WaitForMoreData();
 1755         return;
 1756     }
 1757 
 1758     //Options
 1759     if (PTS_DTS_flags==0x2)
 1760     {
 1761         #if MEDIAINFO_TRACE
 1762         if (Trace_Activated)
 1763         {
 1764             int16u PTS_29, PTS_14;
 1765             int8u  PTS_32;
 1766             Element_Begin1("PTS_DTS_flags");
 1767             Element_Begin1("PTS");
 1768             BS_Begin();
 1769             Mark_0();
 1770             Mark_0();
 1771             Mark_1_NoTrustError(); //Is "0" in one sample
 1772             Mark_0_NoTrustError(); //Is "1" in one sample
 1773             Get_S1 ( 3, PTS_32,                                     "PTS_32");
 1774             Mark_1();
 1775             Get_S2 (15, PTS_29,                                     "PTS_29");
 1776             Mark_1();
 1777             Get_S2 (15, PTS_14,                                     "PTS_14");
 1778             Mark_1();
 1779             BS_End();
 1780             #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1781                 if (!FromAribStdB24B37)
 1782             #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1783                     FrameInfo.PTS=(((int64u)PTS_32)<<30)
 1784                                 | (((int64u)PTS_29)<<15)
 1785                                 | (((int64u)PTS_14));
 1786             Element_Info_From_Milliseconds(float64_int64s(((float64)FrameInfo.PTS)/90));
 1787             Element_End0();
 1788             Element_End0();
 1789         }
 1790         else
 1791         {
 1792         #endif //MEDIAINFO_TRACE
 1793             if (Element_Offset+5>Element_Size)
 1794             {
 1795                 Element_WaitForMoreData();
 1796                 return;
 1797             }
 1798             size_t Buffer_Pos=Buffer_Offset+(size_t)Element_Offset;
 1799             if ((Buffer[Buffer_Pos  ]&0xC1)!=0x01 //bit 5 and 4 are not tested because of one sample with wrong mark bits
 1800              || (Buffer[Buffer_Pos+2]&0x01)!=0x01
 1801              || (Buffer[Buffer_Pos+4]&0x01)!=0x01)
 1802             {
 1803                 Element_DoNotTrust(""); //Mark bits are wrong
 1804                 return;
 1805             }
 1806             #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1807                 if (!FromAribStdB24B37)
 1808             #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1809                     FrameInfo.PTS=                                  ((((int64u)Buffer[Buffer_Pos  ]&0x0E))<<29)
 1810                       | ( ((int64u)Buffer[Buffer_Pos+1]      )<<22)|((((int64u)Buffer[Buffer_Pos+2]&0xFE))<<14)
 1811                       | ( ((int64u)Buffer[Buffer_Pos+3]      )<< 7)|((((int64u)Buffer[Buffer_Pos+4]&0xFE))>> 1);
 1812             Element_Offset+=5;
 1813         #if MEDIAINFO_TRACE
 1814         }
 1815         #endif //MEDIAINFO_TRACE
 1816 
 1817         //Incoherencies test
 1818         if (!Config->File_MpegPs_PTS_Begin_IsNearZero && Frame_Count<16)
 1819         {
 1820             if (FrameInfo.PTS<90000 || FrameInfo.PTS>0x200000000LL-90000) // 1 second before and after 
 1821                 Config->File_MpegPs_PTS_Begin_IsNearZero=true;
 1822         }
 1823         if (Config->File_MpegPs_PTS_Begin_IsNearZero && FrameInfo.PTS>0x200000000LL-90000)
 1824             FrameInfo.PTS=0; //TODO: find a better method for synchronizing streams, Hack in case DTS is negative (currently not supported by MI). TODO: negative DTS.
 1825 
 1826         //Filling
 1827         if (Streams[stream_id].Searching_TimeStamp_End)
 1828         {
 1829             if (Streams[stream_id].TimeStamp_End.PTS.TimeStamp==(int64u)-1)
 1830                 Streams[stream_id].TimeStamp_End.PTS.TimeStamp=FrameInfo.PTS;
 1831             #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1832                 if (!FromAribStdB24B37)
 1833             #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1834                     while (FrameInfo.PTS+0x100000000LL<Streams[stream_id].TimeStamp_End.PTS.TimeStamp)
 1835                         FrameInfo.PTS+=0x200000000LL;
 1836             Streams[stream_id].TimeStamp_End.DTS.File_Pos=Streams[stream_id].TimeStamp_End.PTS.File_Pos=File_Offset+Buffer_Offset;
 1837             Streams[stream_id].TimeStamp_End.DTS.TimeStamp=Streams[stream_id].TimeStamp_End.PTS.TimeStamp=FrameInfo.PTS;
 1838         }
 1839         if (Searching_TimeStamp_Start && Streams[stream_id].Searching_TimeStamp_Start)
 1840         {
 1841             Streams[stream_id].TimeStamp_Start.DTS.File_Pos=Streams[stream_id].TimeStamp_Start.PTS.File_Pos=File_Offset+Buffer_Offset;
 1842             Streams[stream_id].TimeStamp_Start.DTS.TimeStamp=Streams[stream_id].TimeStamp_Start.PTS.TimeStamp=FrameInfo.PTS;
 1843             Streams[stream_id].Searching_TimeStamp_Start=false;
 1844         }
 1845         #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1846             if (!FromAribStdB24B37)
 1847         #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1848                 TS_Set(FrameInfo.PTS);
 1849         HasTimeStamps=true;
 1850     }
 1851     else if (PTS_DTS_flags==0x3)
 1852     {
 1853         size_t Buffer_Pos;
 1854         #if MEDIAINFO_TRACE
 1855         int16u PTS_29, PTS_14, DTS_29, DTS_14;
 1856         int8u  PTS_32, DTS_32;
 1857         if (Trace_Activated)
 1858         {
 1859             Element_Begin1("PTS_DTS_flags");
 1860             Element_Begin1("PTS");
 1861             BS_Begin();
 1862             Mark_0();
 1863             Mark_0();
 1864             Mark_1_NoTrustError(); //Is "0" in one sample
 1865             Mark_1_NoTrustError(); //Is "0" in one sample
 1866             Get_S1 ( 3, PTS_32,                                     "PTS_32");
 1867             Mark_1();
 1868             Get_S2 (15, PTS_29,                                     "PTS_29");
 1869             Mark_1();
 1870             Get_S2 (15, PTS_14,                                     "PTS_14");
 1871             Mark_1();
 1872             BS_End();
 1873             #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1874                 if (!FromAribStdB24B37)
 1875             #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1876                     FrameInfo.PTS=(((int64u)PTS_32)<<30)
 1877                                 | (((int64u)PTS_29)<<15)
 1878                                 | (((int64u)PTS_14));
 1879             Element_Info_From_Milliseconds(float64_int64s(((float64)FrameInfo.PTS)/90));
 1880             Element_End0();
 1881         }
 1882         else
 1883         {
 1884         #endif //MEDIAINFO_TRACE
 1885             if (Element_Offset+5>Element_Size)
 1886             {
 1887                 Element_WaitForMoreData();
 1888                 return;
 1889             }
 1890             Buffer_Pos=Buffer_Offset+(size_t)Element_Offset;
 1891             if ((Buffer[Buffer_Pos  ]&0xC1)!=0x01 //bit 5 and 4 are not tested because of one sample with wrong mark bits
 1892              || (Buffer[Buffer_Pos+2]&0x01)!=0x01
 1893              || (Buffer[Buffer_Pos+4]&0x01)!=0x01)
 1894             {
 1895                 Element_DoNotTrust(""); //Mark bits are wrong
 1896                 return;
 1897             }
 1898             #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1899                 if (!FromAribStdB24B37)
 1900             #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1901                     FrameInfo.PTS=                                  ((((int64u)Buffer[Buffer_Pos  ]&0x0E))<<29)
 1902                       | ( ((int64u)Buffer[Buffer_Pos+1]      )<<22)|((((int64u)Buffer[Buffer_Pos+2]&0xFE))<<14)
 1903                       | ( ((int64u)Buffer[Buffer_Pos+3]      )<< 7)|((((int64u)Buffer[Buffer_Pos+4]&0xFE))>> 1);
 1904             Element_Offset+=5;
 1905         #if MEDIAINFO_TRACE
 1906         }
 1907         #endif //MEDIAINFO_TRACE
 1908 
 1909         //Incoherencies test
 1910         if (!Config->File_MpegPs_PTS_Begin_IsNearZero && Frame_Count<16)
 1911         {
 1912             if (FrameInfo.PTS<90000 || FrameInfo.PTS>0x200000000LL-90000) // 1 second before and after 
 1913                 Config->File_MpegPs_PTS_Begin_IsNearZero=true;
 1914         }
 1915         if (Config->File_MpegPs_PTS_Begin_IsNearZero && FrameInfo.PTS>0x200000000LL-90000)
 1916             FrameInfo.PTS=0; //TODO: find a better method for synchronizing streams, Hack in case DTS is negative (currently not supported by MI). TODO: negative DTS.
 1917 
 1918         //Filling
 1919         if (Streams[stream_id].Searching_TimeStamp_End)
 1920         {
 1921             if (Streams[stream_id].TimeStamp_End.PTS.TimeStamp==(int64u)-1)
 1922                 Streams[stream_id].TimeStamp_End.PTS.TimeStamp=FrameInfo.PTS;
 1923             #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1924                 if (!FromAribStdB24B37)
 1925             #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1926                     while (FrameInfo.PTS+0x100000000LL<Streams[stream_id].TimeStamp_End.PTS.TimeStamp)
 1927                         FrameInfo.PTS+=0x200000000LL;
 1928             Streams[stream_id].TimeStamp_End.PTS.File_Pos=File_Offset+Buffer_Offset;
 1929             Streams[stream_id].TimeStamp_End.PTS.TimeStamp=FrameInfo.PTS;
 1930         }
 1931         if (Searching_TimeStamp_Start && Streams[stream_id].Searching_TimeStamp_Start)
 1932         {
 1933             Streams[stream_id].TimeStamp_Start.PTS.File_Pos=File_Offset+Buffer_Offset;
 1934             Streams[stream_id].TimeStamp_Start.PTS.TimeStamp=FrameInfo.PTS;
 1935             //Streams[stream_id].Searching_TimeStamp_Start=false; //Done with DTS
 1936         }
 1937         #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1938             if (!FromAribStdB24B37)
 1939         #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
 1940                 TS_Set(FrameInfo.PTS, TS_PTS);
 1941 
 1942         #if MEDIAINFO_TRACE
 1943         if (Trace_Activated)
 1944         {
 1945             Element_Begin1("DTS");
 1946             BS_Begin();
 1947             Mark_0();
 1948             Mark_0();
 1949             Mark_0_NoTrustError(); //Is "1" in one sample
 1950             Mark_1_NoTrustError(); //Is "0" in one sample
 1951             Get_S1 ( 3, DTS_32,                                     "DTS_32");
 1952             Mark_1();
 1953             Get_S2 (15, DTS_29,                                     "DTS_29");
 1954             Mark_1();
 1955             Get_S2 (15, DTS_14,                                     "DTS_14");
 1956             Mark_1();
 1957             BS_End();
 1958             FrameInfo.DTS=(((int64u)DTS_32)<<30)
 1959                         | (((int64u)DTS_29)<<15)
 1960                         | (((int64u)DTS_14));
 1961             if (Frame_Count<16 &&FrameInfo.DTS>=0x100000000LL) //Hack in case DTS is negative (currently not supported by MI). TODO: negative DTS.
 1962                 FrameInfo.DTS=0;
 1963             Element_Info_From_Milliseconds(float64_int64s(((float64)FrameInfo.DTS)/90));
 1964             Element_End0();
 1965             Element_End0();
 1966         }
 1967         else
 1968         {
 1969         #endif //MEDIAINFO_TRACE
 1970             if (Element_Offset+5>Element_Size)
 1971             {
 1972                 Element_WaitForMoreData();
 1973                 return;
 1974             }
 1975             Buffer_Pos=Buffer_Offset+(size_t)Element_Offset;
 1976             if ((Buffer[Buffer_Pos  ]&0xC1)!=0x01 //bit 5 and 4 are not tested because of one sample with wrong mark bits
 1977              || (Buffer[Buffer_Pos+2]&0x01)!=0x01
 1978              || (Buffer[Buffer_Pos+4]&0x01)!=0x01)
 1979             {
 1980                 Element_DoNotTrust(""); //Mark bits are wrong
 1981                 return;
 1982             }
 1983             FrameInfo.DTS=                                  ((((int64u)Buffer[Buffer_Pos  ]&0x0E))<<29)
 1984               | ( ((int64u)Buffer[Buffer_Pos+1]      )<<22)|((((int64u)Buffer[Buffer_Pos+2]&0xFE))<<14)
 1985               | ( ((int64u)Buffer[Buffer_Pos+3]      )<< 7)|((((int64u)Buffer[Buffer_Pos+4]&0xFE))>> 1);
 1986             Element_Offset+=5;
 1987             if (Frame_Count<16 &&FrameInfo.DTS>=0x100000000LL) //Hack in case DTS is negative (currently not supported by MI). TODO: negative DTS.
 1988                 FrameInfo.DTS=0;
 1989         #if MEDIAINFO_TRACE
 1990         }
 1991         #endif //MEDIAINFO_TRACE
 1992 
 1993         //Incoherencies test
 1994         if (!Config->File_MpegPs_PTS_Begin_IsNearZero && Frame_Count<16)
 1995         {
 1996             if (FrameInfo.DTS<90000 || FrameInfo.DTS>0x200000000LL-90000) // 1 second before and after 
 1997                 Config->File_MpegPs_PTS_Begin_IsNearZero=true;
 1998         }
 1999         if (Config->File_MpegPs_PTS_Begin_IsNearZero && FrameInfo.DTS>0x200000000LL-90000)
 2000             FrameInfo.DTS=0; //TODO: find a better method for synchronizing streams, Hack in case DTS is negative (currently not supported by MI). TODO: negative DTS.
 2001 
 2002         //Filling
 2003         if (Streams[stream_id].Searching_TimeStamp_End)
 2004         {
 2005             if (Streams[stream_id].TimeStamp_End.DTS.TimeStamp==(int64u)-1)
 2006                 Streams[stream_id].TimeStamp_End.DTS.TimeStamp=FrameInfo.DTS;
 2007             #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 2008                 if (!FromAribStdB24B37)
 2009             #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
 2010                     while (FrameInfo.DTS+0x100000000LL<Streams[stream_id].TimeStamp_End.DTS.TimeStamp)
 2011                         FrameInfo.DTS+=0x200000000LL;
 2012             Streams[stream_id].TimeStamp_End.DTS.File_Pos=File_Offset+Buffer_Offset;
 2013             Streams[stream_id].TimeStamp_End.DTS.TimeStamp=FrameInfo.DTS;
 2014         }
 2015         if (Searching_TimeStamp_Start && Streams[stream_id].Searching_TimeStamp_Start)
 2016         {
 2017             Streams[stream_id].TimeStamp_Start.DTS.TimeStamp=FrameInfo.DTS;
 2018             Streams[stream_id].Searching_TimeStamp_Start=false;
 2019         }
 2020         #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 2021             if (!FromAribStdB24B37)
 2022         #endif //defined(MEDIAINFO_ARIBSTDB24B37_YES)
 2023                 TS_Set(FrameInfo.DTS, TS_DTS);
 2024         HasTimeStamps=true;
 2025     }
 2026     else if (!FromTS)
 2027         PES_FirstByte_Value=false;
 2028     if (ESCR_flag && Element_Offset<Element_Pos_After_Data)
 2029     {
 2030         Element_Begin1("ESCR_flag");
 2031         BS_Begin();
 2032         int16u ESCR_29, ESCR_14, ESCR_extension;
 2033         int8u  ESCR_32;
 2034         Skip_S1( 2,                                             "reserved");
 2035         Get_S1 ( 3, ESCR_32,                                    "PTS_32");
 2036         Mark_1();
 2037         Get_S2 (15, ESCR_29,                                    "PTS_29");
 2038         Mark_1();
 2039         Get_S2 (15, ESCR_14,                                    "PTS_14");
 2040         Mark_1();
 2041         Get_S2 (15, ESCR_extension,                             "ESCR_extension");
 2042         Mark_1();
 2043         BS_End();
 2044         Element_End0();
 2045     }
 2046     if (ES_rate_flag && Element_Offset<Element_Pos_After_Data)
 2047     {
 2048         Element_Begin1("ES_rate_flag");
 2049         BS_Begin();
 2050         int32u ES_rate;
 2051         Mark_1();
 2052         Get_S3 (22, ES_rate,                                    "ES_rate");
 2053         Mark_1();
 2054         BS_End();
 2055         Element_End0();
 2056     }
 2057     if (DSM_trick_mode_flag && Element_Offset<Element_Pos_After_Data)
 2058     {
 2059         Element_Begin1("DSM_trick_mode_flag");
 2060         BS_Begin();
 2061         int8u trick_mode_control;
 2062         Get_S1 (3, trick_mode_control,                         "trick_mode_control"); Param_Info1(MpegPs_trick_mode_control_values[trick_mode_control]);
 2063         switch (trick_mode_control)
 2064         {
 2065             case 0 :{ //fast_forward
 2066                         Skip_S1(2,                              "field_id");
 2067                         Skip_SB(                                "intra_slice_refresh");
 2068                         Skip_S1(2,                              "frequency_truncation");
 2069                     }
 2070                     break;
 2071             case 1 :{ //slow_motion
 2072                         int8u rep_cntrl;
 2073                         Get_S1 (5, rep_cntrl,                   "rep_cntrl");
 2074                     }
 2075                     break;
 2076             case 2 :{ //freeze_frame
 2077                         Skip_S1(2,                              "field_id");
 2078                         Skip_S1(3,                              "reserved");
 2079                     }
 2080                     break;
 2081             case 3 :{ //fast_reverse
 2082                         Skip_S1(2,                              "field_id");
 2083                         Skip_SB(                                "intra_slice_refresh");
 2084                         Skip_S1(2,                              "frequency_truncation");
 2085                     }
 2086                     break;
 2087             case 4 :{ //slow_reverse
 2088                         int8u rep_cntrl;
 2089                         Get_S1 (5, rep_cntrl,                   "rep_cntrl");
 2090                     }
 2091                     break;
 2092             default:{
 2093                         Skip_S1(5,                              "reserved");
 2094                     }
 2095         }
 2096         BS_End();
 2097         Element_End0();
 2098     }
 2099     if (additional_copy_info_flag && Element_Offset<Element_Pos_After_Data)
 2100     {
 2101         Element_Begin1("additional_copy_info_flag");
 2102         BS_Begin();
 2103         Mark_1();
 2104         Skip_S1(7,                                              "additional_copy_info");
 2105         BS_End();
 2106         Element_End0();
 2107     }
 2108     if (PES_CRC_flag && Element_Offset<Element_Pos_After_Data)
 2109     {
 2110         Element_Begin1("PES_CRC_flag");
 2111         Skip_B2(                                                "previous_PES_packet_CRC");
 2112         Element_End0();
 2113     }
 2114     if (PES_extension_flag && Element_Offset<Element_Pos_After_Data)
 2115     {
 2116         bool PES_private_data_flag=false, pack_header_field_flag=false, program_packet_sequence_counter_flag=false, p_STD_buffer_flag=false, PES_extension_flag_2=false;
 2117         Element_Begin1("PES_extension_flag");
 2118         BS_Begin();
 2119         Get_SB (PES_private_data_flag,                          "PES_private_data_flag");
 2120         Get_SB (pack_header_field_flag,                         "pack_header_field_flag");
 2121         Get_SB (program_packet_sequence_counter_flag,           "program_packet_sequence_counter_flag");
 2122         Get_SB (p_STD_buffer_flag,                              "P-STD_buffer_flag");
 2123         Skip_S1(3,                                              "reserved");
 2124         Get_SB (PES_extension_flag_2,                           "PES_extension_flag_2");
 2125         BS_End();
 2126 
 2127         //Integrity test
 2128         if (Element_Offset+(PES_private_data_flag?16:0)+(pack_header_field_flag?1:0)+(program_packet_sequence_counter_flag?2:0)+(p_STD_buffer_flag?2:0)+(PES_extension_flag_2?2:0)>Element_Pos_After_Data)
 2129         {
 2130             //There is a problem
 2131             PES_private_data_flag=false;
 2132             pack_header_field_flag=false;
 2133             program_packet_sequence_counter_flag=false;
 2134             p_STD_buffer_flag=false;
 2135             PES_extension_flag_2=false;
 2136         }
 2137 
 2138         if (PES_private_data_flag)
 2139         {
 2140             Element_Begin1("PES_private_data");
 2141             int32u Code;
 2142             Peek_B4(Code);
 2143             if (Code==0x43434953) // "CCIS"
 2144             {
 2145                 if (Streams_Private1[private_stream_1_ID].Parsers.size()>1)
 2146                 {
 2147                     //Should not happen, this is only in case the previous packet was without CCIS
 2148                     Streams_Private1[private_stream_1_ID].Parsers.clear();
 2149                     Streams_Private1[private_stream_1_ID].StreamRegistration_Count=0;
 2150                 }
 2151                 if (!Streams_Private1[private_stream_1_ID].StreamRegistration_Count)
 2152                 {
 2153                     Streams_Private1[private_stream_1_ID].Parsers.push_back(ChooseParser_AribStdB24B37(true));
 2154                     Open_Buffer_Init(Streams_Private1[private_stream_1_ID].Parsers[0]);
 2155                     Streams_Private1[private_stream_1_ID].StreamRegistration_Count++;
 2156                 }
 2157 #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 2158                 if (Streams_Private1[private_stream_1_ID].Parsers.size()==1)
 2159                 {
 2160                     File_AribStdB24B37* Parser=(File_AribStdB24B37*)Streams_Private1[private_stream_1_ID].Parsers[0];
 2161                     Parser->ParseCcis=true;
 2162                     Open_Buffer_Continue(Parser, Buffer+Buffer_Offset+(size_t)Element_Offset, 16);
 2163                 }
 2164                 else
 2165 #endif
 2166                     Skip_B16(                                   "PES_private_data");
 2167            }
 2168             else
 2169                 Skip_B16(                                       "PES_private_data");
 2170             Element_End0();
 2171         }
 2172         if (pack_header_field_flag)
 2173         {
 2174             Element_Begin1("pack_header_field_flag");
 2175             int8u pack_field_length;
 2176             Get_B1 (pack_field_length,                          "pack_field_length");
 2177             Skip_XX(pack_field_length,                          "pack_header");
 2178             Element_End0();
 2179         }
 2180         if (program_packet_sequence_counter_flag)
 2181         {
 2182             Element_Begin1("program_packet_sequence_counter_flag");
 2183             int8u   program_packet_sequence_counter, original_stuff_length;
 2184             bool    MPEG1_MPEG2_identifier;
 2185             BS_Begin();
 2186             Mark_1();
 2187             Get_S1 (7, program_packet_sequence_counter,         "program_packet_sequence_counter");
 2188             Mark_1();
 2189             Get_SB (   MPEG1_MPEG2_identifier,                  "MPEG1_MPEG2_identifier");
 2190             Get_S1 (6, original_stuff_length,                   "original_stuff_length");
 2191             BS_End();
 2192             Element_End0();
 2193         }
 2194         if (p_STD_buffer_flag)
 2195         {
 2196             Element_Begin1("p_STD_buffer_flag");
 2197             bool P_STD_buffer_scale;
 2198             BS_Begin();
 2199             Mark_0();
 2200             Skip_SB(                                            "Should be 1"); //But I saw a file with "0"
 2201             Get_SB (    P_STD_buffer_scale,                     "P-STD_buffer_scale");
 2202             Skip_S2(13,                                         "P-STD_buffer_size");
 2203             BS_End();
 2204             Element_End0();
 2205         }
 2206         if (PES_extension_flag_2)
 2207         {
 2208             Element_Begin1("PES_extension_flag_2");
 2209             int8u PES_extension_field_length;
 2210             bool stream_id_extension_flag;
 2211             BS_Begin();
 2212             Mark_1();
 2213             Get_S1 (7, PES_extension_field_length,              "PES_extension_field_length");
 2214             Get_SB (stream_id_extension_flag,                   "stream_id_extension_flag");
 2215             if (stream_id_extension_flag==0) //This should be limited to stream_id_extension_flag==0, but I found a file with stream_id_extension_flag=1 and a real code...
 2216             {
 2217                 Get_S1 (7, stream_id_extension,                 "stream_id_extension"); Param_Info1(MpegPs_stream_id_extension(stream_id_extension));
 2218             }
 2219             BS_End();
 2220             if (PES_extension_field_length-1>0)
 2221                 Skip_XX(PES_extension_field_length-1,           "reserved");
 2222             Element_End0();
 2223         }
 2224         Element_End0();
 2225     }
 2226     if (Element_Pos_After_Data>Element_Offset)
 2227         Skip_XX(Element_Pos_After_Data-Element_Offset,          "stuffing_bytes");
 2228 }
 2229 
 2230 //---------------------------------------------------------------------------
 2231 void File_MpegPs::Data_Parse()
 2232 {
 2233     //Counting
 2234     Frame_Count++;
 2235 
 2236     //Needed?
 2237     if (!Streams[stream_id].Searching_Payload)
 2238     {
 2239         Skip_XX(Element_Size,                                   "data");
 2240         Element_DoNotShow();
 2241         return;
 2242     }
 2243 
 2244     //From TS
 2245     if (FromTS && !Status[IsAccepted])
 2246     {
 2247         Data_Accept("MPEG-PS");
 2248         if (!IsSub)
 2249             Fill(Stream_General, 0, General_Format, "MPEG-PS");
 2250     }
 2251 
 2252     //Parsing
 2253     switch (stream_id)
 2254     {
 2255         case 0xB9 : MPEG_program_end(); break;
 2256         case 0xBA : pack_start(); break;
 2257         case 0xBB : system_header_start(); break;
 2258         case 0xBC : program_stream_map(); break;
 2259         case 0xBD : private_stream_1(); break;
 2260         case 0xBE : padding_stream(); break;
 2261         case 0xBF : private_stream_2(); break;
 2262         case 0xF0 : Element_Name("ECM_Stream"); Skip_XX(Element_Size, "Data"); break;
 2263         case 0xF1 : Element_Name("EMM_Stream"); Skip_XX(Element_Size, "Data"); break;
 2264         case 0xF2 : Element_Name("DSMCC_stream"); Skip_XX(Element_Size, "Data"); break;
 2265         case 0xF3 : Element_Name("ISO/IEC_13522_stream"); Skip_XX(Element_Size, "Data"); break;
 2266         case 0xF4 : Element_Name("ITU-T Rec. H.222.1 type A"); Skip_XX(Element_Size, "Data"); break;
 2267         case 0xF5 : Element_Name("ITU-T Rec. H.222.1 type B"); Skip_XX(Element_Size, "Data"); break;
 2268         case 0xF6 : Element_Name("ITU-T Rec. H.222.1 type C"); Skip_XX(Element_Size, "Data"); break;
 2269         case 0xF7 : Element_Name("ITU-T Rec. H.222.1 type D"); Skip_XX(Element_Size, "Data"); break;
 2270         case 0xF8 : Element_Name("ITU-T Rec. H.222.1 type E"); Skip_XX(Element_Size, "Data"); break;
 2271         case 0xF9 : Element_Name("ancillary_stream"); Skip_XX(Element_Size, "Data"); break;
 2272         case 0xFA : SL_packetized_stream(); break;
 2273         case 0xFB : Element_Name("FlexMux_stream"); Skip_XX(Element_Size, "Data"); break;
 2274         case 0xFC : Element_Name("descriptive data stream"); Skip_XX(Element_Size, "Data"); break;
 2275         case 0xFD : extension_stream(); break;
 2276         case 0xFE : video_stream(); break;
 2277         case 0xFF : Element_Name("program_stream_directory"); Skip_XX(Element_Size, "Data"); break;
 2278         default:
 2279                  if ((stream_id&0xE0)==0xC0) audio_stream();
 2280             else if ((stream_id&0xF0)==0xE0) video_stream();
 2281             else
 2282                 Trusted_IsNot("Unattended element!");
 2283     }
 2284 
 2285     #if MEDIAINFO_EVENTS
 2286         PES_FirstByte_IsAvailable=false;
 2287     #endif //MEDIAINFO_EVENTS
 2288 }
 2289 
 2290 //---------------------------------------------------------------------------
 2291 //Jumping to the last DTS if needed
 2292 bool File_MpegPs::BookMark_Needed()
 2293 {
 2294     if (IsSub || Streams.empty() || Config->ParseSpeed>=1.0)
 2295         return false;
 2296 
 2297     int64u ToJump=(int64u)-1;
 2298     for (size_t StreamID=0; StreamID<0x100; StreamID++)
 2299     {
 2300         //End timestamp is out of date
 2301         if (Streams[StreamID].TimeStamp_End.PTS.File_Pos!=(int64u)-1)
 2302         {
 2303             if (Streams[StreamID].TimeStamp_End.PTS.File_Pos<ToJump)
 2304                 ToJump=Streams[StreamID].TimeStamp_End.PTS.File_Pos;
 2305             Streams[StreamID].Searching_Payload=true;
 2306         }
 2307         if (Streams[StreamID].TimeStamp_End.DTS.File_Pos!=(int64u)-1)
 2308         {
 2309             if (Streams[StreamID].TimeStamp_End.DTS.File_Pos<ToJump)
 2310                 ToJump=Streams[StreamID].TimeStamp_End.DTS.File_Pos;
 2311             Streams[StreamID].Searching_Payload=true;
 2312         }
 2313         if (Streams_Private1[StreamID].TimeStamp_End.PTS.File_Pos!=(int64u)-1)
 2314         {
 2315             if (Streams_Private1[StreamID].TimeStamp_End.PTS.File_Pos<ToJump)
 2316                 ToJump=Streams_Private1[StreamID].TimeStamp_End.PTS.File_Pos;
 2317             Streams_Private1[StreamID].Searching_Payload=true;
 2318         }
 2319         if (Streams_Private1[StreamID].TimeStamp_End.DTS.File_Pos!=(int64u)-1)
 2320         {
 2321             if (Streams_Private1[StreamID].TimeStamp_End.DTS.File_Pos<ToJump)
 2322                 ToJump=Streams_Private1[StreamID].TimeStamp_End.DTS.File_Pos;
 2323             Streams_Private1[StreamID].Searching_Payload=true;
 2324         }
 2325         if (Streams_Extension[StreamID].TimeStamp_End.PTS.File_Pos!=(int64u)-1)
 2326         {
 2327             if (Streams_Extension[StreamID].TimeStamp_End.PTS.File_Pos<ToJump)
 2328                 ToJump=Streams_Extension[StreamID].TimeStamp_End.PTS.File_Pos;
 2329             Streams_Extension[StreamID].Searching_Payload=true;
 2330         }
 2331         if (Streams_Extension[StreamID].TimeStamp_End.DTS.File_Pos!=(int64u)-1)
 2332         {
 2333             if (Streams_Extension[StreamID].TimeStamp_End.DTS.File_Pos<ToJump)
 2334                 ToJump=Streams_Extension[StreamID].TimeStamp_End.DTS.File_Pos;
 2335             Streams_Extension[StreamID].Searching_Payload=true;
 2336         }
 2337     }
 2338 
 2339     return false;
 2340 }
 2341 
 2342 //***************************************************************************
 2343 // Elements
 2344 //***************************************************************************
 2345 
 2346 //---------------------------------------------------------------------------
 2347 // Packet "B9"
 2348 void File_MpegPs::MPEG_program_end()
 2349 {
 2350     Element_Name("MPEG_program_end");
 2351 }
 2352 
 2353 //---------------------------------------------------------------------------
 2354 // Packet "BA"
 2355 void File_MpegPs::pack_start()
 2356 {
 2357     Element_Name("pack_start");
 2358 
 2359     //Parsing
 2360     int16u SysClock_29, SysClock_14;
 2361     int8u Version, SysClock_32;
 2362     size_t Buffer_Pos=Buffer_Offset+(size_t)Element_Offset;
 2363     #if MEDIAINFO_TRACE
 2364     if (Trace_Activated)
 2365     {
 2366         //Parsing
 2367         BS_Begin();
 2368         Peek_S1( 2, Version);
 2369     }
 2370     else
 2371     {
 2372     #endif //MEDIAINFO_TRACE
 2373         //Parsing
 2374         Version=Buffer[Buffer_Pos]>>6;
 2375     #if MEDIAINFO_TRACE
 2376     }
 2377     #endif //MEDIAINFO_TRACE
 2378     if (Version==1)
 2379     {
 2380         //MPEG-2
 2381         #if MEDIAINFO_TRACE
 2382         if (Trace_Activated)
 2383         {
 2384             //Parsing
 2385             int8u Padding;
 2386             Mark_0();
 2387             Mark_1();
 2388             Get_S1 ( 3, SysClock_32,                                "system_clock_reference_base32");
 2389             Mark_1();
 2390             Get_S2 (15, SysClock_29,                                "system_clock_reference_base29");
 2391             Mark_1();
 2392             Get_S2 (15, SysClock_14,                                "system_clock_reference_base14");
 2393 
 2394             //Filling
 2395             Streams[0xBA].TimeStamp_End.PTS.TimeStamp=(((int64u)SysClock_32)<<30)
 2396                                                     | (((int64u)SysClock_29)<<15)
 2397                                                     | (((int64u)SysClock_14));
 2398             if (Searching_TimeStamp_Start && Streams[0xBA].Searching_TimeStamp_Start)
 2399             {
 2400                 Streams[0xBA].TimeStamp_Start=Streams[0xBA].TimeStamp_End;
 2401                 Streams[0xBA].Searching_TimeStamp_Start=false;
 2402             }
 2403             Param_Info_From_Milliseconds(Streams[0xBA].TimeStamp_End.PTS.TimeStamp/90);
 2404 
 2405             Mark_1();
 2406             Skip_S2( 9,                                             "system_clock_reference_extension");
 2407             Mark_1();
 2408             Get_S3 (22, program_mux_rate,                           "program_mux_rate"); Param_Info2(program_mux_rate*400, " bps");
 2409             Mark_1();
 2410             Mark_1();
 2411             Skip_S1( 5,                                             "reserved");
 2412             Get_S1 ( 3, Padding,                                    "pack_stuffing_length");
 2413             BS_End();
 2414             if (Padding>0)
 2415                 Skip_XX(Padding,                                    "padding");
 2416         }
 2417         else
 2418         {
 2419         #endif //MEDIAINFO_TRACE
 2420             //Parsing
 2421             Streams[0xBA].TimeStamp_End.PTS.TimeStamp=(((int64u)(Buffer[Buffer_Pos    ] & 0x38)) << 30)
 2422                                                     | (((int64u)(Buffer[Buffer_Pos    ] & 0x03)) << 28)
 2423                                                     | (((int64u)(Buffer[Buffer_Pos + 1]       )) << 20)
 2424                                                     | (((int64u)(Buffer[Buffer_Pos + 2] & 0xF8)) << 15)
 2425                                                     | (((int64u)(Buffer[Buffer_Pos + 2] & 0x03)) << 13)
 2426                                                     | (((int64u)(Buffer[Buffer_Pos + 3]       )) <<  5)
 2427                                                     | (((int64u)(Buffer[Buffer_Pos + 4] & 0xF8)) >>  3);
 2428             if (!Status[IsAccepted])
 2429             {
 2430                 program_mux_rate                     =((Buffer[Buffer_Pos+6]     )<<14)
 2431                                                     | ((Buffer[Buffer_Pos+7]     )<< 6)
 2432                                                     | ((Buffer[Buffer_Pos+8]     )>> 2);
 2433             }
 2434             int8u Padding                            =  Buffer[Buffer_Pos+9]&0x07;
 2435             Element_Offset=10+Padding;
 2436         #if MEDIAINFO_TRACE
 2437         }
 2438         #endif //MEDIAINFO_TRACE
 2439     }
 2440     else
 2441     {
 2442         BS_Begin();
 2443         Mark_0();
 2444         Mark_0();
 2445         Mark_1();
 2446         Mark_0();
 2447         Get_S1 ( 3, SysClock_32,                                "system_clock_reference_base32");
 2448         Mark_1();
 2449         Get_S2 (15, SysClock_29,                                "system_clock_reference_base29");
 2450         Mark_1();
 2451         Get_S2 (15, SysClock_14,                                "system_clock_reference_base14");
 2452 
 2453         //Filling
 2454         Streams[0xBA].TimeStamp_End.PTS.TimeStamp=(((int64u)SysClock_32)<<30)
 2455                                                 | (((int64u)SysClock_29)<<15)
 2456                                                 | (((int64u)SysClock_14));
 2457         if (Searching_TimeStamp_Start && Streams[0xBA].Searching_TimeStamp_Start)
 2458         {
 2459             Streams[0xBA].TimeStamp_Start=Streams[0xBA].TimeStamp_End;
 2460             Streams[0xBA].Searching_TimeStamp_Start=false;
 2461         }
 2462         Param_Info_From_Milliseconds(Streams[0xBA].TimeStamp_End.PTS.TimeStamp/90);
 2463 
 2464         Mark_1();
 2465         Mark_1();
 2466         Get_S3(22, program_mux_rate,                            "mux_rate"); Param_Info2(program_mux_rate*400, " bps");
 2467         Mark_1();
 2468         BS_End();
 2469     }
 2470 
 2471 
 2472     //Filling
 2473     FILLING_BEGIN_PRECISE();
 2474         if (!Status[IsAccepted])
 2475         {
 2476             Data_Accept("MPEG-PS");
 2477             if (!IsSub)
 2478                 Fill(Stream_General, 0, General_Format, "MPEG-PS");
 2479 
 2480             //Autorisation of other streams
 2481             Streams[0xB9].Searching_Payload=true;            //MPEG_program_end
 2482             Streams[0xBB].Searching_Payload=true;            //system_header_start
 2483             Streams[0xBD].Searching_Payload=true;            //private_stream_1
 2484             Streams[0xBD].Searching_TimeStamp_Start=true;    //private_stream_1
 2485             Streams[0xBD].Searching_TimeStamp_End=true;      //private_stream_1
 2486             Streams[0xBF].Searching_Payload=true;            //private_stream_2
 2487             Streams[0xFD].Searching_Payload=true;            //private_stream_1 or video_stream
 2488             Streams[0xFD].Searching_TimeStamp_Start=true;    //private_stream_1 or video_stream
 2489             Streams[0xFD].Searching_TimeStamp_End=true;      //private_stream_1 or video_stream
 2490             for (int8u Pos=0xC0; Pos<=0xEF; Pos++)
 2491             {
 2492                 Streams[Pos].Searching_Payload=true;         //audio_stream or video_stream
 2493                 Streams[Pos].Searching_TimeStamp_Start=true; //audio_stream or video_stream
 2494                 Streams[Pos].Searching_TimeStamp_End=true;   //audio_stream or video_stream
 2495             }
 2496 
 2497             MPEG_Version=Version==1?2:1;
 2498 
 2499             SizeToAnalyze=((int64u)program_mux_rate)*50*4*(MustExtendParsingDuration?4:1); //standard delay between TimeStamps is 0.7s, we try 4s to be sure
 2500             if (SizeToAnalyze>16*1024*1024)
 2501                 SizeToAnalyze=16*1024*1024; //Not too much
 2502             if (SizeToAnalyze<2*1024*1024)
 2503                 SizeToAnalyze=2*1024*1024; //Not too less
 2504         }
 2505 
 2506         #if MEDIAINFO_IBIUSAGE
 2507             if (!IsSub)
 2508                 Ibi_SynchronizationOffset_Current=File_Offset+Buffer_Offset-Header_Size;
 2509         #endif //MEDIAINFO_IBIUSAGE
 2510     FILLING_END();
 2511 }
 2512 
 2513 //---------------------------------------------------------------------------
 2514 // Packet "BB"
 2515 void File_MpegPs::system_header_start()
 2516 {
 2517     Element_Name("system_header_start");
 2518 
 2519     //If there is system_header_start, default value for private sections are false
 2520     private_stream_1_Count=0;
 2521     private_stream_2_Count=0;
 2522     SL_packetized_stream_Count=0;
 2523 
 2524     //Parsing
 2525     int32u rate_bound;
 2526     int8u  audio_bound, video_bound;
 2527     BS_Begin();
 2528     Mark_1();
 2529     Get_S3 (22, rate_bound,                                     "rate_bound"); Param_Info2(rate_bound*400, " bps");
 2530     Mark_1();
 2531     Get_S1 ( 6, audio_bound,                                    "audio_bound");
 2532     Info_SB(    fixed_flag,                                     "fixed_flag"); Param_Info1(MpegPs_System_Fixed[fixed_flag]);
 2533     Skip_SB(                                                    "CSPS_flag");
 2534     Skip_SB(                                                    "system_audio_lock_flag");
 2535     Skip_SB(                                                    "system_video_lock_flag");
 2536     Mark_1();
 2537     Get_S1 ( 5, video_bound,                                    "video_bound");
 2538     Skip_SB(                                                    "packet_rate_restriction_flag");
 2539     Skip_S1( 7,                                                 "reserved_byte");
 2540     bool one=false;
 2541     size_t StreamOrder=0;
 2542     if (Element_IsNotFinished())
 2543         Peek_SB(one);
 2544     while (one)
 2545     {
 2546         Element_Begin0();
 2547         int16u STD_buffer_size_bound;
 2548         int8u stream_id, stream_id_extension=0;
 2549         bool STD_buffer_bound_scale;
 2550         Get_S1 ( 8, stream_id,                                  "stream_id"); Param_Info1(MpegPs_stream_id(stream_id));
 2551         Element_Name(Ztring().From_CC1(stream_id));
 2552         Element_Info1(MpegPs_stream_id(stream_id));
 2553         if (stream_id==0xB7)
 2554         {
 2555             Mark_1();
 2556             Mark_1();
 2557             Mark_0();
 2558             Mark_0();
 2559             Mark_0();
 2560             Mark_0();
 2561             Mark_0();
 2562             Mark_0();
 2563             Mark_0();
 2564             Get_S1 (8, stream_id_extension,                     "stream_id_extension");
 2565             Mark_1();
 2566             Mark_0();
 2567             Mark_1();
 2568             Mark_1();
 2569             Mark_0();
 2570             Mark_1();
 2571             Mark_1();
 2572             Mark_0();
 2573         }
 2574         Mark_1();
 2575         Mark_1();
 2576         Get_SB (    STD_buffer_bound_scale,                     "STD_buffer_bound_scale");
 2577         Get_S2 (13, STD_buffer_size_bound,                      "STD_buffer_size_bound"); Param_Info1(Ztring::ToZtring(STD_buffer_size_bound*(STD_buffer_bound_scale?1024:128)) + __T(" bytes"));
 2578         Element_End0();
 2579 
 2580         FILLING_BEGIN();
 2581             switch (stream_id)
 2582             {
 2583                 case 0xBD : private_stream_1_Count=(int8u)-1; break;
 2584                 case 0xBF : private_stream_2_Count=(int8u)-1; break;
 2585                 case 0xFA : SL_packetized_stream_Count=(int8u)-1; break;
 2586                 case 0xFD : extension_stream_Count=(int8u)-1; break;
 2587                 default   : ;
 2588             }
 2589 
 2590             if (stream_id==0xBD && Streams[stream_id].StreamOrder!=(size_t)-1)
 2591                 StreamOrder_CountOfPrivateStreams_Minus1++;
 2592             else if (stream_id>0xB9)
 2593             {
 2594                 Streams[stream_id].StreamOrder=StreamOrder;
 2595                 StreamOrder++;
 2596             }
 2597         FILLING_END();
 2598 
 2599         if (Element_IsNotFinished())
 2600             Peek_SB(one);
 2601         else
 2602             one=false;
 2603     }
 2604     BS_End();
 2605 
 2606     //Filling
 2607     if (audio_stream_Count==(int8u)-1) //0xBB may be multipart
 2608         audio_stream_Count=0;
 2609     audio_stream_Count+=audio_bound;
 2610     if (video_stream_Count==(int8u)-1) //0xBB may be multipart
 2611         video_stream_Count=0;
 2612     video_stream_Count+=video_bound;
 2613     if (private_stream_1_Count>0 && program_mux_rate*50==SizeToAnalyze)
 2614         SizeToAnalyze*=32; //If there is a private section, this may be DVD, with late data --> 10s minimum
 2615     if (SizeToAnalyze>8*1024*1024)
 2616         SizeToAnalyze=8*1024*1024;
 2617 
 2618     //Autorisation of other streams
 2619     if ((private_stream_1_Count>0 || audio_stream_Count>0) && video_stream_Count>0) //0xBB may be multipart
 2620         Streams[0xBB].Searching_Payload=false;
 2621     Streams[0xBC].Searching_Payload=true;            //program_stream_map
 2622 }
 2623 
 2624 //---------------------------------------------------------------------------
 2625 // Packet "BC"
 2626 void File_MpegPs::program_stream_map()
 2627 {
 2628     Element_Name("program_stream_map");
 2629     MPEG_Version=2; //program_stream_map does NOT exist in MPEG-1 specs
 2630 
 2631     File_Mpeg_Psi Parser;
 2632     Parser.From_TS=false;
 2633     Parser.Complete_Stream=new complete_stream;
 2634     Parser.Complete_Stream->Streams.resize(0x100);
 2635     for (size_t StreamID=0; StreamID<0x100; StreamID++)
 2636         Parser.Complete_Stream->Streams[StreamID]=new complete_stream::stream;
 2637     Open_Buffer_Init(&Parser);
 2638     Open_Buffer_Continue(&Parser);
 2639     Finish(&Parser);
 2640 
 2641     FILLING_BEGIN();
 2642         //Time stamps
 2643         Streams[0xBC].TimeStamp_End=Streams[0xBA].TimeStamp_End;
 2644         if (Streams[0xBC].TimeStamp_Start.PTS.TimeStamp==(int64u)-1)
 2645             Streams[0xBC].TimeStamp_Start=Streams[0xBC].TimeStamp_End;
 2646 
 2647         //Registering the streams
 2648         for (int8u Pos=0; Pos<0xFF; Pos++)
 2649             if (Parser.Complete_Stream->Streams[Pos]->stream_type!=(int8u)-1)
 2650             {
 2651                 if (!Parser.Complete_Stream->Transport_Streams.empty() && !Parser.Complete_Stream->Transport_Streams.begin()->second.Programs.empty())
 2652                     Streams[Pos].program_format_identifier=Parser.Complete_Stream->Transport_Streams.begin()->second.Programs.begin()->second.registration_format_identifier;
 2653                 Streams[Pos].format_identifier=Parser.Complete_Stream->Streams[Pos]->registration_format_identifier;
 2654                 Streams[Pos].stream_type=Parser.Complete_Stream->Streams[Pos]->stream_type;
 2655             }
 2656             else
 2657             {
 2658             }
 2659     FILLING_END();
 2660 
 2661     delete Parser.Complete_Stream; //Parser.Complete_Stream=NULL;
 2662 }
 2663 
 2664 //---------------------------------------------------------------------------
 2665 // Packet "BD"
 2666 void File_MpegPs::private_stream_1()
 2667 {
 2668     Element_Name("private_stream_1");
 2669 
 2670     if (!FromTS)
 2671     {
 2672         //From PS, trying DVD system
 2673         private_stream_1_ID=0;
 2674         private_stream_1_Offset=0;
 2675         if (!private_stream_1_Choose_DVD_ID())
 2676         {
 2677             Skip_XX(Element_Size-Element_Offset,                "Unknown");
 2678             return;
 2679         }
 2680         Element_Info1C(private_stream_1_ID, Ztring::ToZtring(private_stream_1_ID, 16));
 2681     }
 2682 
 2683     if (!Streams_Private1[private_stream_1_ID].StreamRegistration_Count)
 2684     {
 2685         //For TS streams, which does not have Start chunk
 2686         if (FromTS)
 2687         {
 2688             if (video_stream_Count==(int8u)-1 && audio_stream_Count==(int8u)-1)
 2689             {
 2690                 video_stream_Count=0;
 2691                 audio_stream_Count=0;
 2692                 private_stream_1_Count=1;
 2693                 private_stream_2_Count=0;
 2694                 extension_stream_Count=0;
 2695                 SL_packetized_stream_Count=0;
 2696                 private_stream_1_ID=0;
 2697                 private_stream_1_Offset=0;
 2698                 Streams_Private1[private_stream_1_ID].stream_type=FromTS_stream_type;
 2699             }
 2700             else if (!IsSub)
 2701             {
 2702                 //2 streams in the file, this can not be From TS, we have no idea of the count of streams
 2703                 video_stream_Count=(int8u)-1;
 2704                 audio_stream_Count=(int8u)-1;
 2705                 private_stream_1_Count=(int8u)-1;
 2706                 private_stream_2_Count=(int8u)-1;
 2707                 extension_stream_Count=(int8u)-1;
 2708                 SL_packetized_stream_Count=(int8u)-1;
 2709                 FromTS=false;
 2710             }
 2711         }
 2712 
 2713         //Registering
 2714         if (!Status[IsAccepted])
 2715         {
 2716             Data_Accept("MPEG-PS");
 2717             if (!IsSub)
 2718                 Fill(Stream_General, 0, General_Format, "MPEG-PS");
 2719         }
 2720         Streams[stream_id].StreamRegistration_Count++;
 2721         Streams_Private1[private_stream_1_ID].StreamRegistration_Count++;
 2722         Streams_Private1[private_stream_1_ID].Init_Stream(true);
 2723         Streams_Private1[private_stream_1_ID].FirstPacketOrder=FirstPacketOrder_Last;
 2724         FirstPacketOrder_Last++;
 2725 
 2726         //New parsers
 2727         Streams_Private1[private_stream_1_ID].Parsers.push_back(private_stream_1_ChooseParser());
 2728         if (Streams_Private1[private_stream_1_ID].Parsers[Streams_Private1[private_stream_1_ID].Parsers.size()-1]==NULL)
 2729         {
 2730             Streams_Private1[private_stream_1_ID].Parsers.clear();
 2731             #if defined(MEDIAINFO_AC3_YES)
 2732                 Streams_Private1[private_stream_1_ID].Parsers.push_back(ChooseParser_AC3());
 2733             #endif
 2734             #if defined(MEDIAINFO_AC4_YES)
 2735                 Streams_Private1[private_stream_1_ID].Parsers.push_back(ChooseParser_AC4());
 2736             #endif
 2737             #if defined(MEDIAINFO_DTS_YES)
 2738                 Streams_Private1[private_stream_1_ID].Parsers.push_back(ChooseParser_DTS());
 2739             #endif
 2740             #if defined(MEDIAINFO_SMPTEST0337_YES)
 2741                 Streams_Private1[private_stream_1_ID].Parsers.push_back(ChooseParser_SmpteSt0302());
 2742             #endif
 2743             #if defined(MEDIAINFO_ARIBSTDB24B37_YES)
 2744                 Streams_Private1[private_stream_1_ID].Parsers.push_back(ChooseParser_AribStdB24B37());
 2745             #endif
 2746         }
 2747         #if MEDIAINFO_EVENTS
 2748             if (private_stream_1_Offset)
 2749             {
 2750                 //Multiple substreams in 1 stream
 2751                 StreamIDs[StreamIDs_Size-1]=Element_Code;
 2752                 Element_Code=private_stream_1_ID; //The upper level ID is filled by Element_Code in the common code
 2753                 StreamIDs_Width[StreamIDs_Size]=2;
 2754                 ParserIDs[StreamIDs_Size]=MediaInfo_Parser_MpegPs_Ext;
 2755                 StreamIDs_Size++;
 2756             }
 2757         #endif //MEDIAINFO_EVENTS
 2758         for (size_t Pos=0; Pos<Streams_Private1[private_stream_1_ID].Parsers.size(); Pos++)
 2759         {
 2760             Streams_Private1[private_stream_1_ID].Parsers[Pos]->CA_system_ID_MustSkipSlices=CA_system_ID_MustSkipSlices;
 2761             Open_Buffer_Init(Streams_Private1[private_stream_1_ID].Parsers[Pos]);
 2762         }
 2763         #if MEDIAINFO_EVENTS
 2764             if (private_stream_1_Offset)
 2765             {
 2766                 StreamIDs_Size--;
 2767                 Element_Code=StreamIDs[StreamIDs_Size-1];
 2768             }
 2769         #endif //MEDIAINFO_EVENTS
 2770     }
 2771 
 2772     //Demux
 2773     #if MEDIAINFO_DEMUX
 2774         if (Streams_Private1[private_stream_1_ID].Searching_Payload)
 2775         {
 2776             if (private_stream_1_Offset)
 2777             {
 2778                 //Multiple substreams in 1 stream
 2779                 StreamIDs[StreamIDs_Size-1]=Element_Code;
 2780                 Element_Code=private_stream_1_ID; //The upper level ID is filled by Element_Code in the common code
 2781                 StreamIDs_Width[StreamIDs_Size]=2;
 2782                 ParserIDs[StreamIDs_Size]=MediaInfo_Parser_MpegPs_Ext;
 2783                 StreamIDs_Size++;
 2784                 Demux(Buffer+Buffer_Offset+private_stream_1_Offset, (size_t)(Element_Size-private_stream_1_Offset), ContentType_MainStream);
 2785                 StreamIDs_Size--;
 2786                 Element_Code=StreamIDs[StreamIDs_Size-1];
 2787             }
 2788             else
 2789                 Demux(Buffer+Buffer_Offset, (size_t)Element_Size, ContentType_MainStream);
 2790         }
 2791     #endif //MEDIAINFO_DEMUX
 2792 
 2793     //Parsing
 2794     if (Element_Offset<private_stream_1_Offset)
 2795         Skip_XX(private_stream_1_Offset-Element_Offset,         "DVD-Video data");
 2796 
 2797     #if MEDIAINFO_EVENTS
 2798         StreamIDs[StreamIDs_Size-1]=Element_Code;
 2799         if (private_stream_1_Offset)
 2800         {
 2801             //Multiple substreams in 1 stream
 2802             StreamIDs[StreamIDs_Size]=Element_Code=private_stream_1_ID;
 2803             StreamIDs_Width[StreamIDs_Size]=2;
 2804             ParserIDs[StreamIDs_Size]=MediaInfo_Parser_MpegPs_Ext;
 2805             StreamIDs_Size++;
 2806         }
 2807     #endif //MEDIAINFO_EVENTS
 2808     xxx_stream_Parse(Streams_Private1[private_stream_1_ID], private_stream_1_Count);
 2809     #if MEDIAINFO_EVENTS
 2810         if (private_stream_1_Offset)
 2811         {
 2812             StreamIDs_Size--;
 2813             Element_Code=StreamIDs[StreamIDs_Size-1];
 2814         }
 2815     #endif //MEDIAINFO_EVENTS
 2816     #if MEDIAINFO_DEMUX
 2817         if (Config->Demux_EventWasSent)
 2818         {
 2819             Demux_StreamIsBeingParsed_type=1;
 2820             Demux_StreamIsBeingParsed_stream_id=private_stream_1_ID;
 2821         }
 2822     #endif //MEDIAINFO_DEMUX
 2823 }
 2824 
 2825 //---------------------------------------------------------------------------
 2826 bool File_MpegPs::private_stream_1_Choose_DVD_ID()
 2827 {
 2828     private_stream_1_IsDvdVideo=false;
 2829 
 2830     if (Element_Size<4)
 2831         return false;
 2832 
 2833     //Testing false-positives
 2834     if (CC2(Buffer+Buffer_Offset+(size_t)Element_Offset)==0x0B77)
 2835         return true;
 2836 
 2837     //Parsing
 2838     int8u  CodecID;
 2839     Get_B1 (CodecID,                                            "CodecID");
 2840 
 2841     //Testing
 2842     //Subtitles (CVD)
 2843          if (CodecID<=0x0F)
 2844     {
 2845         private_stream_1_IsDvdVideo=true;
 2846         private_stream_1_Offset=1;
 2847     }
 2848     //Subtitles (DVD)
 2849     else if (CodecID>=0x20 && CodecID<=0x3F)
 2850     {
 2851         private_stream_1_IsDvdVideo=true;
 2852         private_stream_1_Offset=1;
 2853     }
 2854     //Subtitles (SVCD)
 2855     else if (CodecID>=0x70 && CodecID<=0x7F)
 2856     {
 2857         private_stream_1_IsDvdVideo=true;
 2858         private_stream_1_Offset=1;
 2859     }
 2860     //AC-3 (OTA?)
 2861     else if (CodecID==0x80 && CC3(Buffer+Buffer_Offset+1)==0x000000)
 2862     {
 2863         private_stream_1_IsDvdVideo=true; //Not sure
 2864         private_stream_1_Offset=4;
 2865     }
 2866     //PCM
 2867     else if (CodecID>=0xA0 && CodecID<=0xAF && Element_Size>=7 && Buffer[Buffer_Offset+6]==0x80)
 2868     {
 2869         private_stream_1_IsDvdVideo=true;
 2870         private_stream_1_Offset=1;
 2871     }
 2872     //PS2-MPG
 2873     else if (CodecID==0xFF)
 2874     {
 2875         int16u StreamID;
 2876         int8u  SubID;
 2877         Get_B1 (SubID,                                          "CodecID (part 2)");
 2878         Get_B2 (StreamID,                                       "Stream ID");
 2879 
 2880              if ((SubID&0xFE)==0xA0) //0xFFA0 or 0xFFA1
 2881         {
 2882             //PS2-MPG PCM/ADPCM
 2883             private_stream_1_Offset=4;
 2884             private_stream_1_ID=(int8u)StreamID; //ID is maybe 2 byte long, but private_stream_1_ID is an int8u
 2885             return true;
 2886         }
 2887         else if (SubID==0x90) //0xFF90
 2888         {
 2889             //PS2-MPG AC-3 or subtitles
 2890             private_stream_1_Offset=4;
 2891             private_stream_1_ID=(int8u)StreamID; //ID is maybe 2 byte long, but private_stream_1_ID is an int8u
 2892             return true;
 2893         }
 2894         else
 2895             return false;
 2896     }
 2897     else
 2898     {
 2899         int16u Next;
 2900         int8u  Count;
 2901         Get_B1 (Count,                                          "Count of next frame headers");
 2902         Get_B2 (Next,                                           "Next frame offset minus 1");
 2903 
 2904         if (Count>0 && 4+(int64u)Next+4<=Element_Size)
 2905         {
 2906             //Subtitles (CVD)
 2907             //     if (CodecID>=0x00 && CodecID<=0x0F)
 2908             //    ; //Seems to not work with subtitles, to be confirmed
 2909             //Subtitles (DVD)
 2910             //     if (CodecID>=0x20 && CodecID<=0x3F)
 2911             //    ; //Seems to not work with subtitles, to be confirmed
 2912             //Subtitles (SVCD)
 2913             //     if (CodecID>=0x70 && CodecID<=0x7F)
 2914             //    ; //Seems to not work with subtitles, to be confirmed
 2915             //AC3
 2916                 if (CodecID>=0x80 && CodecID<=0x87)
 2917             {
 2918                 if (CC2(Buffer+Buffer_Offset+4+Next)!=0x0B77 && CC2(Buffer+Buffer_Offset+3+Next)!=0x0B77 && CC2(Buffer+Buffer_Offset+2+Next)!=0x0B77)
 2919                     return false;
 2920             }
 2921             //DTS
 2922             else if (CodecID>=0x88 && CodecID<=0x8F)
 2923             {
 2924                 if (CC4(Buffer+Buffer_Offset+4+Next)!=0x7FFE8001 && CC4(Buffer+Buffer_Offset+3+Next)!=0x7FFE8001 && CC4(Buffer+Buffer_Offset+2+Next)!=0x7FFE8001)
 2925                     return false;
 2926             }
 2927             //DTS
 2928             else if (CodecID>=0x98 && CodecID<=0x9F)
 2929             {
 2930                 if (CC4(Buffer+Buffer_Offset+4+Next)!=0x7FFE8001 && CC4(Buffer+Buffer_Offset+3+Next)!=0x7FFE8001 && CC4(Buffer+Buffer_Offset+2+Next)!=0x7FFE8001)
 2931                     return false;
 2932             }
 2933             //PCM
 2934             //else if (CodecID>=0xA0 && CodecID<=0xAF)
 2935             //    ;
 2936             //MLP
 2937             else if (CodecID>=0xB0 && CodecID<=0xBF)
 2938             {
 2939                 if (CC2(Buffer+Buffer_Offset+4+Next)!=0x0B77 && CC2(Buffer+Buffer_Offset+3+Next)!=0x0B77 && CC2(Buffer+Buffer_Offset+2+Next)!=0x0B77)
 2940                     return false;
 2941             }
 2942             //AC3+
 2943             else if (CodecID>=0xC0 && CodecID<=0xCF)
 2944             {
 2945                 if (CC2(Buffer+Buffer_Offset+4+Next)!=0x0B77 && CC2(Buffer+Buffer_Offset+3+Next)!=0x0B77 && CC2(Buffer+Buffer_Offset+2+Next)!=0x0B77)
 2946                     return false;
 2947             }
 2948             else
 2949                 return false;
 2950 
 2951             private_stream_1_IsDvdVideo=true;
 2952             private_stream_1_Offset=4;
 2953         }
 2954     }
 2955 
 2956     //Filling
 2957     private_stream_1_ID=CodecID;
 2958     return true;
 2959 }
 2960 
 2961 //---------------------------------------------------------------------------
 2962 File__Analyze* File_MpegPs::private_stream_1_ChooseParser()
 2963 {
 2964     if (FromTS || Streams[stream_id].program_format_identifier || Streams[stream_id].format_identifier || Streams[stream_id].descriptor_tag)
 2965     {
 2966         int32u format_identifier=FromTS?FromTS_format_identifier:Streams[stream_id].format_identifier;
 2967         if (format_identifier==0x42535344) //"BSSD"
 2968         {
 2969             return ChooseParser_SmpteSt0302(); //AES3 (SMPTE 302M)
 2970         }
 2971         int32u stream_type=FromTS?FromTS_stream_type:Streams[stream_id].stream_type;
 2972         switch (stream_type)
 2973         {
 2974             case 0x03 :
 2975             case 0x04 : return ChooseParser_Mpega(); //MPEG Audio
 2976             case 0x0F : return ChooseParser_Adts(); //ADTS
 2977             case 0x11 : return ChooseParser_Latm(); //LATM
 2978             case 0x80 : return ChooseParser_PCM(); //PCM
 2979             case 0x81 :
 2980             case 0x83 :
 2981             case 0x84 :
 2982             case 0x87 :
 2983             case 0xA1 : return ChooseParser_AC3(); //AC3/AC3+
 2984             case 0x82 :
 2985             case 0x85 :
 2986             case 0x86 :
 2987             case 0xA2 : return ChooseParser_DTS(); //DTS
 2988             case 0x90 : return ChooseParser_PGS(); //PGS from Bluray
 2989             case 0xEA : return ChooseParser_NULL(); //VC1()
 2990             default   :
 2991                         {
 2992                         int8u descriptor_tag=FromTS?FromTS_descriptor_tag:Streams[stream_id].descriptor_tag;
 2993                         switch (descriptor_tag)
 2994                         {
 2995                             case 0x56 : return ChooseParser_Teletext(); //Teletext
 2996                             case 0x59 : return ChooseParser_DvbSubtitle(); //DVB Subtiles
 2997                             case 0x6A :
 2998                             case 0x7A :
 2999                             case 0x81 : return ChooseParser_AC3(); //AC3/AC3+
 3000                             case 0x7B : return ChooseParser_DTS(); //DTS
 3001                             case 0x7C : return ChooseParser_AAC(); //AAC
 3002                             default   :      if (Element_Size>2 && CC2(Buffer+Buffer_Offset)==0x0B77)
 3003                                             return ChooseParser_AC3(); //AC3/AC3+
 3004                                         else if (Element_Size>4 && CC4(Buffer+Buffer_Offset)==0x7FFE8001)
 3005                                             return ChooseParser_DTS(); //DTS
 3006                                         else
 3007                                             return NULL;
 3008                         }
 3009                         }
 3010         }
 3011     }
 3012     else if (Element_Code==0xBD && private_stream_1_IsDvdVideo)
 3013     {
 3014         //Subtitles (CVD)
 3015              if (private_stream_1_ID<=0x0F)
 3016             return ChooseParser_RLE();
 3017         //Subtitles (DVD)
 3018              if (private_stream_1_ID>=0x20 && private_stream_1_ID<=0x3F)
 3019             return ChooseParser_RLE();
 3020         //Subtitles (SVCD)
 3021              if (private_stream_1_ID>=0x70 && private_stream_1_ID<=0x7F)
 3022             return ChooseParser_RLE();
 3023         //AC3
 3024         else if (private_stream_1_ID>=0x80 && private_stream_1_ID<=0x87)
 3025             return ChooseParser_AC3();
 3026         //DTS
 3027         else if (private_stream_1_ID>=0x88 && private_stream_1_ID<=0x8F)
 3028             return ChooseParser_DTS();
 3029         //SDDS
 3030         else if (private_stream_1_ID>=0x90 && private_stream_1_ID<=0x97)
 3031             return ChooseParser_DTS();
 3032         //DTS
 3033         else if (private_stream_1_ID>=0x98 && private_stream_1_ID<=0x9F)
 3034             return ChooseParser_DTS();
 3035         //PCM
 3036         else if (private_stream_1_ID>=0xA0 && private_stream_1_ID<=0xAF)
 3037             return ChooseParser_PCM();
 3038         //AC3+
 3039         else if (private_stream_1_ID>=0xC0 && private_stream_1_ID<=0xCF)
 3040             return ChooseParser_AC3();
 3041         else
 3042             return NULL;
 3043     }
 3044     else
 3045     {
 3046              if (Element_Size>2 && CC2(Buffer+Buffer_Offset)==0x0B77)
 3047             return ChooseParser_AC3(); //AC3/AC3+
 3048         else if (Element_Size>4 && CC4(Buffer+Buffer_Offset)==0x7FFE8001)
 3049             return ChooseParser_DTS(); //DTS
 3050         else if (Element_Size>2 && (CC2(Buffer+Buffer_Offset)&0xFFFE)==0xFFA0) //0xFFA0 or 0xFFA1
 3051             return ChooseParser_PS2(); //PS2-MPG PCM/ADPCM
 3052         else if (Element_Size>6 && CC2(Buffer+Buffer_Offset)==0xFF90 && CC2(Buffer+Buffer_Offset+4)==0x0B77)
 3053             return ChooseParser_AC3(); //PS2-MPG AC-3
 3054         else if (Element_Size>6 && CC2(Buffer+Buffer_Offset)==0xFF90 && CC2(Buffer+Buffer_Offset+4)==0x0000)
 3055             return ChooseParser_RLE(); //PS2-MPG Subtitles
 3056         else
 3057             return NULL;
 3058     }
 3059 }
 3060 
 3061 //---------------------------------------------------------------------------
 3062 const ZenLib::Char* File_MpegPs::private_stream_1_ChooseExtension()
 3063 {
 3064     if (FromTS)
 3065     {
 3066         switch (private_stream_1_ID)
 3067         {
 3068             case 0x80 : return __T(".pcm"); //PCM
 3069             case 0x81 : return __T(".ac3"); //AC3
 3070             case 0x83 :
 3071             case 0x87 : return __T(".dd+"); //AC3+
 3072             case 0x86 : return __T(".dts"); //DTS
 3073             case 0xEA : return __T(".vc1"); //DTS
 3074             default   : return __T(".raw");
 3075         }
 3076     }
 3077     else
 3078     {
 3079         //Subtitles
 3080              if (private_stream_1_ID>=0x20 && private_stream_1_ID<=0x3F)
 3081             return __T(".sub");
 3082         //AC3
 3083         else if (private_stream_1_ID>=0x80 && private_stream_1_ID<=0x87)
 3084             return __T(".ac3");
 3085         //DTS
 3086         else if (private_stream_1_ID>=0x88 && private_stream_1_ID<=0x8F)
 3087             return __T(".dts");
 3088         //SDDS
 3089         else if (private_stream_1_ID>=0x90 && private_stream_1_ID<=0x97)
 3090             return __T(".sdds");
 3091         //DTS
 3092         else if (private_stream_1_ID>=0x98 && private_stream_1_ID<=0x9F)
 3093             return __T(".dts");
 3094         //PCM
 3095         else if (private_stream_1_ID>=0xA0 && private_stream_1_ID<=0xAF)
 3096             return __T(".pcm");
 3097         //MLP
 3098         else if (private_stream_1_ID>=0xB0 && private_stream_1_ID<=0xBF)
 3099             return __T(".dd+");
 3100         //AC3+
 3101         else if (private_stream_1_ID>=0xC0 && private_stream_1_ID<=0xCF)
 3102             return __T(".dd+");
 3103         else
 3104             return __T(".raw");
 3105     }
 3106 }
 3107 
 3108 //---------------------------------------------------------------------------
 3109 #if MEDIAINFO_TRACE
 3110 void File_MpegPs::private_stream_1_Element_Info1()
 3111 {
 3112     if (FromTS)
 3113     {
 3114         switch (private_stream_1_ID)
 3115         {
 3116             case 0x80 : Element_Info1("PCM"); return;
 3117             case 0x81 : Element_Info1("AC3"); return;
 3118             case 0x83 :
 3119             case 0x87 : Element_Info1("AC3+"); return;
 3120             case 0x86 : Element_Info1("DTS"); return;
 3121             case 0xEA : Element_Info1("VC1"); return;
 3122             default   : return;
 3123         }
 3124     }
 3125     else
 3126     {
 3127         //Subtitles
 3128              if (private_stream_1_ID>=0x20 && private_stream_1_ID<=0x3F)
 3129             Element_Info1("RLE");
 3130         //AC3
 3131         else if (private_stream_1_ID>=0x80 && private_stream_1_ID<=0x87)
 3132             Element_Info1("AC3");
 3133         //DTS
 3134         else if (private_stream_1_ID>=0x88 && private_stream_1_ID<=0x8F)
 3135             Element_Info1("DTS");
 3136         //SDDS
 3137         else if (private_stream_1_ID>=0x90 && private_stream_1_ID<=0x97)
 3138             Element_Info1("SDDS");
 3139         //DTS
 3140         else if (private_stream_1_ID>=0x98 && private_stream_1_ID<=0x9F)
 3141             Element_Info1("DTS");
 3142         //PCM
 3143         else if (private_stream_1_ID>=0xA0 && private_stream_1_ID<=0xAF)
 3144             Element_Info1("LPCM");
 3145         //MLP
 3146         else if (private_stream_1_ID>=0xB0 && private_stream_1_ID<=0xBF)
 3147             Element_Info1("MLP");
 3148         //AC3+
 3149         else if (private_stream_1_ID>=0xC0 && private_stream_1_ID<=0xCF)
 3150             Element_Info1("AC3+");
 3151     }
 3152 }
 3153 #endif //MEDIAINFO_TRACE
 3154 
 3155 //---------------------------------------------------------------------------
 3156 // Packet "BE"
 3157 void File_MpegPs::padding_stream()
 3158 {
 3159     Element_Name("padding_stream");
 3160 
 3161     Skip_XX(Element_Size,                                       "stuffing_bytes");
 3162 }
 3163 
 3164 //---------------------------------------------------------------------------
 3165 // Packet "BF"
 3166 void File_MpegPs::private_stream_2()
 3167 {
 3168     Element_Name("private_stream_2");
 3169 
 3170     //Filling
 3171     if (FromTS)
 3172     {
 3173         switch (FromTS_program_format_identifier)
 3174         {
 3175             case 0x54534856 : //TSHV
 3176                                 switch (FromTS_stream_type)
 3177                                 {
 3178                                     case 0xA0 : private_stream_2_TSHV_A0(); break;
 3179                                     case 0xA1 : private_stream_2_TSHV_A1(); break;
 3180                                     default   : Skip_XX(Element_Size, "Unknown");
 3181                                 }
 3182                                 break;
 3183             default         : Skip_XX(Element_Size,             "Unknown");
 3184         }
 3185 
 3186         //Disabling the program
 3187         if (!Status[IsAccepted])
 3188             Data_Accept("MPEG-PS");
 3189     }
 3190     else //DVD?
 3191     {
 3192         Stream_Prepare(Stream_Menu);
 3193         Fill(Stream_Menu, StreamPos_Last, Menu_Format, "DVD-Video");
 3194         Fill(Stream_Menu, StreamPos_Last, Menu_Codec, "DVD-Video");
 3195         Streams[0xBF].StreamKind=StreamKind_Last;
 3196         Streams[0xBF].StreamPos=StreamPos_Last;
 3197 
 3198         //Disabling this Stream
 3199         Streams[0xBF].Searching_Payload=false;
 3200         private_stream_2_Count=0;
 3201     }
 3202 }
 3203 
 3204 //---------------------------------------------------------------------------
 3205 void File_MpegPs::private_stream_2_TSHV_A0()
 3206 {
 3207     Element_Name("DV A0");
 3208 
 3209     //Parsing
 3210     Skip_XX(Element_Size,                                       "Unknown");
 3211 
 3212     //Filling
 3213     Data_Accept("MPEG-PS");
 3214     Finish("MPEG-PS");
 3215 }
 3216 
 3217 //---------------------------------------------------------------------------
 3218 void File_MpegPs::private_stream_2_TSHV_A1()
 3219 {
 3220     Element_Name("DV A1");
 3221 
 3222     //Parsing
 3223     int8u day, month, year, second, minute, hour;
 3224     Skip_XX(31,                                                 "Unknown");
 3225     BS_Begin();
 3226     Skip_S1(2,                                                  "Unknown");
 3227     Skip_S1(6,                                                  "timecode_frame");
 3228     Skip_S1(1,                                                  "Unknown");
 3229     Skip_S1(7,                                                  "timecode_second");
 3230     Skip_S1(1,                                                  "Unknown");
 3231     Skip_S1(7,                                                  "timecode_minute");
 3232     Skip_S1(2,                                                  "Unknown");
 3233     Skip_S1(6,                                                  "timecode_hour");
 3234     Skip_S1(8,                                                  "Unknown");
 3235     Skip_S1(2,                                                  "Unknown");
 3236     Get_S1 (6, day,                                             "day");
 3237     Skip_S1(3,                                                  "Unknown");
 3238     Get_S1 (5, month,                                           "month");
 3239     Get_S1 (8, year,                                            "year");
 3240     Skip_S1(8,                                                  "Unknown");
 3241     Skip_S1(1,                                                  "Unknown");
 3242     Get_S1 (7, second,                                          "second");
 3243     Skip_S1(1,                                                  "Unknown");
 3244     Get_S1 (7, minute,                                          "minute");
 3245     Skip_S1(2,                                                  "Unknown");
 3246     Get_S1 (6, hour,                                            "hour");
 3247     Skip_S1(2,                                                  "Unknown");
 3248     Skip_S1(1,                                                  "scene_start");
 3249     Skip_S1(5,                                                  "Unknown");
 3250     BS_End();
 3251     Skip_XX(Element_Size-Element_Offset,                        "Unknown");
 3252 
 3253     FILLING_BEGIN();
 3254         Ztring Date_Time=Ztring().Date_From_Numbers(year/0x10*10+year%0x10, month/0x10*10+month%0x10, day/0x10*10+day%0x10, hour/0x10*10+hour%0x10, minute/0x10*10+minute%0x10, second/0x10*10+second%0x10);
 3255         if (Retrieve(Stream_General, 0, General_Encoded_Date).empty())
 3256         {
 3257             Fill(Stream_General, 0, General_Encoded_Date, Date_Time);
 3258             Fill(Stream_General, 0, General_Duration_Start, Date_Time);
 3259         }
 3260         Fill(Stream_General, 0, General_Duration_End, Date_Time, true);
 3261     FILLING_END();
 3262 }
 3263 
 3264 //---------------------------------------------------------------------------
 3265 void File_MpegPs::audio_stream()
 3266 {
 3267     Element_Name("Audio");
 3268 
 3269     if (!Streams[stream_id].StreamRegistration_Count)
 3270     {
 3271         //For TS streams, which does not have Start chunk
 3272         if (FromTS)
 3273         {
 3274             if (video_stream_Count==(int8u)-1 && audio_stream_Count==(int8u)-1)
 3275             {
 3276                 video_stream_Count=0;
 3277                 audio_stream_Count=1;
 3278                 private_stream_1_Count=0;
 3279                 private_stream_2_Count=0;
 3280                 extension_stream_Count=0;
 3281                 SL_packetized_stream_Count=0;
 3282                 Streams[stream_id].stream_type=FromTS_stream_type;
 3283             }
 3284             else if (!IsSub)
 3285             {
 3286                 //2 streams in the file, this can not be From TS, we have no idea of the count of streams
 3287                 video_stream_Count=(int8u)-1;
 3288                 audio_stream_Count=(int8u)-1;
 3289                 private_stream_1_Count=(int8u)-1;
 3290                 private_stream_2_Count=(int8u)-1;
 3291                 extension_stream_Count=(int8u)-1;
 3292                 SL_packetized_stream_Count=(int8u)-1;
 3293                 FromTS=false;
 3294             }
 3295         }
 3296 
 3297         //If we have no Streams map --> Registering the Streams as MPEG Audio
 3298         if (Streams[stream_id].stream_type==0 && !FromTS)
 3299         {
 3300             if (MPEG_Version==2)
 3301                 Streams[stream_id].stream_type=0x04; //MPEG-2 Audio
 3302             else
 3303                 Streams[stream_id].stream_type=0x03; //MPEG-1 Audio
 3304         }
 3305 
 3306         //Registering
 3307         if (!Status[IsAccepted])
 3308         {
 3309             Data_Accept("MPEG-PS");
 3310             if (!IsSub)
 3311                 Fill(Stream_General, 0, General_Format, "MPEG-PS");
 3312         }
 3313         Streams[stream_id].StreamRegistration_Count++;
 3314         Streams[stream_id].FirstPacketOrder=FirstPacketOrder_Last;
 3315         FirstPacketOrder_Last++;
 3316 
 3317         //New parsers
 3318         Streams[stream_id].Parsers.push_back(private_stream_1_ChooseParser());
 3319         if (Streams[stream_id].Parsers[Streams[stream_id].Parsers.size()-1]==NULL)
 3320         {
 3321             Streams[stream_id].Parsers.clear();
 3322             #if defined(MEDIAINFO_MPEGA_YES)
 3323                 Streams[stream_id].Parsers.push_back(ChooseParser_Mpega());
 3324             #endif
 3325             #if defined(MEDIAINFO_AC3_YES)
 3326                 Streams[stream_id].Parsers.push_back(ChooseParser_AC3());
 3327             #endif
 3328             #if defined(MEDIAINFO_AC4_YES)
 3329                 Streams[stream_id].Parsers.push_back(ChooseParser_AC4());
 3330             #endif
 3331             #if defined(MEDIAINFO_DTS_YES)
 3332                 Streams[stream_id].Parsers.push_back(ChooseParser_DTS());
 3333             #endif
 3334             #if defined(MEDIAINFO_AAC_YES)
 3335                 Streams[stream_id].Parsers.push_back(ChooseParser_Adts());
 3336             #endif
 3337             #if defined(MEDIAINFO_AAC_YES)
 3338                 Streams[stream_id].Parsers.push_back(ChooseParser_Latm());
 3339             #endif
 3340         }
 3341         for (size_t Pos=0; Pos<Streams[stream_id].Parsers.size(); Pos++)
 3342         {
 3343             Streams[stream_id].Parsers[Pos]->CA_system_ID_MustSkipSlices=CA_system_ID_MustSkipSlices;
 3344             Open_Buffer_Init(Streams[stream_id].Parsers[Pos]);
 3345         }
 3346     }
 3347 
 3348     //Demux
 3349     #if MEDIAINFO_DEMUX
 3350         if (Streams[stream_id].Parsers.empty() || !Streams[stream_id].Parsers[0]->Demux_UnpacketizeContainer)
 3351             Demux(Buffer+Buffer_Offset, (size_t)Element_Size, ContentType_MainStream);
 3352     #endif //MEDIAINFO_DEMUX
 3353 
 3354     //Parsing
 3355     #if MEDIAINFO_EVENTS
 3356         StreamIDs[StreamIDs_Size-1]=Element_Code;
 3357     #endif //MEDIAINFO_EVENTS
 3358     xxx_stream_Parse(Streams[stream_id], audio_stream_Count);
 3359     #if MEDIAINFO_DEMUX
 3360         if (Config->Demux_EventWasSent)
 3361         {
 3362             Demux_StreamIsBeingParsed_type=0;
 3363             Demux_StreamIsBeingParsed_stream_id=stream_id;
 3364         }
 3365     #endif //MEDIAINFO_DEMUX
 3366 }
 3367 
 3368 //---------------------------------------------------------------------------
 3369 void File_MpegPs::video_stream()
 3370 {
 3371     Element_Name("Video");
 3372 
 3373     if (!Streams[stream_id].StreamRegistration_Count)
 3374     {
 3375         //For TS streams, which does not have Start chunk
 3376         if (FromTS)
 3377         {
 3378             if (video_stream_Count==(int8u)-1 && audio_stream_Count==(int8u)-1)
 3379             {
 3380                 video_stream_Count=1;
 3381                 audio_stream_Count=0;
 3382                 private_stream_1_Count=0;
 3383                 private_stream_2_Count=0;
 3384                 extension_stream_Count=0;
 3385                 SL_packetized_stream_Count=0;
 3386                 Streams[stream_id].stream_type=FromTS_stream_type;
 3387             }
 3388             else if (!IsSub)
 3389             {
 3390                 //2 streams in the file, this can not be From TS, we have no idea of the count of streams
 3391                 video_stream_Count=(int8u)-1;
 3392                 audio_stream_Count=(int8u)-1;
 3393                 private_stream_1_Count=(int8u)-1;
 3394                 private_stream_2_Count=(int8u)-1;
 3395                 extension_stream_Count=(int8u)-1;
 3396                 SL_packetized_stream_Count=(int8u)-1;
 3397                 FromTS=false;
 3398             }
 3399         }
 3400 
 3401         //Registering
 3402         if (!Status[IsAccepted])
 3403         {
 3404             Data_Accept("MPEG-PS");
 3405             if (!IsSub)
 3406                 Fill(Stream_General, 0, General_Format, "MPEG-PS");
 3407         }
 3408         Streams[stream_id].StreamRegistration_Count++;
 3409         Streams[stream_id].FirstPacketOrder=FirstPacketOrder_Last;
 3410         FirstPacketOrder_Last++;
 3411 
 3412         //New parsers
 3413         switch (Streams[stream_id].stream_type)
 3414         {
 3415             case 0x01 :
 3416             case 0x02 :
 3417             case 0x80 : Streams[stream_id].Parsers.push_back(ChooseParser_Mpegv() ); break;
 3418             case 0x10 :
 3419                         Streams[stream_id].Parsers.push_back(ChooseParser_Mpeg4v()); break;
 3420             case 0x1B :
 3421                         Streams[stream_id].Parsers.push_back(ChooseParser_Avc()   ); break;
 3422             case 0x24 :
 3423             case 0x27 : Streams[stream_id].Parsers.push_back(ChooseParser_Hevc()  ); break;
 3424             default   :
 3425                         #if defined(MEDIAINFO_MPEGV_YES)
 3426                             Streams[stream_id].Parsers.push_back(ChooseParser_Mpegv());
 3427                         #endif
 3428                         #if defined(MEDIAINFO_AVC_YES)
 3429                             Streams[stream_id].Parsers.push_back(ChooseParser_Avc());
 3430                         #endif
 3431                         #if defined(MEDIAINFO_HEVC_YES)
 3432                             Streams[stream_id].Parsers.push_back(ChooseParser_Hevc());
 3433                         #endif
 3434                         #if defined(MEDIAINFO_MPEG4V_YES)
 3435                             Streams[stream_id].Parsers.push_back(ChooseParser_Mpeg4v());
 3436                         #endif
 3437                         #if defined(MEDIAINFO_AVSV_YES)
 3438                         {
 3439                             File_AvsV* Parser=new File_AvsV;
 3440                             Streams[stream_id].Parsers.push_back(Parser);
 3441                         }
 3442                         #endif
 3443         }
 3444         for (size_t Pos=0; Pos<Streams[stream_id].Parsers.size(); Pos++)
 3445         {
 3446             Streams[stream_id].Parsers[Pos]->CA_system_ID_MustSkipSlices=CA_system_ID_MustSkipSlices;
 3447             Open_Buffer_Init(Streams[stream_id].Parsers[Pos]);
 3448             #if MEDIAINFO_IBIUSAGE
 3449                 if (FromTS)
 3450                     Streams[stream_id].Parsers[Pos]->IbiStream=IbiStream;
 3451                 else
 3452                 {
 3453                     if (Ibi.Streams[stream_id]==NULL)
 3454                         Ibi.Streams[stream_id]=new ibi::stream;
 3455                     Streams[stream_id].Parsers[Pos]->IbiStream=Ibi.Streams[stream_id];
 3456                 }
 3457             #endif //MEDIAINFO_IBIUSAGE
 3458             #if MEDIAINFO_SEEK
 3459                 if (Unsynch_Frame_Counts.find(stream_id)!=Unsynch_Frame_Counts.end())
 3460                     Streams[stream_id].Parsers[Pos]->Frame_Count_NotParsedIncluded=Unsynch_Frame_Counts[stream_id];
 3461             #endif //MEDIAINFO_SEEK
 3462         }
 3463         #if MEDIAINFO_SEEK
 3464             Unsynch_Frame_Counts.erase(stream_id);
 3465         #endif //MEDIAINFO_SEEK
 3466     }
 3467 
 3468     //Demux
 3469     #if MEDIAINFO_DEMUX
 3470         if (!(FromTS_stream_type==0x20 && SubStream_Demux) && (Streams[stream_id].Parsers.empty() || !Streams[stream_id].Parsers[0]->Demux_UnpacketizeContainer))
 3471             Demux(Buffer+Buffer_Offset, (size_t)Element_Size, ContentType_MainStream);
 3472     #endif //MEDIAINFO_DEMUX
 3473 
 3474     //Parsing
 3475     #if MEDIAINFO_EVENTS
 3476         StreamIDs[StreamIDs_Size-1]=Element_Code;
 3477     #endif //MEDIAINFO_EVENTS
 3478     xxx_stream_Parse(Streams[stream_id], video_stream_Count);
 3479     #if MEDIAINFO_DEMUX
 3480         if (Config->Demux_EventWasSent)
 3481         {
 3482             Demux_StreamIsBeingParsed_type=0;
 3483             Demux_StreamIsBeingParsed_stream_id=stream_id;
 3484         }
 3485     #endif //MEDIAINFO_DEMUX
 3486 }
 3487 
 3488 //---------------------------------------------------------------------------
 3489 // Packet "FA"
 3490 void File_MpegPs::SL_packetized_stream()
 3491 {
 3492     Element_Name("SL-packetized_stream");
 3493 
 3494     if (!Streams[stream_id].StreamRegistration_Count)
 3495     {
 3496         //For TS streams, which does not have Start chunk
 3497         if (FromTS)
 3498         {
 3499             if (video_stream_Count==(int8u)-1 && audio_stream_Count==(int8u)-1)
 3500             {
 3501                 video_stream_Count=0;
 3502                 audio_stream_Count=0;
 3503                 private_stream_1_Count=0;
 3504                 private_stream_2_Count=0;
 3505                 extension_stream_Count=0;
 3506                 SL_packetized_stream_Count=1;
 3507                 Streams[stream_id].stream_type=FromTS_stream_type;
 3508             }
 3509             else if (!IsSub)
 3510             {
 3511                 //2 streams in the file, this can not be From TS, we have no idea of the count of streams
 3512                 video_stream_Count=(int8u)-1;
 3513                 audio_stream_Count=(int8u)-1;
 3514                 private_stream_1_Count=(int8u)-1;
 3515                 private_stream_2_Count=(int8u)-1;
 3516                 extension_stream_Count=(int8u)-1;
 3517                 SL_packetized_stream_Count=(int8u)-1;
 3518                 FromTS=false;
 3519             }
 3520         }
 3521 
 3522         //Registering
 3523         Streams[stream_id].StreamRegistration_Count++;
 3524         Streams[stream_id].FirstPacketOrder=FirstPacketOrder_Last;
 3525         FirstPacketOrder_Last++;
 3526         if (!Status[IsAccepted])
 3527             Data_Accept("MPEG-PS");
 3528         Streams[stream_id].Searching_TimeStamp_Start=true;
 3529 
 3530         //New parsers
 3531         #ifdef MEDIAINFO_MPEG4_YES
 3532             if (ParserFromTs)
 3533             {
 3534                 Streams[stream_id].Parsers.push_back(ParserFromTs); ParserFromTs=NULL;
 3535             }
 3536             else
 3537         #endif
 3538         if (FromTS_stream_type)
 3539             switch (FromTS_stream_type)
 3540             {
 3541                 case 0x0F :
 3542                             Streams[stream_id].Parsers.push_back(ChooseParser_Adts());
 3543                             break;
 3544 
 3545                 case 0x11 :
 3546                             Streams[stream_id].Parsers.push_back(ChooseParser_Latm());
 3547                             break;
 3548                 default   : ;
 3549             }
 3550         else
 3551         {
 3552             #if defined(MEDIAINFO_AAC_YES)
 3553                 Streams[stream_id].Parsers.push_back(ChooseParser_Adts());
 3554             #endif
 3555             #if defined(MEDIAINFO_AAC_YES)
 3556                 Streams[stream_id].Parsers.push_back(ChooseParser_Latm());
 3557             #endif
 3558         }
 3559         for (size_t Pos=0; Pos<Streams[stream_id].Parsers.size(); Pos++)
 3560         {
 3561             Streams[stream_id].Parsers[Pos]->CA_system_ID_MustSkipSlices=CA_system_ID_MustSkipSlices;
 3562             Open_Buffer_Init(Streams[stream_id].Parsers[Pos]);
 3563         }
 3564     }
 3565 
 3566     //Parsing
 3567     #ifdef MEDIAINFO_MPEG4_YES
 3568         if (SLConfig) //SL
 3569         {
 3570             BS_Begin();
 3571             int8u paddingBits=0;
 3572             bool paddingFlag=false, idleFlag=false, OCRflag=false, accessUnitStartFlag=false;
 3573             if (SLConfig->useAccessUnitStartFlag)
 3574                 Get_SB (accessUnitStartFlag,                        "accessUnitStartFlag");
 3575             if (SLConfig->useAccessUnitEndFlag)
 3576                 Skip_SB(                                            "accessUnitEndFlag");
 3577             if (SLConfig->OCRLength>0)
 3578                 Get_SB (OCRflag,                                    "OCRflag");
 3579             if (SLConfig->useIdleFlag)
 3580                 Get_SB (idleFlag,                                   "idleFlag");
 3581             if (SLConfig->usePaddingFlag)
 3582                 Get_SB (paddingFlag,                                "paddingFlag");
 3583             if (paddingFlag)
 3584                 Get_S1(3, paddingBits,                              "paddingBits");
 3585             if (!idleFlag && (!paddingFlag || paddingBits!=0))
 3586             {
 3587                 bool DegPrioflag=false;
 3588                 if (SLConfig->packetSeqNumLength>0)
 3589                     Skip_S2(SLConfig->packetSeqNumLength,           "packetSequenceNumber");
 3590                 if (SLConfig->degradationPriorityLength>0)
 3591                     Get_SB (DegPrioflag,                            "DegPrioflag");
 3592                 if (DegPrioflag)
 3593                     Skip_S2(SLConfig->degradationPriorityLength,    "degradationPriority");
 3594                 if (OCRflag)
 3595                     Skip_S8(SLConfig->OCRLength,                    "objectClockReference");
 3596                 if (accessUnitStartFlag)
 3597                 {
 3598                     bool decodingTimeStampFlag=false, compositionTimeStampFlag=false, instantBitrateFlag=false;
 3599                     if (SLConfig->useRandomAccessPointFlag)
 3600                         Skip_SB(                                    "randomAccessPointFlag");
 3601                     if (SLConfig->AU_seqNumLength >0)
 3602                         Skip_S2(SLConfig->AU_seqNumLength,          "AU_sequenceNumber");
 3603                     if (SLConfig->useTimeStampsFlag)
 3604                     {
 3605                         Get_SB (decodingTimeStampFlag,              "decodingTimeStampFlag");
 3606                         Get_SB (compositionTimeStampFlag,           "compositionTimeStampFlag");
 3607                     }
 3608                     if (SLConfig->instantBitrateLength>0)
 3609                         Get_SB (instantBitrateFlag,                 "instantBitrateFlag");
 3610                     if (decodingTimeStampFlag)
 3611                         Skip_S2(SLConfig->timeStampLength,          "decodingTimeStamp");
 3612                     if (compositionTimeStampFlag)
 3613                         Skip_S2(SLConfig->timeStampLength,          "compositionTimeStamp");
 3614                     if (SLConfig->AU_Length > 0)
 3615                         Skip_S2(SLConfig->AU_Length,                "accessUnitLength");
 3616                     if (instantBitrateFlag)
 3617                         Skip_S2(SLConfig->instantBitrateLength,     "instantBitrate");
 3618                 }
 3619             }
 3620             BS_End();
 3621         }
 3622     #else //MEDIAINFO_MPEG4_YES
 3623         Skip_XX(Element_Size,                                       "LATM (not decoded)");
 3624     #endif //MEDIAINFO_MPEG4_YES
 3625 
 3626     //Demux
 3627     /*
 3628     if (Config_Demux)
 3629     {
 3630         int8u A[7];
 3631         //TODO: Only for 24KHz stuff, should be modified... output is ADTS
 3632         A[0]=0xFF;
 3633         A[1]=0xF9;
 3634         A[2]=0x58;
 3635         A[3]=0x80;
 3636         A[4]=0x00;
 3637         A[5]=0x1F;
 3638         A[6]=0xFC;
 3639 
 3640         int32u Size=(int32u)(Element_Size+7);
 3641         Size=Size<<13;
 3642         A[3]=A[3]|((int8u)(Size>>24));
 3643         A[4]=A[4]|((int8u)(Size>>16));
 3644         A[5]=A[5]|((int8u)(Size>>8));
 3645 
 3646         //Demux
 3647         Demux(A, 7, ContentType_Header);
 3648         Demux(Buffer+Buffer_Offset, (size_t)Element_Size, ContentType_MainStream);
 3649     }
 3650     */
 3651     #if MEDIAINFO_DEMUX
 3652         Demux(Buffer+Buffer_Offset, (size_t)Element_Size, ContentType_MainStream);
 3653     #endif //MEDIAINFO_DEMUX
 3654 
 3655     //Parsing
 3656     #if MEDIAINFO_EVENTS
 3657         StreamIDs[StreamIDs_Size-1]=Element_Code;
 3658     #endif //MEDIAINFO_EVENTS
 3659     xxx_stream_Parse(Streams[stream_id], SL_packetized_stream_Count);
 3660     #if MEDIAINFO_DEMUX
 3661         if (Config->Demux_EventWasSent)
 3662         {
 3663             Demux_StreamIsBeingParsed_type=0;
 3664             Demux_StreamIsBeingParsed_stream_id=stream_id;
 3665         }
 3666     #endif //MEDIAINFO_DEMUX
 3667 }
 3668 
 3669 //---------------------------------------------------------------------------
 3670 // Packet "FD"
 3671 void File_MpegPs::extension_stream()
 3672 {
 3673     Element_Name("With Extension");
 3674     Element_Info1(MpegPs_stream_id_extension(stream_id_extension));
 3675 
 3676     if (!Streams_Extension[stream_id_extension].StreamRegistration_Count)
 3677     {
 3678         //For TS streams, which does not have Start chunk
 3679         if (FromTS)
 3680         {
 3681             if (video_stream_Count==(int8u)-1 && audio_stream_Count==(int8u)-1)
 3682             {
 3683                 video_stream_Count=0;
 3684                 audio_stream_Count=0;
 3685                 private_stream_1_Count=0;
 3686                 private_stream_2_Count=0;
 3687                 extension_stream_Count=1;
 3688                 SL_packetized_stream_Count=0;
 3689                 Streams_Extension[stream_id_extension].stream_type=FromTS_stream_type;
 3690             }
 3691             else if (!IsSub)
 3692             {
 3693                 //2 streams in the file, this can not be From TS, we have no idea of the count of streams
 3694                 video_stream_Count=(int8u)-1;
 3695                 audio_stream_Count=(int8u)-1;
 3696                 private_stream_1_Count=(int8u)-1;
 3697                 private_stream_2_Count=(int8u)-1;
 3698                 extension_stream_Count=(int8u)-1;
 3699                 SL_packetized_stream_Count=(int8u)-1;
 3700                 FromTS=false;
 3701             }
 3702         }
 3703 
 3704         //Registering
 3705         if (!Status[IsAccepted])
 3706             Data_Accept("MPEG-PS");
 3707         Streams[stream_id].StreamRegistration_Count++;
 3708         Streams_Extension[stream_id_extension].StreamRegistration_Count++;
 3709         Streams_Extension[stream_id_extension].Searching_Payload=true;
 3710         Streams_Extension[stream_id_extension].Searching_TimeStamp_Start=true;
 3711         Streams_Extension[stream_id_extension].Searching_TimeStamp_End=true;
 3712         Streams_Extension[stream_id_extension].FirstPacketOrder=FirstPacketOrder_Last;
 3713         FirstPacketOrder_Last++;
 3714 
 3715         //New parsers
 3716         if (Streams_Extension[stream_id_extension].stream_type && Streams_Extension[stream_id_extension].stream_type<0x80) //Standard
 3717             switch (Streams_Extension[stream_id_extension].stream_type)
 3718             {
 3719                 case 0x0F : Streams_Extension[stream_id_extension].Parsers.push_back(ChooseParser_Adts()); break;
 3720                 default   : ;
 3721             }
 3722         else
 3723             switch (FromTS_format_identifier)
 3724             {
 3725                 case 0x41432D33 :
 3726                                     Streams_Extension[stream_id_extension].Parsers.push_back(ChooseParser_AC3());
 3727                                     break;
 3728                 case 0x44545331 :
 3729                 case 0x44545332 :
 3730                 case 0x44545333 :
 3731                                     Streams_Extension[stream_id_extension].Parsers.push_back(ChooseParser_DTS());
 3732                                     break;
 3733                 case 0x56432D31 :
 3734                                     Streams_Extension[stream_id_extension].Parsers.push_back(ChooseParser_VC1());
 3735                                     break;
 3736                 case 0x64726163 :
 3737                                     Streams_Extension[stream_id_extension].Parsers.push_back(ChooseParser_Dirac());
 3738                                     break;
 3739                 default           :
 3740                                     switch (FromTS_program_format_identifier)
 3741                                     {
 3742                                         case 0x48444D56 :   //HDMV (BluRay)
 3743                                                             switch (Streams_Extension[stream_id_extension].stream_type)
 3744                                                             {
 3745                                                                 case 0x81 :
 3746                                                                 case 0x83 :
 3747                                                                 case 0x84 :
 3748                                                                 case 0xA1 :
 3749                                                                             Streams_Extension[stream_id_extension].Parsers.push_back(ChooseParser_AC3());
 3750                                                                             break;
 3751                                                                 case 0x82 :
 3752                                                                 case 0x85 :
 3753                                                                 case 0x86 :
 3754                                                                 case 0xA2 :
 3755                                                                             Streams_Extension[stream_id_extension].Parsers.push_back(ChooseParser_DTS());
 3756                                                                             break;
 3757                                                                 case 0xEA :
 3758                                                                             Streams_Extension[stream_id_extension].Parsers.push_back(ChooseParser_VC1());
 3759                                                                             break;
 3760                                                                 default   : ;
 3761                                                             }
 3762                                                             break;
 3763                                         default           : ;
 3764                                                                  if (stream_id_extension==0x00)
 3765                                                                 {} //IPMP Control Information stream
 3766                                                             else if (stream_id_extension==0x01)
 3767                                                                 {} //IPMP stream
 3768                                                             else if (stream_id_extension>=0x55 && stream_id_extension<=0x5F)
 3769                                                                  Streams_Extension[stream_id_extension].Parsers.push_back(ChooseParser_VC1());
 3770                                                             else if (stream_id_extension>=0x60 && stream_id_extension<=0x6F)
 3771                                                                  Streams_Extension[stream_id_extension].Parsers.push_back(ChooseParser_Dirac());
 3772                                                             else if (stream_id_extension==0x71 || stream_id_extension==0x72 || stream_id_extension==0x76)
 3773                                                             {
 3774                                                                 Streams_Extension[stream_id_extension].Parsers.push_back(ChooseParser_DTS());
 3775                                                                 Streams_Extension[stream_id_extension].Parsers.push_back(ChooseParser_AC3());
 3776                                                             }
 3777                                                             else if (stream_id_extension==0x75)
 3778                                                                  Streams_Extension[stream_id_extension].Parsers.push_back(ChooseParser_VC1());
 3779                                                       }
 3780             }
 3781 
 3782         if (Streams_Extension[stream_id_extension].Parsers.empty())
 3783         {
 3784             #if defined(MEDIAINFO_DIRAC_YES)
 3785                 Streams_Extension[stream_id_extension].Parsers.push_back(ChooseParser_Dirac());
 3786             #endif
 3787             #if defined(MEDIAINFO_VC1_YES)
 3788                 Streams_Extension[stream_id_extension].Parsers.push_back(ChooseParser_VC1());
 3789             #endif
 3790             #if defined(MEDIAINFO_AC3_YES)
 3791                 Streams_Extension[stream_id_extension].Parsers.push_back(ChooseParser_AC3());
 3792             #endif
 3793             #if defined(MEDIAINFO_AC4_YES)
 3794                 Streams_Extension[stream_id_extension].Parsers.push_back(ChooseParser_AC4());
 3795             #endif
 3796             #if defined(MEDIAINFO_DTS_YES)
 3797                 Streams_Extension[stream_id_extension].Parsers.push_back(ChooseParser_DTS());
 3798             #endif
 3799         }
 3800 
 3801         //In case of HD part before Core part
 3802         switch (stream_id_extension)
 3803         {
 3804             case 0x71 :
 3805             case 0x76 :
 3806                         for (size_t Pos=0; Pos<Streams_Extension[0x72].Parsers.size(); Pos++)
 3807                             delete Streams_Extension[0x72].Parsers[Pos]; //Streams_Extension[0x72].Parsers[Pos]=NULL;
 3808                         Streams_Extension[0x72].Parsers.clear();
 3809                         break;
 3810         }
 3811 
 3812         //Init
 3813         for (size_t Pos=0; Pos<Streams_Extension[stream_id_extension].Parsers.size(); Pos++)
 3814         {
 3815             Streams_Extension[stream_id_extension].Parsers[Pos]->CA_system_ID_MustSkipSlices=CA_system_ID_MustSkipSlices;
 3816             Open_Buffer_Init(Streams_Extension[stream_id_extension].Parsers[Pos]);
 3817         }
 3818     }
 3819 
 3820     //Demux
 3821     #if MEDIAINFO_DEMUX
 3822         if (Streams_Extension[stream_id_extension].Searching_Payload)
 3823         {
 3824             StreamIDs[StreamIDs_Size-1]=Element_Code;
 3825             if (stream_id_extension==0x72 && !(Streams_Extension[0x71].Parsers.empty() && Streams_Extension[0x76].Parsers.empty()))
 3826             {
 3827                 if (!Streams_Extension[0x71].Parsers.empty())
 3828                     Element_Code=0x71;
 3829                 if (!Streams_Extension[0x76].Parsers.empty())
 3830                     Element_Code=0x76;
 3831             }
 3832             else
 3833                 Element_Code=stream_id_extension; //The upper level ID is filled by Element_Code in the common code
 3834             StreamIDs_Width[StreamIDs_Size]=2;
 3835             ParserIDs[StreamIDs_Size]=MediaInfo_Parser_MpegPs_Ext;
 3836             StreamIDs_Size++;
 3837             if (stream_id_extension==0x72 && !(Streams_Extension[0x71].Parsers.empty() && Streams_Extension[0x76].Parsers.empty()))
 3838                 Demux(Buffer+Buffer_Offset, (size_t)Element_Size, ContentType_SubStream);
 3839             else
 3840                 Demux(Buffer+Buffer_Offset, (size_t)Element_Size, ContentType_MainStream);
 3841             StreamIDs_Size--;
 3842             Element_Code=StreamIDs[StreamIDs_Size-1];
 3843         }
 3844     #endif //MEDIAINFO_DEMUX
 3845 
 3846     //Parsing
 3847     if (stream_id_extension==0x72 && !(Streams_Extension[0x71].Parsers.empty() && Streams_Extension[0x76].Parsers.empty()))
 3848     {
 3849         if (!Streams_Extension[0x71].Parsers.empty())
 3850         {
 3851             #if MEDIAINFO_EVENTS
 3852                 //Multiple substreams in 1 stream
 3853                 StreamIDs[StreamIDs_Size-1]=Element_Code;
 3854                 StreamIDs[StreamIDs_Size]=Element_Code=0x71;
 3855                 StreamIDs_Width[StreamIDs_Size]=2;
 3856                 ParserIDs[StreamIDs_Size]=MediaInfo_Parser_MpegPs_Ext;
 3857                 StreamIDs_Size++;
 3858             #endif //MEDIAINFO_EVENTS
 3859             xxx_stream_Parse(Streams_Extension[0x71], extension_stream_Count);
 3860             #if MEDIAINFO_EVENTS
 3861                 StreamIDs_Size--;
 3862                 Element_Code=StreamIDs[StreamIDs_Size-1];
 3863             #endif //MEDIAINFO_EVENTS
 3864             #if MEDIAINFO_DEMUX
 3865                 if (Config->Demux_EventWasSent)
 3866                 {
 3867                     Demux_StreamIsBeingParsed_type=2;
 3868                     Demux_StreamIsBeingParsed_stream_id=0x71;
 3869                 }
 3870             #endif //MEDIAINFO_DEMUX
 3871         }
 3872         if (!Streams_Extension[0x76].Parsers.empty())
 3873         {
 3874             #if MEDIAINFO_EVENTS
 3875                 //Multiple substreams in 1 stream
 3876                 StreamIDs[StreamIDs_Size-1]=Element_Code;
 3877                 StreamIDs[StreamIDs_Size]=Element_Code=0x76;
 3878                 StreamIDs_Width[StreamIDs_Size]=2;
 3879                 ParserIDs[StreamIDs_Size]=MediaInfo_Parser_MpegPs_Ext;
 3880                 StreamIDs_Size++;
 3881             #endif //MEDIAINFO_EVENTS
 3882             xxx_stream_Parse(Streams_Extension[0x76], extension_stream_Count);
 3883             #if MEDIAINFO_EVENTS
 3884                 StreamIDs_Size--;
 3885                 Element_Code=StreamIDs[StreamIDs_Size-1];
 3886             #endif //MEDIAINFO_EVENTS
 3887             #if MEDIAINFO_DEMUX
 3888                 if (Config->Demux_EventWasSent)
 3889                 {
 3890                     Demux_StreamIsBeingParsed_type=2;
 3891                     Demux_StreamIsBeingParsed_stream_id=0x76;
 3892                 }
 3893             #endif //MEDIAINFO_DEMUX
 3894         }
 3895     }
 3896     else
 3897     {
 3898         #if MEDIAINFO_EVENTS
 3899             //Multiple substreams in 1 stream
 3900             StreamIDs[StreamIDs_Size-1]=Element_Code;
 3901             StreamIDs[StreamIDs_Size]=Element_Code=stream_id_extension;
 3902             StreamIDs_Width[StreamIDs_Size]=2;
 3903             ParserIDs[StreamIDs_Size]=MediaInfo_Parser_MpegPs_Ext;
 3904             StreamIDs_Size++;
 3905         #endif //MEDIAINFO_EVENTS
 3906         xxx_stream_Parse(Streams_Extension[stream_id_extension], extension_stream_Count);
 3907         #if MEDIAINFO_EVENTS
 3908             StreamIDs_Size--;
 3909             Element_Code=StreamIDs[StreamIDs_Size-1];
 3910         #endif //MEDIAINFO_EVENTS
 3911         #if MEDIAINFO_DEMUX
 3912             if (Config->Demux_EventWasSent)
 3913             {
 3914                 Demux_StreamIsBeingParsed_type=2;
 3915                 Demux_StreamIsBeingParsed_stream_id=stream_id_extension;
 3916             }
 3917         #endif //MEDIAINFO_DEMUX
 3918     }
 3919 }
 3920 
 3921 //---------------------------------------------------------------------------
 3922 const ZenLib::Char* File_MpegPs::extension_stream_ChooseExtension()
 3923 {
 3924     //AC3
 3925         if ((stream_id_extension>=0x55 && stream_id_extension<=0x5F)
 3926          || (stream_id_extension==0x75))
 3927         return __T(".vc1");
 3928     //AC3+
 3929     else if (stream_id_extension>=0x60 && stream_id_extension<=0x6F)
 3930         return __T(".dirac");
 3931     else if (stream_id_extension==0x71)
 3932         return private_stream_1_ChooseExtension();
 3933     else
 3934         return __T(".raw");
 3935 }
 3936 
 3937 //***************************************************************************
 3938 // xxx_stream helpers
 3939 //***************************************************************************
 3940 
 3941 //---------------------------------------------------------------------------
 3942 void File_MpegPs::xxx_stream_Parse(ps_stream &Temp, int8u &stream_Count)
 3943 {
 3944     switch (stream_id)
 3945     {
 3946         case 0xBD :
 3947         //case 0xBF :
 3948         case 0xFD :
 3949             //PTS
 3950             if (Streams[stream_id].TimeStamp_End.PTS.TimeStamp!=(int64u)-1)
 3951             {
 3952                 if (Streams[stream_id].Searching_TimeStamp_End)
 3953                 {
 3954                     Temp.TimeStamp_End.PTS.File_Pos=File_Offset+Buffer_Offset;
 3955                     Temp.TimeStamp_End.PTS.TimeStamp=Streams[stream_id].TimeStamp_End.PTS.TimeStamp;
 3956                 }
 3957                 if (Searching_TimeStamp_Start && Temp.Searching_TimeStamp_Start)
 3958                 {
 3959                     Temp.TimeStamp_Start.PTS.File_Pos=File_Offset+Buffer_Offset;
 3960                     Temp.TimeStamp_Start.PTS.TimeStamp=Streams[stream_id].TimeStamp_End.PTS.TimeStamp;
 3961                     Temp.Searching_TimeStamp_Start=false;
 3962                 }
 3963             }
 3964 
 3965             //DTS
 3966             if (Streams[stream_id].TimeStamp_End.DTS.TimeStamp!=(int64u)-1)
 3967             {
 3968                 if (Streams[stream_id].Searching_TimeStamp_End)
 3969                 {
 3970                     Temp.TimeStamp_End.DTS.File_Pos=File_Offset+Buffer_Offset;
 3971                     Temp.TimeStamp_End.DTS.TimeStamp=Streams[stream_id].TimeStamp_End.DTS.TimeStamp;
 3972                 }
 3973                 if (Searching_TimeStamp_Start && Streams[stream_id].TimeStamp_End.DTS.TimeStamp!=(int64u)-1 && Temp.Searching_TimeStamp_Start)
 3974                 {
 3975                     Temp.TimeStamp_Start.DTS.TimeStamp=Streams[stream_id].TimeStamp_End.DTS.TimeStamp;
 3976                     Temp.Searching_TimeStamp_Start=false;
 3977                 }
 3978             }
 3979         default : ;
 3980     }
 3981 
 3982     //Needed?
 3983     if (Temp.Parsers.size()==1 && Temp.Parsers[0]->Status[IsFinished])
 3984     {
 3985         Skip_XX(Element_Size-Element_Offset,                    "data");
 3986         return;
 3987     }
 3988 
 3989     #if MEDIAINFO_TRACE
 3990         if (stream_id==0xBD /*|| stream_id==0xBF*/)
 3991             private_stream_1_Element_Info1();
 3992     #endif //MEDIAINFO_TRACE
 3993 
 3994     for (size_t Pos=0; Pos<Temp.Parsers.size(); Pos++)
 3995         if (Temp.Parsers[Pos] && !Temp.Parsers[Pos]->Status[IsFinished])
 3996         {
 3997             //PTS/DTS
 3998             if (Temp.Parsers[Pos]->PTS_DTS_Needed)
 3999             {
 4000                 if (FrameInfo.PCR!=(int64u)-1)
 4001                     Temp.Parsers[Pos]->FrameInfo.PCR=FrameInfo.PCR;
 4002                 TS_Set(Temp.Parsers[Pos]);
 4003             }
 4004 
 4005             #if MEDIAINFO_TRACE
 4006                 if (Temp.Parsers.size()>1)
 4007                     Element_Begin1("Test");
 4008             #endif //MEDIAINFO_TRACE
 4009             #if MEDIAINFO_IBIUSAGE
 4010                 Temp.Parsers[Pos]->Ibi_SynchronizationOffset_Current=Ibi_SynchronizationOffset_Current;
 4011             #endif //MEDIAINFO_IBIUSAGE
 4012             #if defined(MEDIAINFO_EIA608_YES) || defined(MEDIAINFO_EIA708_YES)
 4013                 Temp.Parsers[Pos]->ServiceDescriptors=ServiceDescriptors;
 4014             #endif
 4015             #if defined(MEDIAINFO_TELETEXT_YES)
 4016                 Temp.Parsers[Pos]->Teletexts=Teletexts;
 4017             #endif
 4018             Open_Buffer_Continue(Temp.Parsers[Pos], Buffer+Buffer_Offset+(size_t)Element_Offset, (size_t)(Element_Size-Element_Offset));
 4019             if (IsSub && Temp.Parsers[Pos]->Frame_Count_NotParsedIncluded!=(int64u)-1)
 4020                 Frame_Count_NotParsedIncluded=Temp.Parsers[Pos]->Frame_Count_NotParsedIncluded;
 4021             if (!MustExtendParsingDuration && Temp.Parsers[Pos]->MustExtendParsingDuration)
 4022             {
 4023                 SizeToAnalyze*=4; //Normally 4 seconds, now 16 seconds
 4024                 MustExtendParsingDuration=true;
 4025             }
 4026             #if MEDIAINFO_TRACE
 4027                 if (Temp.Parsers.size()>1)
 4028                     Element_End0();
 4029             #endif //MEDIAINFO_TRACE
 4030 
 4031             if (Temp.Parsers.size()>1)
 4032             {
 4033                 if (!Temp.Parsers[Pos]->Status[IsAccepted] && Temp.Parsers[Pos]->Status[IsFinished])
 4034                 {
 4035                     delete *(Temp.Parsers.begin()+Pos);
 4036                     Temp.Parsers.erase(Temp.Parsers.begin()+Pos);
 4037                     Pos--;
 4038                 }
 4039                 else if (Temp.Parsers.size()>1 && Temp.Parsers[Pos]->Status[IsAccepted])
 4040                 {
 4041                     File__Analyze* Parser=Temp.Parsers[Pos];
 4042                     for (size_t Pos2=0; Pos2<Temp.Parsers.size(); Pos2++)
 4043                     {
 4044                         if (Pos2!=Pos)
 4045                             delete *(Temp.Parsers.begin()+Pos2);
 4046                     }
 4047                     Temp.Parsers.clear();
 4048                     Temp.Parsers.push_back(Parser);
 4049                 }
 4050             }
 4051 
 4052             if (Temp.Parsers.size()==1 && !Temp.IsFilled && Temp.Parsers[0]->Status[IsFilled])
 4053             {
 4054                 stream_Count--;
 4055                 Temp.IsFilled=true;
 4056             }
 4057 
 4058             //Checking cases with B-frames displayed before the first I-frame
 4059             if (Temp.Parsers.size()==1 && Temp.Parsers[0]->PTS_Begin!=(int64u)-1 && Temp.TimeStamp_Start.PTS.TimeStamp!=(int64u)-1 && float64_int64s(((float64)Temp.Parsers[0]->PTS_Begin)*90/1000000)<(int64s)Temp.TimeStamp_Start.PTS.TimeStamp)
 4060             {
 4061                 Temp.TimeStamp_Start.PTS.File_Pos=File_Offset+Buffer_Offset-Header_Size;
 4062                 Temp.TimeStamp_Start.PTS.TimeStamp=float64_int64s(((float64)Temp.Parsers[0]->PTS_Begin)*90/1000000); //TODO: same denominator for the time stamp
 4063             }
 4064         }
 4065     //FrameInfo.PCR=(int64u)-1;
 4066     TS_Clear();
 4067     Element_Show();
 4068 
 4069     #if MEDIAINFO_EVENTS
 4070         if (FrameInfo.DTS==(int64u)-1)
 4071             FrameInfo.DTS=FrameInfo.PTS;
 4072 
 4073         //New PES
 4074         #if MEDIAINFO_DEMUX
 4075             if (PES_FirstByte_IsAvailable && PES_FirstByte_Value)
 4076             {
 4077                 //Demux of substream data
 4078                 if (FromTS_stream_type==0x1B && SubStream_Demux)
 4079                 {
 4080                     if (!SubStream_Demux->Buffers.empty() && SubStream_Demux->Buffers[0] && SubStream_Demux->Buffers[0]->DTS<FrameInfo.DTS)
 4081                     {
 4082                         Demux(SubStream_Demux->Buffers[0]->Buffer, SubStream_Demux->Buffers[0]->Buffer_Size, ContentType_SubStream);
 4083                         delete SubStream_Demux->Buffers[0]->Buffer; SubStream_Demux->Buffers[0]->Buffer=NULL;
 4084                         SubStream_Demux->Buffers.erase(SubStream_Demux->Buffers.begin()); //Moving 2nd Buffer to 1st position
 4085                     }
 4086                 }
 4087             }
 4088 
 4089             //Demux of SubStream
 4090             if (FromTS_stream_type==0x20 && SubStream_Demux)
 4091             {
 4092                 //Searching an available slot
 4093                 size_t Buffers_Pos;
 4094                 if (SubStream_Demux->Buffers.empty() || SubStream_Demux->Buffers[SubStream_Demux->Buffers.size()-1]->DTS!=FrameInfo.DTS)
 4095                 {
 4096                     Buffers_Pos=SubStream_Demux->Buffers.size();
 4097                     SubStream_Demux->Buffers.push_back(new demux::buffer);
 4098                 }
 4099                 else
 4100                 {
 4101                     Buffers_Pos=SubStream_Demux->Buffers.size()-1;
 4102                 }
 4103 
 4104                 //Filling buffer
 4105                 if (SubStream_Demux->Buffers[Buffers_Pos]->Buffer==NULL)
 4106                 {
 4107                     SubStream_Demux->Buffers[Buffers_Pos]->DTS=FrameInfo.DTS;
 4108                     SubStream_Demux->Buffers[Buffers_Pos]->Buffer_Size_Max=128*1024;
 4109                     SubStream_Demux->Buffers[Buffers_Pos]->Buffer_Size=0;
 4110                     SubStream_Demux->Buffers[Buffers_Pos]->Buffer=new int8u[SubStream_Demux->Buffers[Buffers_Pos]->Buffer_Size_Max];
 4111                 }
 4112                 if (SubStream_Demux->Buffers[Buffers_Pos]->Buffer_Size_Max>SubStream_Demux->Buffers[Buffers_Pos]->Buffer_Size+(size_t)(Element_Size-Element_Offset) && SubStream_Demux->Buffers[Buffers_Pos]->Buffer_Size_Max<=16*1024*1024)
 4113                 {
 4114                     SubStream_Demux->Buffers[Buffers_Pos]->Buffer_Size_Max*=2;
 4115                     int8u* Buffer_Demux=SubStream_Demux->Buffers[Buffers_Pos]->Buffer;
 4116                     SubStream_Demux->Buffers[Buffers_Pos]->Buffer=new int8u[SubStream_Demux->Buffers[Buffers_Pos]->Buffer_Size_Max];
 4117                     std::memcpy(SubStream_Demux->Buffers[Buffers_Pos]->Buffer, Buffer_Demux, SubStream_Demux->Buffers[Buffers_Pos]->Buffer_Size);
 4118                     delete[] Buffer_Demux; //Buffer_Demux=NULL;
 4119                 }
 4120                 if (SubStream_Demux->Buffers[Buffers_Pos]->Buffer_Size+(size_t)(Element_Size-Element_Offset)<=SubStream_Demux->Buffers[Buffers_Pos]->Buffer_Size_Max)
 4121                 {
 4122                     std::memcpy(SubStream_Demux->Buffers[Buffers_Pos]->Buffer+SubStream_Demux->Buffers[Buffers_Pos]->Buffer_Size, Buffer+Buffer_Offset+(size_t)Element_Offset, (size_t)(Element_Size-Element_Offset));
 4123                     SubStream_Demux->Buffers[Buffers_Pos]->Buffer_Size+=(size_t)(Element_Size-Element_Offset);
 4124                 }
 4125             }
 4126         #endif //MEDIAINFO_DEMUX
 4127     #endif //MEDIAINFO_EVENTS
 4128 
 4129     #if MEDIAINFO_SEEK && MEDIAINFO_IBIUSAGE
 4130         if (Seek_ID!=(int64u)-1)
 4131         {
 4132             if (Ibi.Streams[Seek_ID]->IsModified)
 4133             {
 4134                 Read_Buffer_Seek(2, Seek_Value, Seek_ID);
 4135             }
 4136             else if (File_Offset+Buffer_Offset>=Seek_Value_Maximal)
 4137             {
 4138                 //No intermediate seek point found, going to previous seek point
 4139                 for (size_t Pos=1; Pos<Ibi.Streams[Seek_ID]->Infos.size(); Pos++)
 4140                     if (Ibi.Streams[Seek_ID]->Infos[Pos].StreamOffset>=Seek_Value_Maximal)
 4141                     {
 4142                         if (Ibi.Streams[Seek_ID]->IsSynchronized)
 4143                         {
 4144                             //No intermediate point is possible
 4145                             Ibi.Streams[Seek_ID]->Infos[Pos-1].IsContinuous=true;
 4146                             Read_Buffer_Seek(2, Seek_Value, Seek_ID);
 4147                         }
 4148                         else
 4149                         {
 4150                             //Going to last known seek point
 4151                             GoTo(Ibi.Streams[Seek_ID]->Infos[Pos-1].StreamOffset);
 4152                             Open_Buffer_Unsynch();
 4153                         }
 4154                         break;
 4155                     }
 4156             }
 4157         }
 4158     #endif //MEDIAINFO_SEEK && MEDIAINFO_IBIUSAGE
 4159 }
 4160 
 4161 //***************************************************************************
 4162 // Helpers
 4163 //***************************************************************************
 4164 
 4165 //---------------------------------------------------------------------------
 4166 bool File_MpegPs::Header_Parser_QuickSearch()
 4167 {
 4168     while (           Buffer_Offset+4<=Buffer_Size
 4169       &&   CC3(Buffer+Buffer_Offset)==0x000001)
 4170     {
 4171         //Getting stream_id
 4172         int8u stream_id=Buffer[Buffer_Offset+3];
 4173 
 4174         //Trace config
 4175         #if MEDIAINFO_TRACE
 4176             if (Config_Trace_Level)
 4177             {
 4178                 if (stream_id==0xC0 || stream_id==0xE0)
 4179                 {
 4180                     if (!Trace_Layers[8])
 4181                         Trace_Layers_Update(8); //Stream
 4182                 }
 4183                 else
 4184                     Trace_Layers_Update(IsSub?1:0);
 4185             }
 4186         #endif //MEDIAINFO_TRACE
 4187 
 4188         //Searching start
 4189         if (Streams[stream_id].Searching_Payload)
 4190         {
 4191             if (stream_id!=0xBD /*&& stream_id!=0xBF)*/ || !private_stream_1_IsDvdVideo) //Not (private_stream_1 and IsDvdVideo)
 4192                 return true;
 4193 
 4194             //private_stream_1 and IsDvdVideo, looking for substream ID
 4195             if (Buffer_Offset+9>=Buffer_Size)
 4196                 return false; //Need more data
 4197             size_t Data_Offset=Buffer[Buffer_Offset+8];
 4198             if (Buffer_Offset+9+Data_Offset>=Buffer_Size)
 4199                 return false; //Need more data
 4200             int8u  private_stream_1_ID=Buffer[Buffer_Offset+9+Data_Offset];
 4201             if (!Streams_Private1[private_stream_1_ID].StreamRegistration_Count || Streams_Private1[private_stream_1_ID].Searching_Payload)
 4202                 return true;
 4203         }
 4204 
 4205         //Searching TimeStamp_End
 4206         if (Streams[stream_id].Searching_TimeStamp_End)
 4207         {
 4208             switch(stream_id)
 4209             {
 4210                 //Element with no PES Header
 4211                 case 0xB9 : //MPEG_program_end
 4212                 case 0xBA : //pack_start
 4213                 case 0xBB : //system_header_start
 4214                 case 0xBC : //program_stream_map
 4215                 case 0xBE : //padding_stream
 4216                 case 0xBF : //private_stream_2
 4217                 case 0xF0 : //ECM
 4218                 case 0xF1 : //EMM
 4219                 case 0xF2 : //DSMCC Streams
 4220                 case 0xF8 : //ITU-T Rec. H .222.1 type E
 4221                 case 0xFF : //Program Streams directory
 4222                     break;
 4223 
 4224                 //Element with PES Header
 4225                 default :
 4226                     if (MPEG_Version==1)
 4227                     {
 4228                         size_t Buffer_Offset_Temp=Buffer_Offset+6;
 4229                         while(Buffer_Offset_Temp<Buffer_Size && Buffer[Buffer_Offset_Temp]==0xFF)
 4230                         {
 4231                             Buffer_Offset_Temp++;
 4232                             if (Buffer_Offset_Temp+1>=Buffer_Size)
 4233                                 return false; //Not enough data
 4234                         }
 4235                         if (Buffer_Offset_Temp+1>=Buffer_Size)
 4236                             return false; //Not enough data
 4237                         if (Buffer_Offset_Temp<Buffer_Size && (Buffer[Buffer_Offset_Temp]&0xF0)!=0x00)
 4238                             return true; //With a PTS
 4239                     }
 4240                     if (MPEG_Version==2)
 4241                     {
 4242                         if (Buffer_Offset+8>Buffer_Size)
 4243                             return false; //Not enough buffer
 4244                         if ((Buffer[Buffer_Offset+7]&0xC0)!=0x00)
 4245                             return true; //With a PTS
 4246                     }
 4247             }
 4248         }
 4249 
 4250         //Getting size
 4251         switch(stream_id)
 4252         {
 4253             //No size
 4254             case 0xB9 : //MPEG_program_end
 4255             case 0xBA : //pack_start
 4256                 Buffer_Offset+=4;
 4257                 while(Buffer_Offset+4<=Buffer_Size && !(CC3(Buffer+Buffer_Offset)==0x000001 && Buffer[Buffer_Offset+3]>=0xB9))
 4258                 {
 4259                     Buffer_Offset+=2;
 4260                     while(Buffer_Offset<Buffer_Size && Buffer[Buffer_Offset]!=0x00)
 4261                         Buffer_Offset+=2;
 4262                     if (Buffer_Offset>=Buffer_Size || Buffer[Buffer_Offset-1]==0x00)
 4263                         Buffer_Offset--;
 4264                 }
 4265                 //Parsing last bytes if needed
 4266                 if (Buffer_Offset+4>Buffer_Size)
 4267                 {
 4268                     if (Buffer_Offset+3==Buffer_Size && CC3(Buffer+Buffer_Offset)!=0x000001)
 4269                         Buffer_Offset++;
 4270                     if (Buffer_Offset+2==Buffer_Size && CC2(Buffer+Buffer_Offset)!=0x0000)
 4271                         Buffer_Offset++;
 4272                     if (Buffer_Offset+1==Buffer_Size && CC1(Buffer+Buffer_Offset)!=0x00)
 4273                         Buffer_Offset++;
 4274                 }
 4275                 break;
 4276 
 4277             //Element with size
 4278             default :
 4279                 if (Buffer_Offset+6>=Buffer_Size)
 4280                     return false; //Not enough data
 4281                 int16u Size=CC2(Buffer+Buffer_Offset+4);
 4282                 if (Size>0)
 4283                 {
 4284                     Buffer_Offset+=6+Size;
 4285 
 4286                     //Trailing 0xFF
 4287                     while(Buffer_Offset<Buffer_Size && Buffer[Buffer_Offset]==0xFF)
 4288                         Buffer_Offset++;
 4289 
 4290                     //Trailing 0x00
 4291                     while(Buffer_Offset+3<=Buffer_Size
 4292                        && Buffer[Buffer_Offset+2]==0x00
 4293                        && Buffer[Buffer_Offset+1]==0x00
 4294                        && Buffer[Buffer_Offset  ]==0x00)
 4295                         Buffer_Offset++;
 4296                 }
 4297                 else
 4298                 {
 4299                     Buffer_Offset+=6;
 4300                     while(Buffer_Offset+4<=Buffer_Size && !(CC3(Buffer+Buffer_Offset)==0x000001 && Buffer[Buffer_Offset+3]>=0xB9))
 4301                         Buffer_Offset++;
 4302                     if (Buffer_Offset+4>Buffer_Size)
 4303                     {
 4304                         if (Buffer_Offset+3==Buffer_Size && CC3(Buffer+Buffer_Offset)!=0x000001)
 4305                             Buffer_Offset++;
 4306                         if (Buffer_Offset+2==Buffer_Size && CC2(Buffer+Buffer_Offset)!=0x0000)
 4307                             Buffer_Offset++;
 4308                         if (Buffer_Offset+1==Buffer_Size && CC1(Buffer+Buffer_Offset)!=0x00)
 4309                             Buffer_Offset++;
 4310                     }
 4311                 }
 4312         }
 4313     }
 4314 
 4315     if (Buffer_Offset+3==Buffer_Size)
 4316         return false; //Sync is OK, but stream_id is not available
 4317     if (Buffer_Offset+4<=Buffer_Size)
 4318         Trusted_IsNot("MPEG-PS, Synchronisation lost");
 4319     Synched=false;
 4320     return Synchronize();
 4321 }
 4322 
 4323 //***************************************************************************
 4324 // Parsers
 4325 //***************************************************************************
 4326 
 4327 //---------------------------------------------------------------------------
 4328 File__Analyze* File_MpegPs::ChooseParser_Mpegv()
 4329 {
 4330     //Filling
 4331     #if defined(MEDIAINFO_MPEGV_YES)
 4332         File_Mpegv* Parser=new File_Mpegv;
 4333         Parser->ShouldContinueParsing=true;
 4334         #if MEDIAINFO_DEMUX
 4335             if (Config->Demux_Unpacketize_Get())
 4336             {
 4337                 Demux_UnpacketizeContainer=false; //No demux from this parser
 4338                 Demux_Level=4; //Intermediate
 4339                 Parser->Demux_Level=2; //Container
 4340                 Parser->Demux_UnpacketizeContainer=true;
 4341             }
 4342         #endif //MEDIAINFO_DEMUX
 4343     #else
 4344         //Filling
 4345         File__Analyze* Parser=new File_Unknown();
 4346         Open_Buffer_Init(Parser);
 4347         Parser->Stream_Prepare(Stream_Video);
 4348         Parser->Fill(Stream_Video, 0, Video_Format, "MPEG Video");
 4349         switch (FromTS_stream_type)
 4350         {
 4351             case 0x01 : Parser->Fill(Stream_Video, 0, Video_Codec, "MPEG-1V");
 4352                         Parser->Fill(Stream_Video, 0, Video_Format_Version, "Version 1"); break;
 4353             case 0x02 : Parser->Fill(Stream_Video, 0, Video_Codec, "MPEG-2V");
 4354                         Parser->Fill(Stream_Video, 0, Video_Format_Version, "Version 2"); break;
 4355             default   : Parser->Fill(Stream_Video, 0, Video_Codec, "MPEG-V");
 4356         }
 4357     #endif
 4358     return Parser;
 4359 }
 4360 
 4361 //---------------------------------------------------------------------------
 4362 File__Analyze* File_MpegPs::ChooseParser_Mpeg4v()
 4363 {
 4364     //Filling
 4365     #if defined(MEDIAINFO_MPEG4V_YES)
 4366         File_Mpeg4v* Parser=new File_Mpeg4v;
 4367         #if MEDIAINFO_DEMUX
 4368             if (Config->Demux_Unpacketize_Get())
 4369             {
 4370                 Demux_UnpacketizeContainer=false; //No demux from this parser
 4371                 Demux_Level=4; //Intermediate
 4372                 Parser->Demux_Level=2; //Container
 4373                 Parser->Demux_UnpacketizeContainer=true;
 4374             }
 4375         #endif //MEDIAINFO_DEMUX
 4376     #else
 4377         //Filling
 4378         File__Analyze* Parser=new File_Unknown();
 4379         Open_Buffer_Init(Parser);
 4380         Parser->Stream_Prepare(Stream_Video);
 4381         Parser->Fill(Stream_Video, 0, Video_Codec, "MPEG-4V");
 4382         Parser->Fill(Stream_Video, 0, Video_Format, "MPEG-4 Visual");
 4383     #endif
 4384     return Parser;
 4385 }
 4386 
 4387 //---------------------------------------------------------------------------
 4388 File__Analyze* File_MpegPs::ChooseParser_Avc()
 4389 {
 4390     //Filling
 4391     #if defined(MEDIAINFO_AVC_YES)
 4392         File_Avc* Parser=new File_Avc;
 4393         #if MEDIAINFO_DEMUX
 4394             if (Config->Demux_Unpacketize_Get())
 4395             {
 4396                 Demux_UnpacketizeContainer=false; //No demux from this parser
 4397                 Demux_Level=4; //Intermediate
 4398                 Parser->Demux_Level=2; //Container
 4399                 Parser->Demux_UnpacketizeContainer=true;
 4400             }
 4401         #endif //MEDIAINFO_DEMUX
 4402     #else
 4403         //Filling
 4404         File__Analyze* Parser=new File_Unknown();
 4405         Open_Buffer_Init(Parser);
 4406         Parser->Stream_Prepare(Stream_Video);
 4407         Parser->Fill(Stream_Video, 0, Video_Codec,  "AVC");
 4408         Parser->Fill(Stream_Video, 0, Video_Format, "AVC");
 4409     #endif
 4410     return Parser;
 4411 }
 4412 
 4413 //---------------------------------------------------------------------------
 4414 File__Analyze* File_MpegPs::ChooseParser_Hevc()
 4415 {
 4416     //Filling
 4417     #if defined(MEDIAINFO_HEVC_YES)
 4418         File_Hevc* Parser=new File_Hevc;
 4419         #if MEDIAINFO_DEMUX
 4420             if (Config->Demux_Unpacketize_Get())
 4421             {
 4422                 Demux_UnpacketizeContainer=false; //No demux from this parser
 4423                 Demux_Level=4; //Intermediate
 4424                 Parser->Demux_Level=2; //Container
 4425                 Parser->Demux_UnpacketizeContainer=true;
 4426             }
 4427         #endif //MEDIAINFO_DEMUX
 4428     #else
 4429         //Filling
 4430         File__Analyze* Parser=new File_Unknown();
 4431         Open_Buffer_Init(Parser);
 4432         Parser->Stream_Prepare(Stream_Video);
 4433         Parser->Fill(Stream_Video, 0, Video_Codec,  "HEVC");
 4434         Parser->Fill(Stream_Video, 0, Video_Format, "HEVC");
 4435     #endif
 4436     return Parser;
 4437 }
 4438 //---------------------------------------------------------------------------
 4439 File__Analyze* File_MpegPs::ChooseParser_VC1()
 4440 {
 4441     //Filling
 4442     #if defined(MEDIAINFO_VC1_YES)
 4443         File_Vc1* Parser=new File_Vc1;
 4444         #if MEDIAINFO_DEMUX
 4445             if (Config->Demux_Unpacketize_Get())
 4446             {
 4447                 Demux_UnpacketizeContainer=false; //No demux from this parser
 4448                 Demux_Level=4; //Intermediate
 4449                 Parser->Demux_Level=2; //Container
 4450                 Parser->Demux_UnpacketizeContainer=true;
 4451             }
 4452         #endif //MEDIAINFO_DEMUX
 4453     #else
 4454         //Filling
 4455         File__Analyze* Parser=new File_Unknown();
 4456         Open_Buffer_Init(Parser);
 4457         Parser->Stream_Prepare(Stream_Video);
 4458         Parser->Fill(Stream_Video, 0, Video_Codec,  "VC-1");
 4459         Parser->Fill(Stream_Video, 0, Video_Format, "VC-1");
 4460     #endif
 4461     return Parser;
 4462 }
 4463 
 4464 //---------------------------------------------------------------------------
 4465 File__Analyze* File_MpegPs::ChooseParser_Dirac()
 4466 {
 4467     //Filling
 4468     #if defined(MEDIAINFO_DIRAC_YES)
 4469         File__Analyze* Parser=new File_Dirac;
 4470     #else
 4471         //Filling
 4472         File__Analyze* Parser=new File_Unknown();
 4473         Open_Buffer_Init(Parser);
 4474         Parser->Stream_Prepare(Stream_Video);
 4475         Parser->Fill(Stream_Video, 0, Video_Codec,  "Dirac");
 4476         Parser->Fill(Stream_Video, 0, Video_Format, "Dirac");
 4477     #endif
 4478     return Parser;
 4479 }
 4480 
 4481 //---------------------------------------------------------------------------
 4482 File__Analyze* File_MpegPs::ChooseParser_Mpega()
 4483 {
 4484     //Filling
 4485     #if defined(MEDIAINFO_MPEGA_YES)
 4486         File_Mpega* Parser=new File_Mpega;
 4487         #if MEDIAINFO_DEMUX
 4488             if (Config->Demux_Unpacketize_Get())
 4489             {
 4490                 Demux_UnpacketizeContainer=false; //No demux from this parser
 4491                 Demux_Level=4; //Intermediate
 4492                 Parser->Demux_Level=2; //Container
 4493                 Parser->Demux_UnpacketizeContainer=true;
 4494             }
 4495         #endif //MEDIAINFO_DEMUX
 4496     #else
 4497         //Filling
 4498         File__Analyze* Parser=new File_Unknown();
 4499         Open_Buffer_Init(Parser);
 4500         Parser->Stream_Prepare(Stream_Audio);
 4501         Parser->Fill(Stream_Audio, 0, Audio_Format, "MPEG Audio");
 4502         switch (FromTS_stream_type)
 4503         {
 4504             case 0x03 : Parser->Fill(Stream_Audio, 0, Audio_Codec,  "MPEG-1A");
 4505                         Parser->Fill(Stream_Audio, 0, Audio_Format_Version, "Version 1"); break;
 4506             case 0x04 : Parser->Fill(Stream_Audio, 0, Audio_Codec,  "MPEG-2A");
 4507                         Parser->Fill(Stream_Audio, 0, Audio_Format_Version, "Version 2"); break;
 4508             default   : Parser->Fill(Stream_Audio, 0, Audio_Codec,  "MPEG-A"); break;
 4509         }
 4510     #endif
 4511     return Parser;
 4512 }
 4513 
 4514 //---------------------------------------------------------------------------
 4515 File__Analyze* File_MpegPs::ChooseParser_Adts()
 4516 {
 4517     //Filling
 4518     #if defined(MEDIAINFO_AAC_YES)
 4519         File_Aac* Parser=new File_Aac;
 4520         Parser->Mode=File_Aac::Mode_ADTS;
 4521         #if MEDIAINFO_DEMUX
 4522             if (Config->Demux_Unpacketize_Get())
 4523             {
 4524                 Demux_UnpacketizeContainer=false; //No demux from this parser
 4525                 Demux_Level=4; //Intermediate
 4526                 Parser->Demux_Level=2; //Container
 4527                 Parser->Demux_UnpacketizeContainer=true;
 4528             }
 4529         #endif //MEDIAINFO_DEMUX
 4530     #else
 4531         //Filling
 4532         File__Analyze* Parser=new File_Unknown();
 4533         Open_Buffer_Init(Parser);
 4534         Parser->Stream_Prepare(Stream_Audio);
 4535         Parser->Fill(Stream_Audio, 0, Audio_Codec,  "AAC");
 4536         Parser->Fill(Stream_Audio, 0, Audio_Format, "AAC");
 4537         Parser->Fill(Stream_Audio, 0, Audio_MuxingMode, "ADTS");
 4538     #endif
 4539     return Parser;
 4540 }
 4541 
 4542 //---------------------------------------------------------------------------
 4543 File__Analyze* File_MpegPs::ChooseParser_Latm()
 4544 {
 4545     //Filling
 4546     #if defined(MEDIAINFO_AAC_YES)
 4547         File_Aac* Parser=new File_Aac;
 4548         Parser->Mode=File_Aac::Mode_LATM;
 4549         #if MEDIAINFO_DEMUX
 4550             if (Config->Demux_Unpacketize_Get())
 4551             {
 4552                 Demux_UnpacketizeContainer=false; //No demux from this parser
 4553                 Demux_Level=4; //Intermediate
 4554                 Parser->Demux_Level=2; //Container
 4555                 Parser->Demux_UnpacketizeContainer=true;
 4556             }
 4557         #endif //MEDIAINFO_DEMUX
 4558     #else
 4559         //Filling
 4560         File__Analyze* Parser=new File_Unknown();
 4561         Open_Buffer_Init(Parser);
 4562         Parser->Stream_Prepare(Stream_Audio);
 4563         Parser->Fill(Stream_Audio, 0, Audio_Codec,  "AAC");
 4564         Parser->Fill(Stream_Audio, 0, Audio_Format, "AAC");
 4565         Parser->Fill(Stream_Audio, 0, Audio_MuxingMode, "LATM");
 4566     #endif
 4567     return Parser;
 4568 }
 4569 
 4570 //---------------------------------------------------------------------------
 4571 File__Analyze* File_MpegPs::ChooseParser_AC3()
 4572 {
 4573     //Filling
 4574     #if defined(MEDIAINFO_AC3_YES)
 4575         File_Ac3* Parser=new File_Ac3();
 4576         #if MEDIAINFO_DEMUX
 4577             if (Config->Demux_Unpacketize_Get())
 4578             {
 4579                 Demux_UnpacketizeContainer=false; //No demux from this parser
 4580                 Demux_Level=4; //Intermediate
 4581                 Parser->Demux_Level=2; //Container
 4582                 Parser->Demux_UnpacketizeContainer=true;
 4583             }
 4584         #endif //MEDIAINFO_DEMUX
 4585     #else
 4586         //Filling
 4587         File__Analyze* Parser=new File_Unknown();
 4588         Open_Buffer_Init(Parser);
 4589         Parser->Stream_Prepare(Stream_Audio);
 4590         Parser->Fill(Stream_Audio, 0, Audio_Format, private_stream_1_ID==0x83?"E-AC-3":"AC-3");
 4591         Parser->Fill(Stream_Audio, 0, Audio_Codec, private_stream_1_ID==0x83?"AC3+":"AC3");
 4592     #endif
 4593     return Parser;
 4594 }
 4595 
 4596 //---------------------------------------------------------------------------
 4597 File__Analyze* File_MpegPs::ChooseParser_AC4()
 4598 {
 4599     //Filling
 4600     #if defined(MEDIAINFO_AC4_YES)
 4601         File_Ac4* Parser=new File_Ac4();
 4602         #if MEDIAINFO_DEMUX
 4603             if (Config->Demux_Unpacketize_Get())
 4604             {
 4605                 Demux_UnpacketizeContainer=false; //No demux from this parser
 4606                 Demux_Level=4; //Intermediate
 4607                 Parser->Demux_Level=2; //Container
 4608                 Parser->Demux_UnpacketizeContainer=true;
 4609             }
 4610         #endif //MEDIAINFO_DEMUX
 4611     #else
 4612