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

📄 decode_pat.c

📁 数字电视广播系统mpeg-2的ts流的解复用和分析软件
💻 C
字号:
#include "config.h"#include <stdio.h>#include <stdlib.h>//#include <unistd.h>#include <fcntl.h>#if defined(HAVE_INTTYPES_H)#include <inttypes.h>#elif defined(HAVE_STDINT_H)#include <stdint.h>#endif/* The libdvbpsi distribution defines DVBPSI_DIST */#ifdef DVBPSI_DIST#include "../src/dvbpsi.h"#include "../src/psi.h"#include "../src/tables/pat.h"
#include "../src/tables/pmt.h"
#include "../src/tables/sdt.h"
#else#include <dvbpsi/dvbpsi.h>#include <dvbpsi/psi.h>#include <dvbpsi/pat.h>#endif
#define PID_COUNT  8 * 1024
uint32_t pid_array[PID_COUNT];	/****************************************************************************** ReadPacket*****************************************************************************/int ReadPacket(int i_fd, uint8_t* p_dst){	int i = 187;	int i_rc = 1;		p_dst[0] = 0;		while((p_dst[0] != 0x47) && (i_rc > 0))	{		//    i_rc = read(i_fd, p_dst, 1);
		i_rc = fread(p_dst, 1, 1, i_fd );	}		while((i != 0) && (i_rc > 0))	{		i_rc = fread(p_dst + 188 - i, 1, i, i_fd);
		//    i_rc = read(i_fd, p_dst + 188 - i, i);		if(i_rc >= 0)			i -= i_rc;	}		return (i == 0) ? 1 : 0;}
/*****************************************************************************
 * DumpDescriptors
 *****************************************************************************/
void DumpDescriptors(const char* str, dvbpsi_descriptor_t* p_descriptor)
{
  while(p_descriptor)
  {
    int i;
    printf("%s 0x%02x : \"", str, p_descriptor->i_tag);
    for(i = 0; i < p_descriptor->i_length; i++)
      printf("%c", p_descriptor->p_data[i]);
    printf("\"\n");
    p_descriptor = p_descriptor->p_next;
  }
};


/*****************************************************************************
 * DumpSDT
 *****************************************************************************/
void DumpSDT(void* p_zero, dvbpsi_sdt_t* p_sdt)
{
  dvbpsi_sdt_service_t* p_service = p_sdt->p_first_service;
  printf(  "\n");
  printf(  "New active SDT\n");
  printf(  "  ts_id : %d\n",
         p_sdt->i_ts_id);
  printf(  "  version_number : %d\n",
         p_sdt->i_version);
  printf(  "  network_id        : %d\n",
         p_sdt->i_network_id);
  printf(  "    | service_id \n");
  while(p_service)
  {
    printf("    | 0x%02x \n",
           p_service->i_service_id);
    DumpDescriptors("    |  ]", p_service->p_first_descriptor);
    p_service = p_service->p_next;
  }
  dvbpsi_DeleteSDT(p_sdt);
}

/*****************************************************************************
 * NewSubtable
 *****************************************************************************/
void NewSubtable(void * p_zero, dvbpsi_handle h_dvbpsi,
                 uint8_t i_table_id, uint16_t i_extension)
{
  if(i_table_id == 0x42)
  {
    dvbpsi_AttachSDT(h_dvbpsi, i_table_id, i_extension, DumpSDT, NULL);
  }
}

/*****************************************************************************
 * DumpPMT
 *****************************************************************************/
void DumpPMT(void* p_zero, dvbpsi_pmt_t* p_pmt)
{
  dvbpsi_pmt_es_t* p_es = p_pmt->p_first_es;
  printf(  "\n");
  printf(  "New active PMT\n");
  printf(  "  program_number : %d\n",
         p_pmt->i_program_number);
  printf(  "  version_number : %d\n",
         p_pmt->i_version);
  printf(  "  PCR_PID        : 0x%x (%d)\n",
         p_pmt->i_pcr_pid, p_pmt->i_pcr_pid);
  DumpDescriptors("    ]", p_pmt->p_first_descriptor);
  printf(  "    | type @ elementary_PID\n");
  while(p_es)
  {
    printf("    | 0x%02x @ 0x%x (%d)\n",
           p_es->i_type, p_es->i_pid, p_es->i_pid);
    DumpDescriptors("    |  ]", p_es->p_first_descriptor);
    p_es = p_es->p_next;
  }
  dvbpsi_DeletePMT(p_pmt);
}

dvbpsi_handle h_pmt ;
/****************************************************************************** DumpPAT*****************************************************************************/void DumpPAT(void* p_zero, dvbpsi_pat_t* p_pat){	dvbpsi_pat_program_t* p_program = p_pat->p_first_program;	printf(  "\n");	printf(  "New PAT\n");	printf(  "  transport_stream_id : %d\n", p_pat->i_ts_id);	printf(  "  version_number      : %d\n", p_pat->i_version);	printf(  "    | program_number @ [NIT|PMT]_PID\n");

	while(p_program)	{		printf("    | 0x%x (%14d) @ 0x%x (%d)\n",			p_program->i_number, p_program->i_number, p_program->i_pid, p_program->i_pid);
		
		h_pmt = dvbpsi_AttachPMT(p_program->i_number, DumpPMT, NULL);

		pid_array[p_program->i_pid] = h_pmt;
		p_program = p_program->p_next;
			}	printf(  "  active              : %d\n", p_pat->b_current_next);	dvbpsi_DeletePAT(p_pat);}/****************************************************************************** main*****************************************************************************/
int main(int i_argc, char* pa_argv[]){
	

	FILE* i_fd;
	uint8_t data[188];
	dvbpsi_handle h_dvbpsi_pat, h_dvbpsi_sdt;
	int b_ok;
	int i;

	memset(pid_array, 0,  sizeof(uint16_t) * PID_COUNT);
	
	if(i_argc != 2)
		return 1;
	
	i_fd = fopen(pa_argv[1], "rb");
	
	h_dvbpsi_pat = dvbpsi_AttachPAT(DumpPAT, NULL);
	h_dvbpsi_sdt = dvbpsi_AttachDemux(NewSubtable, NULL);
	pid_array[0x11] = h_dvbpsi_sdt;
	
	b_ok = ReadPacket(i_fd, data);
	
	while(b_ok)
	{
		uint16_t i_pid = ((uint16_t)(data[1] & 0x1f) << 8) + data[2];
		if(i_pid == 0x0)
		{
			dvbpsi_PushPacket(h_dvbpsi_pat, data);			
		}
		else
		{			
			if(0 != pid_array[i_pid] )
			{
				dvbpsi_PushPacket( pid_array[i_pid], data);
			}
		}
		b_ok = ReadPacket(i_fd, data);
	}
	
	dvbpsi_DetachPAT(h_dvbpsi_pat);
	dvbpsi_DetachDemux(h_dvbpsi_sdt);
	pid_array[0x11] = 0;


	for (i = 0; i < PID_COUNT; i ++)
	{
		if(0 != pid_array[i])
			dvbpsi_DetachPMT( pid_array[i], data);
				
	}
	fclose(i_fd);
	
	
	return 0;}

⌨️ 快捷键说明

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