-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
427 lines (329 loc) · 11.4 KB
/
main.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#ifdef _WIN32
#include <conio.h>
#else
#include <stdio.h>
#define clrscr() printf("\e[1;1H\e[2J")
#endif
#include "main.h"
struct videoStream vStream;
struct timespec wait;
struct playback controls;
FILE* subFile;
regex_t matchTime;
int debug;
int skew;
// Open up the file data stream and parse frames
void openStream(struct vStreamArgs* args){
char *filename = args->file;
struct vBuffer *videoBuffer = args->buffer;
AVFormatContext *pFormatCtx = NULL;
AVCodecContext *pCodecCtx = NULL;
AVCodec *pCodec = NULL;
AVFrame *pFrame = NULL;
AVFrame *pFrameRGB = NULL;
double enableFramePacing = 0;
int frameAdjustment = 0;
// Open video file
if(avformat_open_input(&pFormatCtx, filename, NULL, NULL) != 0) {
vStream.bufferEnd = true; //use this to indicate to other threads that we failed
pthread_exit((void *)1); // Couldn't open file
}
//Get stream info
if(avformat_find_stream_info(pFormatCtx, NULL) < 0) {
vStream.bufferEnd = true; //use this to indicate to other threads that we failed
pthread_exit((void *)2); // Couldn't find stream information
}
// Find first video stream
int i = 0;
int stream = -1;
// Walk over all streams until we find the first video stream
for(int i=0; i<pFormatCtx->nb_streams; i++){
if(pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO){
stream = i;
break;
}
}
if(vStream.fps == 0.0){
vStream.fps = (double) pFormatCtx->streams[stream]->avg_frame_rate.num / (double) pFormatCtx->streams[stream]->avg_frame_rate.den;
vStream.srcFps = vStream.fps;
}
else{
enableFramePacing = (double) pFormatCtx->streams[stream]->avg_frame_rate.num / (double) pFormatCtx->streams[stream]->avg_frame_rate.den;
frameAdjustment = getNiceFramerate(enableFramePacing);
vStream.srcFps = enableFramePacing;
}
vStream.height = pFormatCtx->streams[stream]->codecpar->height;
vStream.width = pFormatCtx->streams[stream]->codecpar->width*2;
if (stream == -1) pthread_exit((void *)3); // No video streams found
//Try to find the video codec
pCodec = avcodec_find_decoder(pFormatCtx->streams[stream]->codecpar->codec_id);
if (pCodec == NULL) pthread_exit((void *)4); // Unsupported codec
// Copy the found codec
pCodecCtx = avcodec_alloc_context3(pCodec);
// Copy codec parameters and open the context
avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[stream]->codecpar);
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) pthread_exit((void *)5); //Could not open codec
// Alocate a frame
pFrame = av_frame_alloc();
pFrameRGB = av_frame_alloc();
if (pFrame == NULL) pthread_exit((void *)6); //Could not allocate AVFrame
if (pFrameRGB == NULL) pthread_exit((void *)7);
// Prepare a buffer for frame conversion
int numBytes;
uint8_t *buffer = NULL;
numBytes = av_image_get_buffer_size(AV_PIX_FMT_GRAY8, pCodecCtx->width, pCodecCtx->height, ALIGNMENT);
buffer = av_malloc(numBytes*sizeof(uint8_t));
avpicture_fill((AVPicture *)pFrameRGB, buffer, AV_PIX_FMT_GRAY8, pCodecCtx->width, pCodecCtx->height);
struct SwsContext *sws_ctx = NULL;
int frameFinished;
AVPacket packet;
sws_ctx = sws_getContext(pCodecCtx->width,
pCodecCtx->height,
pCodecCtx->pix_fmt,
pCodecCtx->width,
pCodecCtx->height,
AV_PIX_FMT_GRAY8,
SWS_BILINEAR,
NULL,
NULL,
NULL
);
for(int frame=0; av_read_frame(pFormatCtx, &packet) >= 0; frame++){
// Check that the packet is from our stream
if (packet.stream_index == stream){
// Decode frame
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
// Check that this is a video frame
if (frameFinished){
// Convert frame to RGB image
sws_scale(sws_ctx, (uint8_t const * const *)pFrame->data,
pFrame->linesize, 0, pCodecCtx->height,
pFrameRGB->data, pFrameRGB->linesize);
vStream.frameCount++;
vStream.realTime = (vStream.frameCount-(vStream.bufferLength*(vStream.srcFps/vStream.fps)))/vStream.srcFps;
if (enableFramePacing && ((frame)%(int) ((ceil(enableFramePacing))/(vStream.fps)))) continue;
// Wait until there is room in the frame buffer
while(vStream.bufferLength >= MAX_BUFFER) nanosleep(&wait, (struct timespec*) NULL);
// Write the new frame to the buffer
writeBuffer(&videoBuffer, pFrameRGB);
}
}
// Free the packet we allocated
av_free_packet(&packet);
if (controls.cease_execution) break;
}
vStream.bufferEnd = true;
// Free the RGB image
av_free(buffer);
//writeBuffer(videoBuffer, pFrameRGB, 1);
av_free(pFrameRGB);
// Free the YUV frame
av_free(pFrame);
// Close the codecs
avcodec_close(pCodecCtx);
// Close the video file
avformat_close_input(&pFormatCtx);
pthread_exit((void *)0);
}
// Write new frame data to the tail of the buffer queue
void writeBuffer(struct vBuffer **buffer, AVFrame *frame){
// Wait for the mutex
while (pthread_mutex_trylock(&vStream.mutex) != 0) nanosleep(&wait, (struct timespec*) NULL);;
// Find the end of the buffer
while ((*buffer)->next != NULL) { (*buffer) = (*buffer)->next; }
// Allocate the buffer
(*buffer)->next = malloc(sizeof(struct videoBuffer*)+sizeof(uint8_t*) * vStream.width*vStream.height);
// Handle the NULL head frame buffer
if (frame != NULL){
// Convert frame data to a grayscale represenation and store it
(*buffer)->next->frame = grayscale(frame, vStream.width, vStream.height);
}
// Update frame buffer
(*buffer)->next->next = NULL;
vStream.bufferLength++;
pthread_mutex_unlock(&vStream.mutex);
}
// Return frame buffer at the head of our queue
struct vBuffer* readBuffer(struct vBuffer **buffer){
struct vBuffer *nextBuffer;
// don't bother trying to do anything if there's no need to do work
while ((vStream.bufferLength <= 2 && !vStream.bufferEnd)) nanosleep(&wait, (struct timespec*) NULL);;
// Wait for the mutex so we don't cause any issues
while (pthread_mutex_trylock(&vStream.mutex) != 0) nanosleep(&wait, (struct timespec*) NULL);
if ((*buffer)->next != NULL) {
// Update buffer and queue
nextBuffer = (*buffer)->next;
free((*buffer)->frame);
free(*buffer);
vStream.bufferLength--;
}
pthread_mutex_unlock(&vStream.mutex);
return &(*nextBuffer);
}
// Load array of subtitle time codes
void loadSubs(){
char buffer[MAX_CHAR];
// Open our file for reading
subFile = fopen(vStream.subFile, "r");
// Return if no file provided
if (!subFile) return;
int lines = 0;
// Determine how much space we need for storing data
while(fgets(buffer, sizeof(buffer), subFile) != NULL){
if(!regexec(&matchTime, buffer, 0, NULL, 0)) lines++;
}
// Reset file pointer
rewind(subFile);
// Allocate space for our sub data
vStream.subTimes = malloc(sizeof(double) * lines * 3);
int count = 0;
lines = 0;
// Write our subtitle data
// Format [start time, end time, first line of text, ...]
while(fgets(buffer, sizeof(buffer), subFile) != NULL){
if(!regexec(&matchTime, buffer, 0, NULL, 0)){
*(vStream.subTimes+(count++)) = decodeTime(buffer, 0);
*(vStream.subTimes+(count++)) = decodeTime(buffer, 1);
*(vStream.subTimes+(count++)) = lines+1;
}
lines++;
}
// Reset file pointer
rewind(subFile);
}
// Run continuously in the background rendering frames when needed
void *renderingEngine(struct vBuffer *buffer){
if (vStream.subFile != NULL) loadSubs();
// Timing variables
struct timeval start, curr, diff;
gettimeofday(&start, 0x0);
int frameDigits = 0;
// Run until we hit the streamEnd frame
while(1){
// Do not render when paused (sleep thread to not spin it)
while(controls.pause) sleep(0.1);
// Thread may be ready before the frames are, so wait for frames
while(vStream.bufferLength <= 0) {
if (vStream.bufferEnd) pthread_exit((void*)1);
nanosleep(&wait, (struct timespec*) NULL);
}
if (frameDigits == 0) frameDigits = getNiceFramerate(vStream.srcFps);
// Update time tracking
gettimeofday(&curr, 0x0);
timeval_subtract(&diff, &curr, &start);
// If enough time has passed between the last frame and this one render it
// Alternatively, if we are fast forwarding loading frames as they come into the buffer
if (diff.tv_usec >= (1/vStream.fps*1000000) || controls.fast_forward){
// Update our timing variable
gettimeofday(&start, 0x0);
// Pull the new frame data from the buffer
while(buffer->next == NULL);
buffer = readBuffer(&buffer);
vStream.time = vStream.time + (1/(vStream.fps));
// Account for possible frame skew when rendering at different framerates
if (!skew && (vStream.time > 1.02*vStream.realTime || vStream.time < 0.98*vStream.realTime)) vStream.time = vStream.realTime;
// Pass frame data to renderer
renderFrame(buffer->frame, vStream.width, vStream.height);
if ((buffer->next == NULL) && vStream.bufferEnd) break;
}
nanosleep(&wait, (struct timespec*) NULL);
if (controls.cease_execution) break;
}
// Close the subtitle file
if (vStream.subFile != NULL) fclose(subFile);
// Free memory
free(buffer);
pthread_exit((void *)0);
}
// Spawn all neccessary threads and manage them
int main(int argc, char *argv[]){
char* filename = NULL;
char* subfile = NULL;
char* flag;
int slowMode = 0;
int disableRenderer = 0;
// Initialize our control structure
controls = (struct playback) {
.pause = false,
.fast_forward = false,
.cease_execution = false
};
wait.tv_nsec = 1000000;
// Initialize our video buffer
struct vBuffer *videoBuffer = malloc(sizeof(struct videoBuffer*));
videoBuffer->frame = NULL;
videoBuffer->next = NULL;
// Initialize the parameters of our video stream
vStream.bufferLength = 0;
vStream.time = 0;
vStream.subTimes = NULL;
vStream.fpPos = 0;
vStream.subPos = 0;
vStream.fps = 0.0;
vStream.scale = 1;
vStream.bufferEnd = false;
pthread_mutex_init(&vStream.mutex, NULL);
// Setup thread tracking
pthread_t threads[NUM_THREADS];
for (int i=1; i<argc; i++){
flag = *(argv+i);
if (!strcmp(flag, "-f")){
i++;
filename = malloc(sizeof(char) * strlen(*(argv+i)));
filename = *(argv+i);
}
if (!strcmp(flag, "-s")){
i++;
subfile = malloc(sizeof(char) * strlen(*(argv+i)));
subfile = *(argv+i);
vStream.subFile = subfile;\
}
if (!strcmp(flag, "--slow-mode")) slowMode = 1;
if (!strcmp(flag, "--no-render")) disableRenderer = 1;
if (!strcmp(flag, "--debug")) debug = 1;
if (!strcmp(flag, "--enable-skew")) skew = 1;
if (!strcmp(flag, "--no-limiter")) controls.fast_forward = true;
if (!strcmp(flag, "--frame-rate")){
i++;
if (strlen(*(argv+i)) > 1) vStream.fps = (((int) *(*(argv+i))-'0')*10) + ((int) (*(*(argv+i)+1)-'0'));
else vStream.fps = ((int) (*(*(argv+i))-'0'));
}
if (!strcmp(flag, "-h")){
printf("Usage ./telnetflix -f <FILENAME> [ -s <SUBFILE> ]\n");
return 0;
}
}
if (filename == NULL){
printf("Error, no file specified\n");
return -1;
}
// Initialize display
if (!disableRenderer){
initscr();
noecho();
curs_set(FALSE);
}
// Setup regex
regcomp(&matchTime, MATCH_EXPR , 0);
// Register codecs
av_register_all();
// Prepare data for the stream
struct vStreamArgs *vArgs = malloc(sizeof(struct vStreamArgs*));
vArgs->file = filename;
vArgs->buffer = videoBuffer;
// Create and track our threads
pthread_create(&threads[0], NULL, (void * (*)(void *))openStream, vArgs);
if (slowMode) sleep(4);
pthread_create(&threads[1], NULL, (void * (*)(void *))renderingEngine, videoBuffer);
pthread_create(&threads[2], NULL, inputHandler, NULL);
pthread_join(threads[0], NULL);
pthread_join(threads[1], NULL);
pthread_cancel(threads[2]);
// Free allocated memory
free(vArgs);
if(vStream.subTimes != NULL) free(vStream.subTimes);
pthread_mutex_destroy(&vStream.mutex);
// Terminate our screen
endwin();
return 0;
}