-
Notifications
You must be signed in to change notification settings - Fork 7
/
showiframe.c
125 lines (111 loc) · 2.7 KB
/
showiframe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* Unknown license, public domain */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <linux/dvb/video.h>
void c(int a)
{
if (a < 0)
{
perror("ioctl");
exit(6);
}
}
ssize_t write_all(int fd, const void *buf, size_t count)
{
int retval;
char *ptr = (char *)buf;
size_t handledcount = 0;
while (handledcount < count)
{
retval = write(fd, &ptr[handledcount], count - handledcount);
if (retval == 0) return -1;
if (retval < 0)
{
if (errno == EINTR) continue;
return retval;
}
handledcount += retval;
}
return handledcount;
}
int main(int argc, char **argv)
{
struct stat s;
if (argc != 2)
{
printf("usage: %s <iframe>\n", *argv);
return 3;
}
int f = open(argv[1], O_RDONLY);
if (f < 0)
{
perror(argv[1]);
return 4;
}
fstat(f, &s);
FILE *fa;
fa = fopen("/proc/stb/avs/0/input", "w");
if (fa != NULL)
{
fputs("encoder", fa);
fclose(fa);
}
int fd = open("/dev/dvb/adapter0/video0", O_WRONLY|O_NONBLOCK);
if (fd <= 0)
{
perror("/dev/dvb/adapter0/video0");
return 2;
}
else if (fork() != 0)
return 0;
else
{
size_t pos=0;
int seq_end_avail = 0;
int count = 7;
/* 0x0 0x0 0x1 0xffffffe0 0x10 0x8 0xffffff80 0xffffff80 0x5 0x21 0x0 0x1 0x0 0x1 */
/* unsigned char pes_header[] = { 0x00, 0x00, 0x01, 0xE0, 0x00, 0x00, 0x80, 0x00, 0x00 }; */
unsigned char pes_header[] = {0x0, 0x0, 0x1, 0xe0, 0x00, 0x00, 0x80, 0x80, 0x5, 0x21, 0x0, 0x1, 0x0, 0x1};
unsigned char seq_end[] = { 0x00, 0x00, 0x01, 0xB7 };
unsigned char iframe[s.st_size];
unsigned char stuffing[8192];
memset(stuffing, 0, 8192);
read(f, iframe, s.st_size);
if(iframe[0] == 0x00 && iframe[1] == 0x00 && iframe[2] == 0x00 && iframe[3] == 0x01 && (iframe[4] & 0x0f) == 0x07)
ioctl(fd, VIDEO_SET_STREAMTYPE, 1); // set to mpeg4
else
ioctl(fd, VIDEO_SET_STREAMTYPE, 0); // set to mpeg2
c(ioctl(fd, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_MEMORY));
c(ioctl(fd, VIDEO_PLAY));
c(ioctl(fd, VIDEO_CONTINUE));
c(ioctl(fd, VIDEO_CLEAR_BUFFER));
while(pos <= (s.st_size-4) && !(seq_end_avail = (!iframe[pos] && !iframe[pos+1] && iframe[pos+2] == 1 && iframe[pos+3] == 0xB7)))
++pos;
while(count--){
if ((iframe[3] >> 4) != 0xE) // no pes header
{
write_all(fd, pes_header, sizeof(pes_header));
usleep(8000);
}
else {
iframe[4] = iframe[5] = 0x00;
}
write_all(fd, iframe, s.st_size);
usleep(8000);
}
if (!seq_end_avail)
write_all(fd, seq_end, sizeof(seq_end));
write_all(fd, stuffing, 8192);
usleep(150000);
c(ioctl(fd, VIDEO_STOP, 0));
c(ioctl(fd, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_DEMUX));
}
return 0;
}