Mailing List archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[linux-dvb] RFC



Hi,
	Can somebody point out if i'm doing something correct/wrong here ?

Manu
struct program {
    uint16_t pid;
    uint16_t program_number; 
};
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <stdint.h>
#include <linux/dvb/dmx.h>
#include "filter.h"


#define MAX_SECTION_SIZE 8192
#define MAX_PIDS 1000

#if !defined(DMX_FILTER_SIZE)
#define DMX_FILTER_SIZE 16
#endif





void parse_pmt(int fd)
{
	uint8_t buf[MAX_SECTION_SIZE];
	int bytes;

	unsigned char stream_type;
	unsigned char table_id, section_number, last_section_number;
	unsigned int section_length, pcr_pid, prog_desc = 0;

	unsigned int section_syntax_indicator, program_number, version_number, reserved;
	unsigned int es_descriptor, found_prog_ca_desc = 0, found_stream_ca_desc = 0;
	unsigned int program_info_length, current_next_indicator;
	unsigned int es_pid, es_info_length = 0;
	unsigned int temp = 0, i = 0;
	
	bytes = read(fd, buf, sizeof(buf));
	if (bytes < 0) {
		perror("read");
		if (errno != EOVERFLOW)
			exit(1);
	}


	
	table_id = (unsigned int) buf[0];
	section_syntax_indicator = buf[1] & 0x80;
	reserved = buf[1] & 0x0c;
	section_length = 0;
	section_length = ((section_length | (buf[1] & 0x0f)) << 8) | buf[2];
	program_number = 0;
	program_number = ((program_number | buf[3]) << 8) | buf[4];
	reserved = buf[5] & 0xc0;
	version_number = buf[5] & 0x3e;
	current_next_indicator = buf[5] & 0x01;
	section_number = (unsigned int) buf[6];
	last_section_number = (unsigned int) buf[7];
	reserved = buf[8] & 0xe0;
	pcr_pid = 0;
	pcr_pid = ((pcr_pid | (buf[8] & 0x1f)) << 8) | buf[9];
	reserved = buf[10] & 0xf0;
	program_info_length = 0;
	program_info_length = ((program_info_length | (buf[10] & 0x0f)) << 8) | buf[11]; 


	printf ("Table ID=[%d]\n", table_id);
	printf ("Section Syntax=[%d]\n", section_syntax_indicator);
	printf ("Section Length=[%d]\n", section_length);
	printf ("Program Number=[%d]\n", program_number);
	printf ("Version Number=[%d]\n", version_number);
	printf ("Current Next=[%d]\n", current_next_indicator);
	printf ("Section Number=[%d]\n", section_number);
	printf ("Last Section=[%d]\n", last_section_number);
	printf ("PCR PID=[%d]\n", pcr_pid);
	printf ("Program Length=[%d]\n", program_info_length);


	if (program_info_length) {

	    printf ("\n\nProgram Descriptors=[");
	    for (i = 0; i < program_info_length; i++) {
		printf ("%d ", buf[12 + i]);
		if (buf[12 + i] == 0x09) {
		    found_prog_ca_desc = 1;
		}
		prog_desc = i + 1;
	    }
	    
	    printf ("]\n\n");
	    if (found_prog_ca_desc) {
	    	printf ("Found CA descriptor @ program level\n");
	    }

	}
	
//	i = 12;
	i = prog_desc + 12;
	
	while ( i < (section_length - 4) ) {
	
		stream_type = (unsigned int) buf[i];
		reserved = buf[i + 1] & 0xe0;
		es_pid = 0;
		es_pid = ((es_pid | (buf[i + 1] & 0x1f)) << 8) | buf[i + 2];
		reserved = (buf[i + 3] & 0xf0);
		es_info_length = 0;
		es_info_length = (es_info_length | (buf[i + 3] & 0x0f) << 8) | buf[i + 4]; 

		i = i + 5;
		
		printf ("\ti=[%d] ES Type=[%d] ES pid=[%d] ES length=[%d]\n", i, stream_type, es_pid, es_info_length);
		
		if (es_info_length != 0) {

			printf ("\n\nES Descriptors = [");
			
			while (i < (temp + es_info_length + 17)) {				
				es_descriptor = buf[i];	

				if (es_descriptor == 0x09) {
					found_stream_ca_desc = 1;
				}
				
				printf (" %02x", es_descriptor);
				i++;
				
				if ( i == (section_length - 1) ) {
					break;
				}
			}
			temp = temp + es_info_length + 5;			
			printf ("]\n\n");


			if (found_stream_ca_desc) {
				printf ("Found CA desccriptor @ Stream level\n");					
			}
		}
	}	
}


void usage(void)
{
	fprintf(stderr, "usage: pmt_parse PMT_PID\n");
	exit(1);
}



int main(int argc, char *argv[])
{
	int dmxfd;
	unsigned long pid = 0, tid;
	char * dmxdev = "/dev/dvb/adapter0/demux0";
        unsigned char filter[DMX_FILTER_SIZE];
        unsigned char mask[DMX_FILTER_SIZE];

	int i, entries = 0;
	
	if (argc < 2 || argc > 3)
		usage();

	i = 0, entries = 0;
	/*	Read line after line till end		*/
	pid = strtoul (argv[1], NULL, 0);

	if (pid > 0x1fff)
		usage();
	tid = 0x100;

	memset(filter, '\0', sizeof(filter));
	memset(mask, '\0', sizeof(mask));

	if ((dmxfd = open(dmxdev, O_RDWR)) < 0){
		perror("open");
		return 1;
	}

	if (set_filter(dmxfd, pid, filter, mask) != 0)
		return 1;

	parse_pmt(dmxfd);
	close(dmxfd);

	return 0;
}
/*
	CA-driver Test utility for TwinHan DST

	Copyright (C) 2004 Manu Abraham
	
	The code is shamelessly derived from Johannes Stezenbach's 
	test_sections.c

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/dvb/dmx.h>
#include "filter.h"


int set_filter(int fd, unsigned int pid, const unsigned char* filter, const unsigned char* mask)
{
	struct dmx_sct_filter_params f;

	memset(&f.filter, 0, sizeof(struct dmx_filter));
	f.pid = (uint16_t) pid;
        memcpy(f.filter.filter, filter, DMX_FILTER_SIZE);
	memcpy(f.filter.mask, mask, DMX_FILTER_SIZE);
	f.timeout = 0;
	f.flags = DMX_IMMEDIATE_START | DMX_CHECK_CRC;
	
	if (ioctl(fd, DMX_SET_FILTER, &f) == -1) {
		perror("DMX_SET_FILTER");
		return 1;
	}
	return 0;
}
CC = gcc
CFLAGS = -g -O2 -W -Wall $(ARCH)

TARGETS = \
	pmt_parser



all: $(TARGETS)
pmt_parser: filter.o


clean:
	rm -f $(TARGETS) *.o
int set_filter(int fd, unsigned int pid, const unsigned char* filter, const unsigned char* mask);

Home | Main Index | Thread Index