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

📄 unit1.~pas

📁 用delphi写的一款分析TS码流的软件
💻 ~PAS
📖 第 1 页 / 共 2 页
字号:

            memo1.Lines.Add(' ');
            ti :=buffer[5];
            ssi :=(buffer[6] and 128) div 128;
            sl :=(buffer[6] and 15)*256+buffer[7];//section_length
            pn :=buffer[8]*256+buffer[9];
            vn :=(buffer[10] and 62) div 2;
            pp :=(buffer[13] and 31)*256+buffer[14];
            pil :=(buffer[15] and 15)*256+buffer[16];
            pil_const:=pil;//后面要用到此处的pil值,但是pil本身在下面的一个循环中值会改变,
                          //所以这边用pil_const存储它

            memo3.Lines.Add('table_id = '+IntToStr(ti));
            memo3.Lines.Add('section_syntax_indicator = '+IntToStr(ssi));
            memo3.Lines.Add('program_num = '+IntToStr(pn));
            memo3.Lines.Add('version_num = '+IntToStr(vn));
            memo3.Lines.Add('pcr_pid = '+IntToStr(pp));
            memo3.Lines.Add('program_info_length = '+IntToStr(pil));//It specifies the number
            //of bytes of  the descriptors immediately following the program_info_length field.
            // 在birds文件中,此值为0,所以birds 中是没有program_info_descriptors的
            //一个program_info_descriptors占4个字节,在122页的table2-55
            j :=1;

            while pil>0 do
                begin
                    memo3.Lines.Add('descriptor_tag = '+IntToStr(buffer[16+j]));
                    memo3.Lines.Add('descriptor_length = '+IntToStr(buffer[16+j+1]));
                    memo3.Lines.Add('descriptor_data :');
                    for i:=1 to buffer[16+j+1] do
                      begin
                      memo3.Lines.Add('           '+IntToStr(buffer[16+j+1+i]));
                      end;

                    pil :=pil-4;
                    j :=j+4;
                end;
           //由section_length 推算component所占的字节,进而确定N1,N2由es_info_length决定
           component_length :=sl-9-pil_const-4;
           memo3.Lines.Add('');
           memo3.Lines.Add('stream_type = '+IntToStr(buffer[16+pil_const+1])+'=>>'
           +string_type[buffer[16+pil_const+1]]);//pil在buffer[16],接下来的descriptor占了pil个字节,所以第一个stream_type是在buffer[16+pil+1]
           g_elementary_pid1 := (buffer[16+pil_const+2] and 31)*256+buffer[16+pil_const+3];
           memo3.Lines.Add('elementary_pid = '+IntToStr(g_elementary_pid1));
           es_info_length :=(buffer[16+pil_const+4] and 15)*256 + buffer[16+pil_const+5];
           memo3.Lines.Add('es_info_length = '+IntToStr(es_info_length));
           //差es descriptors
           memo3.Lines.Add('');

           if component_length > (5+es_info_length) then
              begin
                  memo3.Lines.Add('stream_type = '+IntToStr(buffer[16+pil_const+5+es_info_length+1])+'=>>'
                  +string_type[buffer[16+pil_const+5+es_info_length+1]]);

                  g_elementary_pid2 := (buffer[16+pil_const+5+es_info_length+2] and 31)*256
                  +buffer[16+pil_const+5+es_info_length+3];

                  memo3.Lines.Add('elementary_pid = '+IntToStr(g_elementary_pid2));

                  es_info_length :=(buffer[16+pil_const+5+es_info_length+4] and 15)*256
                  + buffer[16+pil_const+5+es_info_length+5];

                  memo3.Lines.Add('es_info_length = '+IntToStr(es_info_length));
              end;

           crc_start :=16+pil_const+component_length+1;
           memo3.Lines.Add('CRC :'+IntToHex(buffer[crc_start],1)+'  '
           +IntToHex(buffer[crc_start+1],1)+'  '+IntToHex(buffer[crc_start+2],1)
           +'  '+IntToHex(buffer[crc_start+3],1) );

       end;


    end;
    CloseFile(f);
end;
//以下是包解析部分
//注意要先运行PMT后才可以,不然碰到elementary的包无法解析出
procedure TFm_MDIParent.N4Click(Sender: TObject);
var
  inputstring :string;
  f:File;
  buffer :array[0..187] of Byte;
  packet_num :string;
  count :longint;
  sb,tei,pusi,tp,tsc,afc,cc :Byte;
  pid,i,j :longint;
  AmtTransferred,N,length: Integer;
  ch :Byte;
  flag :Integer;//adaptation_field_control=2||3的标志
begin
  inputstring :=inputbox('inputbox','which packet','default');
  count :=1;
  flag :=0;
  AssignFile(f,OpenDialog1.FileName);
  Reset(f,1);

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

  packet_num :=inputstring;
  while count<>StrToInt(packet_num) do
  begin
      BlockRead(f,buffer,188);
      count:=count+1;
  end;




  BlockRead(f,buffer,188);
  Memo1.Clear;


      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) );

      sb :=buffer[0];
	    tei :=(buffer[1]and 128) div 128;
	    pusi :=(buffer[1]and 64) div 64;
	    tp :=(buffer[1]and 32) div 32;
	    pid :=( (buffer[1]and 31)*256 ) + buffer[2];
	    tsc :=(buffer[3]and 192) div 64;        //除以权值
	    afc :=(buffer[3]and 48) div 16;
	    cc :=buffer[3] and 15;

      Memo3.Clear;
      memo3.Lines.Add('sync_byte:'+'  '+IntToHex(sb,1));
      memo3.Lines.Add('transport error indicator:'+'  '+IntToHex(tei,1));
      memo3.Lines.Add('Payload Unit start indicator:'+'  '+IntToHex(pusi,1));
      memo3.Lines.Add('transport priority:'+'  '+IntToHex(tp,1));
      memo3.Lines.Add('pid:'+'  '+IntToHex(pid,1));
      memo3.Lines.Add('transport scrambling control:'+'  '+IntToHex(tsc,1));
      memo3.Lines.Add('adaptation field control:'+'  '+IntToHex(afc,1));
      memo3.Lines.Add('continuity control:'+'  '+IntToHex(cc,1));
      //以下根据pid的值分类讨论,PAT表,PMT表,PES,或填充包
      if pid=0 then
        begin
            memo3.Lines.Add('这是一个PAT表');
            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;
            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]) );
                  //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
        else if pid=8191 then
                memo3.Lines.Add('这是一个填充包')
        else if pid=g_elementary_pid1 then
            begin
                memo3.Lines.Add('这是一个视频包');
                flag :=1;
            end
        else if pid=g_elementary_pid2 then
            begin
                memo3.Lines.Add('这是一个音频包');
                flag :=1;
            end
        else
            begin
              for i:=0 to 8192 do
                //pmt_pid[i]中存的是所有PMT的pid号,只要其中之一与当前的这个包的pid一致,则
                //说明当前这个包为PMT表
                if g_pmt_pid[i]=pid then
                  begin
                    memo3.Lines.Add('这是一个PMT包');
                  end;
            end;

  if flag=1 then //分析调整段
    begin
      memo3.Lines.Add('');
      memo3.Lines.Add('adaptation_field_length :  '+IntToHex(buffer[4],1));
      memo3.Lines.Add('discontinuity_indicator :  '+IntToHex(((buffer[5] and 128) div 128),1));
      memo3.Lines.Add('random_access_indicator :  '+IntToHex(((buffer[5] and 64) div 64),1));
      memo3.Lines.Add('elementary_system_priority_indicator :  '+IntToHex(((buffer[5] and 32) div 32),1));
      memo3.Lines.Add('PCR_flag :  '+IntToHex(((buffer[5] and 16) div 16),1));
      memo3.Lines.Add('OPCR_flag :  '+IntToHex(((buffer[5] and 8) div 8),1));
      memo3.Lines.Add('splicing_poing_flag :  '+IntToHex(((buffer[5] and 4) div 4),1));
      memo3.Lines.Add('transport_private_data_flag :  '+IntToHex(((buffer[5] and 2) div 2),1));
      memo3.Lines.Add('adaptation_field_extension_flag :  '+IntToHex((buffer[5] and 1),1));
      memo3.Lines.Add('program_clock_reference_base :  '+IntToHex(buffer[6],1)
      +' '+IntToHex(buffer[7],1)+' '+IntToHex(buffer[8],1)+' '+IntToHex(buffer[9],1)
      +' '+IntToHex(((buffer[10] and 128) div 128),1));
      memo3.Lines.Add('插入段 :  '+IntToHex((buffer[10] and 126) div 2,1));
      memo3.Lines.Add('program_clock_reference_extension :  '+IntToHex((buffer[10] and 1),1)
      +' '+IntToHex(buffer[11],1));

      pcr_base :=buffer[6]*256*256*256*2 + buffer[7]*256*256*2 + buffer[8]*256*2 + buffer[9]*2
                 + (buffer[10] and 128) div 128;
      pcr_ext :=(buffer[10] and 1)*256 + buffer[11];
      pcr :=pcr_base*300 + pcr_ext;
      memo3.Lines.Add('PCR :'+IntToStr(pcr));

    end;

      CloseFile(f);
end;


procedure TFm_MDIParent.N5Click(Sender: TObject);
begin
    Close;
end;

procedure TFm_MDIParent.ToolButton3Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
      Memo2.Lines.LoadFromFile(OpenDialog1.FileName)
    else
      Memo2.Lines.Clear;
end;

procedure TFm_MDIParent.N7Click(Sender: TObject);
//var
  //Fm_MDIChild:TFm_MDIChild;
begin
  //Fm_MDIChild :=TFm_MDIChild.Create(Self);
  //Fm_MDIChild.Show;
end;

procedure TFm_MDIParent.PES1Click(Sender: TObject);
var
  found,i,jj:integer;
  f:File;
  ch :byte;
  buffer :array[0..187] of Byte;
  AmtTransferred: Integer;
  inputstring :string;
  sb,tei,pusi,tp,tsc,afc,cc :Byte;
  pid :longint;


begin
    AssignFile(f,OpenDialog1.FileName);
    Reset(f,1);
    Memo1.Clear;
    Memo3.Clear;
    ch :=0;
    jj :=0;
    found :=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) );

      sb :=buffer[0];
	    tei :=(buffer[1] and 128) div 128;
	    pusi :=(buffer[1] and 64) div 64;
	    tp :=(buffer[1] and 32) div 32;
	    pid :=( (buffer[1] and 31)*256 ) + buffer[2];
	    tsc :=(buffer[3] and 192) div 64;        //除以权值
	    afc :=(buffer[3] and 48) div 16;
	    cc :=buffer[3] and 15;
      memo3.Lines.Add('sync_byte:'+'  '+IntToHex(sb,1));
      memo3.Lines.Add('transport error indicator:'+'  '+IntToHex(tei,1));
      memo3.Lines.Add('Payload Unit start indicator:'+'  '+IntToHex(pusi,1));
      memo3.Lines.Add('transport priority:'+'  '+IntToHex(tp,1));
      memo3.Lines.Add('pid:'+'  '+IntToHex(pid,1));
      memo3.Lines.Add('transport scrambling control:'+'  '+IntToHex(tsc,1));
      memo3.Lines.Add('adaptation field control:'+'  '+IntToHex(afc,1));
      memo3.Lines.Add('continuity control:'+'  '+IntToHex(cc,1));
      memo3.Lines.Add('');
      memo3.Lines.Add('adaptation_field_length :  '+IntToHex(buffer[4],1));
      memo3.Lines.Add('discontinuity_indicator :  '+IntToHex(((buffer[5] and 128) div 128),1));
      memo3.Lines.Add('random_access_indicator :  '+IntToHex(((buffer[5] and 64) div 64),1));
      memo3.Lines.Add('elementary_system_priority_indicator :  '+IntToHex(((buffer[5] and 32) div 32),1));
      memo3.Lines.Add('PCR_flag :  '+IntToHex(((buffer[5] and 16) div 16),1));
      memo3.Lines.Add('OPCR_flag :  '+IntToHex(((buffer[5] and 8) div 8),1));
      memo3.Lines.Add('splicing_poing_flag :  '+IntToHex(((buffer[5] and 4) div 4),1));
      memo3.Lines.Add('transport_private_data_flag :  '+IntToHex(((buffer[5] and 2) div 2),1));
      memo3.Lines.Add('adaptation_field_extension_flag :  '+IntToHex((buffer[5] and 1),1));
      memo3.Lines.Add('program_clock_reference_base :  '+IntToHex(buffer[6],1)
      +' '+IntToHex(buffer[7],1)+' '+IntToHex(buffer[8],1)+' '+IntToHex(buffer[9],1)
      +' '+IntToHex(((buffer[10] and 128) div 128),1));
      memo3.Lines.Add('插入段 :  '+IntToHex((buffer[10] and 126),1));
      memo3.Lines.Add('program_clock_reference_extension :  '+IntToHex((buffer[10] and 1),1)
      +' '+IntToHex(buffer[11],1));

      pcr_base :=buffer[6]*256*256*256*2 + buffer[7]*256*256*2 + buffer[8]*256*2 + buffer[9]*2
                 + (buffer[10] and 128) div 128;
      pcr_ext :=(buffer[10] and 1)*256 + buffer[11];
      pcr :=pcr_base*300 + pcr_ext;
      memo3.Lines.Add('PCR :'+IntToStr(pcr));

    end;

end;
CloseFile(f);
end;

end.

⌨️ 快捷键说明

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