⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.~pas

📁 用delphi写的一款分析TS码流的软件
💻 ~PAS
📖 第 1 页 / 共 2 页
字号:
//Copyright (c) 2008,厦门大学通信工程系
//All rights reserved.
//author:victor_chen

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, Menus, MPlayer, ActnList, ToolWin,
  ImgList;

type
  TFm_MDIParent = class(TForm)
    Memo1: TMemo;
    OpenDialog1: TOpenDialog;
    Memo2: TMemo;
    MainMenu1: TMainMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Memo3: TMemo;
    N3: TMenuItem;
    PAT1: TMenuItem;
    PMT1: TMenuItem;
    N4: TMenuItem;
    MediaPlayer1: TMediaPlayer;
    N5: TMenuItem;
    N6: TMenuItem;
    ToolBar1: TToolBar;
    ToolButton3: TToolButton;
    ImageList1: TImageList;
    StatusBar1: TStatusBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    N7: TMenuItem;
    PES1: TMenuItem;
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure PAT1Click(Sender: TObject);
    procedure PMT1Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure N7Click(Sender: TObject);
    procedure PES1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Fm_MDIParent: TFm_MDIParent;
  g_pmt_pid :array[0..8191] of integer; //TS码流中PID范围1-1FFF
  g_elementary_pid1,g_elementary_pid2:integer;
  pcr_base,pcr_ext,pcr :longint;
  
implementation

{$R *.dfm}

procedure TFm_MDIParent.N2Click(Sender: TObject);

begin

    if OpenDialog1.Execute then
      Memo2.Lines.LoadFromFile(OpenDialog1.FileName)
    else
      Memo2.Lines.Clear;

end;
//以下procedure实现统计功能
procedure TFm_MDIParent.N3Click(Sender: TObject);
var
  sb_num,tei_0_num,tei_1_num,pusi_0_num,pusi_1_num,tp_0_num,tp_1_num,
  tsc_00_num,tsc_01_num,tsc_10_num,tsc_11_num,
  afc_00_num,afc_01_num,afc_10_num,afc_11_num :LongInt;
  f:File;
  buffer :array[0..187] of Byte;
  AmtTransferred,i: Integer;
  pid_num :array[0..8191] of integer;
  count,jj :LongInt;
  ch :byte;
begin
    count:=0;
    sb_num:=0;
	  tei_0_num:=0;
    tei_1_num:=0;
    pusi_0_num:=0;
    pusi_1_num:=0;
    tp_0_num:=0;
    tp_1_num:=0;
	  tsc_00_num:=0;
    tsc_01_num:=0;
    tsc_10_num:=0;
    tsc_11_num:=0;
	  afc_00_num:=0;
    afc_01_num:=0;
    afc_10_num:=0;
    afc_11_num:=0;
    for i:=0 to 8191 do
    begin
      pid_num[i] :=0;
    end;

    AssignFile(f,OpenDialog1.FileName);
    Reset(f,1);
    Memo1.Clear;

    ch :=0;
    jj :=0;
    //处理第一个字节不是47的情况,jj是用来计数从文件头到第一个47的字节数
    //之后再reset,读过jj-1个字节,再开始写入数组就是从47开始了
    while ch<>71 do
    begin
      BlockRead(f,ch,1,AmtTransferred);
      jj :=jj+1;
    end;
    reset(f,1);
    BlockRead(f,buffer,jj-1);

    while not eof(f) do
    begin
        BlockRead(f,buffer,188,AmtTransferred);
        count :=count+1;
        if buffer[0]=71 then //decimal(71)=hex(47)
        sb_num :=sb_num+1;

        //tei 统计
		    if ((buffer[1]and 128) div 128)=1 then
        tei_1_num :=tei_1_num+1
		    else
        tei_0_num :=tei_0_num+1;

	      //pusi 统计
		    if (buffer[1] and 64) div 64 =1  then
        pusi_1_num :=pusi_1_num+1
		    else
        pusi_0_num :=pusi_0_num+1;

        //tp 统计
		    if (buffer[1] and 32) div 32 =1 then
        tp_1_num :=tp_1_num+1
		    else 
        tp_0_num :=tp_0_num+1;

        //PID统计
        for i:=0 to 8191 do
        begin
          if (buffer[1] and 31)*256+buffer[2] = i then
          pid_num[i] :=pid_num[i]+1;
        end;

        //tsc 统计
		    if (buffer[3] and 192) div 64=0 then
        tsc_00_num :=tsc_00_num+1
		    else if (buffer[3] and 192) div 64=1 then
        tsc_01_num :=tsc_01_num+1
		    else if (buffer[3] and 192) div 64=2 then
        tsc_10_num :=tsc_10_num+1
		    else
        tsc_11_num :=tsc_11_num+1;

	      //afc 统计
		    if (buffer[3] and 48) div 16=0 then
        afc_00_num :=afc_00_num+1
		    else if (buffer[3] and 48) div 16=1 then
        afc_01_num :=afc_01_num+1
		    else if (buffer[3] and 48) div 16=3 then
        afc_11_num :=afc_11_num+1
		    else
        begin
        afc_10_num :=afc_10_num+1;
        if (count<1000) then
        memo3.Lines.Add(' =' + IntToStr(count));
        end;
    end;
    memo1.Lines.Add('Packet_num =' + IntToStr(count));
    memo1.Lines.Add('number of sb =' + IntToStr(sb_num));
    memo1.Lines.Add('');
    memo1.Lines.Add('tei_1_num =' + IntToStr(tei_1_num));
    memo1.Lines.Add('tei_0_num =' + IntToStr(tei_0_num));
    memo1.Lines.Add('');
    memo1.Lines.Add('pusi_1_num =' + IntToStr(pusi_1_num));
    memo1.Lines.Add('pusi_0_num =' + IntToStr(pusi_0_num));
    memo1.Lines.Add('');
    memo1.Lines.Add('tp_1_num =' + IntToStr(tp_1_num));
    memo1.Lines.Add('tp_0_num =' + IntToStr(tp_0_num));
    memo1.Lines.Add('');
    for i:=0 to 8191 do
    begin
      if pid_num[i]<>0 then
      memo1.Lines.Add('pid='+IntToStr(i)+'  个数为'+IntToStr(pid_num[i]));
    end;
    memo1.Lines.Add('');
    memo1.Lines.Add('tsc_00_num =' + IntToStr(tsc_00_num));
    memo1.Lines.Add('tsc_01_num =' + IntToStr(tsc_01_num));
    memo1.Lines.Add('tsc_10_num =' + IntToStr(tsc_10_num));
    memo1.Lines.Add('tsc_11_num =' + IntToStr(tsc_11_num));
    memo1.Lines.Add('');
    memo1.Lines.Add('afc_00_num =' + IntToStr(afc_00_num));
    memo1.Lines.Add('afc_01_num =' + IntToStr(afc_01_num));
    memo1.Lines.Add('afc_10_num =' + IntToStr(afc_10_num));
    memo1.Lines.Add('afc_11_num =' + IntToStr(afc_11_num));

    CloseFile(f);

end;

//PAT
procedure TFm_MDIParent.PAT1Click(Sender: TObject);
var
  found,N,length,i,j,jj:integer;
  f:File;
  buffer :array[0..187] of Byte;
  AmtTransferred: Integer;
  ch :byte;

begin
    found :=0;

    AssignFile(f,OpenDialog1.FileName);
    Reset(f,1);
    Memo1.Clear;
    Memo3.Clear;

    ch :=0;
    jj :=0;
    //处理第一个字节不是47的情况,jj是用来计数从文件头到第一个47的字节数
    //之后再reset,读过jj-1个字节,再开始写入数组就是从47开始了
    while ch<>71 do
    begin
      BlockRead(f,ch,1,AmtTransferred);
      jj :=jj+1;
    end;
    reset(f,1);
    BlockRead(f,buffer,jj-1);


    while (not eof(f)) and (found=0) do
    begin
        BlockRead(f,buffer,188,AmtTransferred);

        if (( (buffer[1] and 31)*256 ) + buffer[2])=0 then
        begin
            found :=1;
            for i:= 0 to 19 do
            begin
              memo1.Lines.Add(IntToHex(buffer[i*9+0],1)+'  '+IntToHex(buffer[i*9+1],1)+'  '+
                IntToHex(buffer[i*9+2],1)+'  '+IntToHex(buffer[i*9+3],1)+'  '+
                IntToHex(buffer[i*9+4],1)+'  '+IntToHex(buffer[i*9+5],1)+'  '+
                IntToHex(buffer[i*9+6],1)+'  '+IntToHex(buffer[i*9+7],1)+'  '+
                IntToHex(buffer[i*9+8],1))
            end;
            memo1.Lines.Add(IntToHex(buffer[180],1)+'  '+IntToHex(buffer[181],1)+'  '+
            IntToHex(buffer[182],1)+'  '+IntToHex(buffer[183],1)+'  '+
            IntToHex(buffer[184],1)+'  '+IntToHex(buffer[185],1)+'  '+
            IntToHex(buffer[186],1)+'  '+IntToHex(buffer[187],1) );

            memo1.Lines.Add(' ');
            memo3.Lines.Add('table_id= '+IntToStr(buffer[5]));
            memo3.Lines.Add('section_syntax_indicator ='+IntToStr((buffer[6] and 128) div 128 ));

            length :=(buffer[6] and 15)*256+buffer[7];
            memo3.Lines.Add('section_length ='+IntToStr(length));
            memo3.Lines.Add('version_number ='+IntToStr(buffer[10] and 62));
            memo3.Lines.Add('current_next_indicator ='+IntToStr(buffer[10] and 1));
            memo3.Lines.Add('section_number ='+IntToStr(buffer[11]));
            memo3.Lines.Add('last_section_number ='+IntToStr(buffer[12]));

            N :=length-9;
            j :=13;
            i :=0;
            while j<13+N do
              begin
                  memo3.Lines.Add('program_number='+IntToStr(buffer[j]*256+buffer[j+1] )
                  +'   '+'pid=' +IntToStr( (buffer[j+2] and 31)*256+buffer[j+3]) );

                  g_pmt_pid[i] :=(buffer[j+2] and 31)*256+buffer[j+3];
                  i :=i+1;
                  j :=j+4;
              end;
              //因为前面最后读到j+3 因此从j+4开始的4个字节是CRC 但是前面最后一句j :=j+4,所以后面要先减4
              j :=j-4;
            memo3.Lines.Add('CRC :'+IntToHex(buffer[j+4],1)+'  '+IntToHex(buffer[j+5],1)
            +'  '+IntToHex(buffer[j+6],1)+'  '+IntToHex(buffer[j+7],1) );


        end;

    end;
    CloseFile(f);
end;

procedure TFm_MDIParent.PMT1Click(Sender: TObject);
var
  found,i,j,jj:integer;
  f:File;
  ch :byte;
  buffer :array[0..187] of Byte;
  string_type :array[1..4] of string;
  AmtTransferred: Integer;
  inputstring :string;
  ti,ssi,sl,pn,vn,pp,pil,pil_const,component_length,es_info_length,crc_start:longint;
begin
    found :=0;

    string_type[1] :='ISO/IEC 11172 Vedio';
    string_type[2] :='ISO/IEC 13818-2 Vedio';
    string_type[3] :='ISO/IEC 11172 Audio';
    string_type[4] :='ISO/IEC 13818-3 Audio';

    AssignFile(f,OpenDialog1.FileName);
    Reset(f,1);
    Memo1.Clear;
    Memo3.Clear;

    ch :=0;
    jj :=0;
    while ch<>71 do
    begin
      BlockRead(f,ch,1,AmtTransferred);
      jj :=jj+1;
    end;
    reset(f,1);
    BlockRead(f,buffer,jj-1);

    inputstring :=inputbox('Input Box','请输入指定program的id','default');

    while (not eof(f)) and (found=0) do
    begin
        BlockRead(f,buffer,188,AmtTransferred);

        if (( (buffer[1] and 31)*256 ) + buffer[2])=StrToInt(inputstring) then
        begin
            found :=1;
            for i:= 0 to 19 do
            begin
                memo1.Lines.Add(IntToHex(buffer[i*9+0],1)+'  '+IntToHex(buffer[i*9+1],1)+'  '+
                IntToHex(buffer[i*9+2],1)+'  '+IntToHex(buffer[i*9+3],1)+'  '+
                IntToHex(buffer[i*9+4],1)+'  '+IntToHex(buffer[i*9+5],1)+'  '+
                IntToHex(buffer[i*9+6],1)+'  '+IntToHex(buffer[i*9+7],1)+'  '+
                IntToHex(buffer[i*9+8],1))
            end;
            memo1.Lines.Add(IntToHex(buffer[180],1)+'  '+IntToHex(buffer[181],1)+'  '+
            IntToHex(buffer[182],1)+'  '+IntToHex(buffer[183],1)+'  '+
            IntToHex(buffer[184],1)+'  '+IntToHex(buffer[185],1)+'  '+
            IntToHex(buffer[186],1)+'  '+IntToHex(buffer[187],1) );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -