tweaks
[tridge/junkcode.git] / pes_parse.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <fcntl.h>
4 #include <sys/stat.h>
5 #include <sys/mman.h>
6
7 typedef unsigned char u8;
8
9 #define PAGE_SIZE 0x1000
10
11 static void scan_pes(u8 *p, size_t len)
12 {
13         u8 *p0 = p;
14         u8 *p1 = p;
15         int nPacketLen, nStreamID, nDTS, nPTS, nHeaderLen;
16
17         while (len > 6) {
18                 /* look for a PES start */
19                 while (len > 6 && (p[0] || p[1] || p[2] != 1)) { p++; len--; }
20
21                 nPacketLen = p[4] << 8 | p[5];
22                 nStreamID  = p[3];
23
24                 if (p[7] & 0x80) { /* PTS avail */
25                         nPTS  = (p[ 9] & 0x0E) << 29 ;
26                         nPTS |=  p[10]         << 22 ;
27                         nPTS |= (p[11] & 0xFE) << 14 ;
28                         nPTS |=  p[12]         <<  7 ;
29                         nPTS |= (p[13] & 0xFE) >>  1 ;
30                 } else {
31                         nPTS = 0;
32                 }
33     
34                 if (p[7] & 0x40) { /* DTS avail */
35                         nDTS  = (p[14] & 0x0E) << 29 ;
36                         nDTS |=  p[15]         << 22 ;
37                         nDTS |= (p[16] & 0xFE) << 14 ;
38                         nDTS |=  p[17]         <<  7 ;
39                         nDTS |= (p[18] & 0xFE) >>  1 ;
40                 } else {
41                         nDTS = 0;
42                 }
43
44
45                 nHeaderLen = p[8];
46
47                 printf("offset=0x%x xsize=0x%x stream_id=0x%x len=0x%x headerlen=%d pts=0x%x dts=0x%x\n", 
48                        (int)(p-p0), (int)(p-p1), nStreamID, nPacketLen, nHeaderLen, nPTS, nDTS);
49
50                 p1 = p;
51
52                 p    += nHeaderLen + 9;
53                 nPacketLen -= nHeaderLen + 3;
54         }
55
56 }
57
58 int main(int argc, char *argv[])
59 {
60         u8 *p;
61         int fd;
62         struct stat st;
63
64         fd = open(argv[1], O_RDONLY);
65         
66         fstat(fd, &st);
67
68         p = mmap(NULL, (st.st_size+(PAGE_SIZE-1))&~(PAGE_SIZE-1), PROT_READ, MAP_PRIVATE|MAP_FILE, fd, 0);
69         
70         scan_pes(p, st.st_size);
71         return 0;
72 }