-
Notifications
You must be signed in to change notification settings - Fork 0
/
dv.c
161 lines (139 loc) · 3.53 KB
/
dv.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <assert.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include "global.h"
#include "dv.h"
#include "util.h"
/*
Start putdv thread
*/
void init_dvsystem()
{
pthread_t printthread;
int rc = pthread_create(&printthread, NULL, putdv, NULL);
assert(rc == 0);
}
/*
Thread that outputs all DV data
*/
void *putdv(void *asdf)
{
// Init - Wait until main program is ready
fpos_t position;
long long filesize = 0;
// In order to start streaming, filename must be given and playerstate must contain NEWSOURCE
// If program is supposed to be terminated, also continue
while((!DVControl.filename
|| (playerstate != PLAYER_STATE_NEWSOURCE
&& playerstate != PLAYER_STATE_PAUSE_NEWSOURCE))
&& playerstate != PLAYER_STATE_TERMINATE)
usleep(10);
FILE *stream = NULL;
for(;;)
{
if (playerstate == PLAYER_STATE_CONTINUE)
{
DVControl.currentframe++;
putframe(stream);
if (stream) fgetpos(stream, &position);
}
else if (playerstate == PLAYER_STATE_PAUSE)
{
putframe_noaudio(stream);
if (stream) fsetpos(stream, &position);
}
else if (playerstate == PLAYER_STATE_NEWSOURCE)
{
if (stream) fclose(stream);
stream = open_file(DVControl.filename, &filesize);
DVControl.totalframes = get_total_frames(filesize);
playerstate = PLAYER_STATE_CONTINUE;
DVControl.currentframe = 0;
DVControl.pos_percent = 0;
}
else if (playerstate == PLAYER_STATE_PAUSE_NEWSOURCE)
{
if (stream) fclose(stream);
stream = open_file(DVControl.filename, &filesize);
DVControl.totalframes = get_total_frames(filesize);
if (stream) fgetpos(stream, &position);
playerstate = PLAYER_STATE_PAUSE;
DVControl.currentframe = 0;
DVControl.pos_percent = 0;
}
else if (playerstate == PLAYER_STATE_NEWPOS)
{
if (DVControl.newpos < filesize && DVControl.newpos >= 0)
{
if (stream) fseeko(stream, DVControl.newpos, SEEK_SET);
DVControl.currentframe = DVControl.newpos / BYTES_PER_FRAME;
if (stream) fgetpos(stream, &position);
}
playerstate = PLAYER_STATE_PAUSE;
}
else if (playerstate == PLAYER_STATE_NEWPOS_PERCENT)
{
long newframe = DVControl.pos_percent * DVControl.totalframes / 100;
DVControl.newpos = newframe * BYTES_PER_FRAME;
playerstate = PLAYER_STATE_NEWPOS;
}
else if (playerstate == PLAYER_STATE_TERMINATE)
{
break;
}
if (DVControl.currentframe >= DVControl.totalframes
&& playerstate == PLAYER_STATE_CONTINUE)
playerstate = PLAYER_STATE_PAUSE;
}
if (stream) fclose(stream);
playerstate = PLAYER_STATE_TERMINATED;
pthread_exit(NULL);
}
/*
Retrieve total amount of frames in a file from the filesize
*/
long get_total_frames(long long filesize)
{
return filesize / (80 * 150 * SEQUENCES) - 1;
}
/*
Put frame to stdout and remove audio, used for pausing
*/
// DV Format Information: http://dvswitch.alioth.debian.org/wiki/DV_format/
void putframe_noaudio(FILE *stream)
{
if (!stream) return;
int block, sequence, pack; // make up 1 frame
for (pack = 0; pack<SEQUENCES; ++pack)
{
for (sequence = 0; sequence < 150; ++sequence)
{
if (((sequence-6) % 16) != 0) // no Audio source (AS)
{
for (block = 0; block < 80; ++block)
printf("%c", getc(stream));
}
else // Audio block
{
for (block = 0; block < 8; ++block)
printf("%c", getc(stream));
for (block = 0; block < 72; ++block)
{
getc(stream);
printf("%c", 0x00);
}
}
}
}
}
/*
Put frame to stdout, with audio
*/
void putframe(FILE *stream)
{
if (!stream) return;
int i;
for (i=0; i<BYTES_PER_FRAME; ++i)
printf("%c", getc(stream));
}