dreambox-dvb-tools-v3-1.7
[showiframe.git] / showiframe.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <string.h>
5 #include <sys/ioctl.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8
9 #include <linux/dvb/video.h>
10
11 void c(int a)
12 {
13         if (a < 0)
14         {
15                 perror("ioctl");
16                 exit(6);
17         }
18 }
19
20 int main(int argc, char **argv)
21 {
22         struct stat s;
23         if (argc != 2)
24         {
25                 printf("usage: %s <iframe>\n", *argv);
26                 return 3;
27         }
28
29         int f = open(argv[1], O_RDONLY);
30         if (f < 0)
31         {
32                 perror(argv[1]);
33                 return 4;
34         }
35         fstat(f, &s);
36
37         int fd = open("/dev/dvb/adapter0/video0", O_WRONLY|O_NONBLOCK);
38
39         if (fd <= 0)
40         {
41                 perror("/dev/dvb/adapter0/video0");
42                 return 2;
43         }
44         else if (fork() != 0)
45                 return 0;
46         else
47         {
48                 size_t pos=0;
49                 int seq_end_avail = 0;
50                 unsigned char pes_header[] = { 0x00, 0x00, 0x01, 0xE0, 0x00, 0x00, 0x80, 0x00, 0x00 };
51                 unsigned char seq_end[] = { 0x00, 0x00, 0x01, 0xB7 };
52                 unsigned char iframe[s.st_size];
53                 unsigned char stuffing[8192];
54                 memset(stuffing, 0, 8192);
55                 read(f, iframe, s.st_size);
56                 ioctl(fd, VIDEO_SET_STREAMTYPE, 0); // set to mpeg2
57                 c(ioctl(fd, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_MEMORY));
58                 c(ioctl(fd, VIDEO_PLAY));
59                 c(ioctl(fd, VIDEO_CONTINUE));
60                 c(ioctl(fd, VIDEO_CLEAR_BUFFER));
61                 while(pos <= (s.st_size-4) && !(seq_end_avail = (!iframe[pos] && !iframe[pos+1] && iframe[pos+2] == 1 && iframe[pos+3] == 0xB7)))
62                         ++pos;
63                 if ((iframe[3] >> 4) != 0xE) // no pes header
64                         write(fd, pes_header, sizeof(pes_header));
65                 else {
66                         iframe[4] = iframe[5] = 0x00;
67                 }
68                 write(fd, iframe, s.st_size);
69                 if (!seq_end_avail)
70                         write(fd, seq_end, sizeof(seq_end));
71                 write(fd, stuffing, 8192);
72                 usleep(150000);
73                 c(ioctl(fd, VIDEO_STOP, 0));
74                 c(ioctl(fd, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_DEMUX));
75         }
76         return 0;
77 }
78