-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
482 lines (418 loc) · 15.9 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
#include <caca.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavcodec/avcodec.h>
#include <dirent.h>
#include <stdlib.h>
#include <time.h>
#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 900
caca_canvas_t *cv = NULL;
SDL_Event event;
SDL_Surface *caca_view = NULL;
SDL_Window *window = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *text = NULL;
SDL_Texture *texture = NULL;
SDL_Renderer *renderer = NULL;
SDL_Texture *screen_texture = NULL;
caca_dither_t *dither = NULL;
int font_size = -1, font_width, font_height;
uint16_t bg, fg;
SDL_Color text_color;
SDL_Color background_color;
SDL_Rect pos;
SDL_AudioSpec wanted_spec;
SDL_AudioSpec spec;
char ch[2];
uint32_t *chars, *attrs;
float zoom = 1;
AVFormatContext *pFormatCtx = NULL;
AVCodecContext *pCodecCtxOrig = NULL;
AVCodecContext *pCodecCtx = NULL;
AVCodecContext *pAudioCodecCtxOrig = NULL;
AVCodecContext *pAudioCodecCtx = NULL;
AVCodecParameters *codecparams = NULL;
AVCodec *pCodec = NULL;
AVCodec *pAudioCodec = NULL;
AVFrame *pFrame = NULL;
AVFrame *pFrameRGBA = NULL;
uint8_t *imageBuffer = NULL;
struct SwsContext *sws_ctx = NULL;
AVPacket packet;
int videoStream = -1, audioStream = -1, frameFinished, numBytes, stream_index, start_time;
int full_screen = -1, sound = -1, rnd = -1, num_pics = 0;
DIR *dir;
struct dirent *ent;
char *filename = NULL;
char **pictures = NULL;
char *input_full_path = NULL;
char *img = NULL;
char *base_path = NULL;
char *folder_path = NULL;
char *font_name = NULL;
char *font_path = NULL;
//PacketQueue pqueue;
void exit_msg(char *msg) {
printf(msg);
exit(-1);
}
void cleanup() {
SDL_free(base_path);
SDL_free(screen_texture);
SDL_free(screen);
free(font_name);
free(font_path);
free(folder_path);
av_free(imageBuffer);
av_free(pFrame);
av_free(pFrameRGBA);
av_free_packet(&packet);
avcodec_close(pCodecCtx);
avcodec_close(pCodecCtxOrig);
avformat_close_input(&pFormatCtx);
caca_free_canvas(cv);
caca_free_dither(dither);
SDL_Quit();
}
void LogOutput(void *userdata, int category, SDL_LogPriority priority, const char *message) {
FILE *fp = NULL;
if ((fp = fopen("ErrorLog.txt", "w")) == NULL) {
exit_msg("couldn't open ErrorLog");
}
fprintf(fp, "%s\n", message);
fclose(fp);
}
void read_config() {
char ch, *line = NULL, *split = NULL, *config_name = NULL, *config = NULL;
int i = 0, line_length = 255, line_end_flag = 0, num_lines = 0;
FILE *fp = NULL;
// setup config strings
config_name = "config.txt";
config = malloc(strlen(base_path) + strlen(config_name) + 1);
// get config location
config[0] = '\0';
strcat(config, base_path);
strcat(config, config_name);
// check if the file exists
if(access(config, F_OK) != -1) {
// file does exist to read in configuration settings
fp = fopen(config, "r");
// make sure we opened the file
if(fp == NULL)
exit_msg("Failed to open config.txt");
// allocate memory
line = malloc(sizeof(char) * line_length);
// read the whole file
while(fgets(line, line_length + 1, fp)) {
// check if the line is a new line character
if(*line != '\n') {
// split on : character
split = strtok(line, "=");
// check the config setting name to set the proper variable
if(strcmp(split, "font_size") == 0) {
split = strtok(NULL, "=");
split = strtok(split, "\n");
font_size = atoi(split);
}
if(strcmp(split, "folder_path") == 0) {
split = strtok(NULL, "=");
split = strtok(split, "\n");
folder_path = malloc(sizeof(char) * 256);
strcpy(folder_path, split);
}
if(strcmp(split, "font_name") == 0) {
split = strtok(NULL, "=");
split = strtok(split, "\n");
font_name = malloc(sizeof(char) * 256);
strcpy(font_name, split);
}
if(strcmp(split, "full_screen") == 0) {
split = strtok(NULL, "\n");
full_screen = atoi(split);
}
if(strcmp(split, "sound") == 0) {
split = strtok(NULL, "\n");
sound = atoi(split);
}
i++;
}
}
// check to make sure that everything got set to something
if(font_size == -1 || font_size == 0)
font_size = 14;
if(folder_path == NULL)
folder_path = "pics/";
if(font_name == NULL)
font_name = "FreeMonoBold.ttf";
if(full_screen == -1)
full_screen = 1;
if(sound == -1)
sound = 0;
} else {
// file doesn't exist so write default configuration
// and use default values
fp = fopen(config, "w");
// make sure we opened the file
if(fp == NULL)
exit_msg("Failed to open config.txt");
// write default settings to file
fprintf(fp, "font_size=14\n");
fprintf(fp, "folder_path=pics/\n");
fprintf(fp, "font_name=FreeMonoBold.ttf\n");
fprintf(fp, "full_screen=1\n");
fprintf(fp, "sound=0\n");
// set default settings for variables
font_size = 14;
folder_path = "pics/";
font_name = "FreeMonoBold.ttf";
full_screen = 1;
sound = 0;
}
// cleanup
fclose(fp);
free(line);
free(config);
}
void audio_callback() {}
int main(int argc, char* args[]) {
int i, j;
SDL_LogSetOutputFunction(&LogOutput, NULL);
// get base working directory
base_path = SDL_GetBasePath();
#ifdef _WIN32
// windows specific call to get the full file path instead of the short file path
GetLongPathName(base_path, base_path, 1024);
#endif
// read or create config settings
read_config();
// setup full font path
font_path = malloc(strlen(base_path) + strlen(font_name) + 1);
// get config location
font_path[0] = '\0';
strcat(font_path, base_path);
strcat(font_path, font_name);
// get full base path with folder path
base_path = strcat(base_path, folder_path);
pictures = (char**)malloc(sizeof(char*) * 256);
if ((dir = opendir(base_path)) != NULL) {
while ((ent = readdir(dir)) != NULL) {
filename = (char*)ent->d_name;
if (strcmp(filename,".") != 0 && strcmp(filename,"..") != 0) {
pictures[num_pics] = (char*)malloc(sizeof(char) * 256);
strcpy(pictures[num_pics], filename);
num_pics++;
}
}
} else {
exit_msg("Failed to open folder");
}
srand(time(NULL));
rnd = rand() % num_pics;
img = pictures[rnd];
if ((input_full_path = malloc(strlen(base_path) + strlen(img) + 1)) != NULL) {
input_full_path[0] = '\0';
strcat(input_full_path, base_path);
strcat(input_full_path, img);
} else {
exit_msg("Failed to create input file full path");
}
// register all file formats
av_register_all();
// open up file
if(avformat_open_input(&pFormatCtx, input_full_path, NULL, NULL) != 0)
exit_msg("Failed to open file");
// get stream info
if(avformat_find_stream_info(pFormatCtx, NULL) < 0)
exit_msg("Failed to get stream info");
// iterate through stream to find video
for(i = 0; i < pFormatCtx->nb_streams; i++) {
if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = i;
//break;
}
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
audioStream = i;
}
}
// check if we found the video stream
if(videoStream == -1)
exit_msg("Couldn't initialize stream");
/* if (audioStream == -1)
exit_msg("Couldn't find audio stream"); */
// get codec context from the video stream
pCodecCtxOrig = pFormatCtx->streams[videoStream]->codec;
//AVCodecContext *pAudioCodecCtxOrig = pFormatCtx->streams[audioStream]->codec; // get pointer to codec context for audio stream - contains info about codec the stream uses
// find decoder
pCodec = avcodec_find_decoder(pCodecCtxOrig->codec_id);
if(pCodec == NULL)
exit_msg("Couldn't find codec");
//pAudioCodec = avcodec_find_decoder(pAudioCodecCtxOrig->codec_id);
// copy context
pCodecCtx = avcodec_alloc_context3(pCodec);
if(avcodec_copy_context(pCodecCtx, pCodecCtxOrig) != 0)
exit_msg("Couldn't copy Codec Context");
/*pAudioCodecCtx = avcodec_alloc_context3(pAudioCodec);
if (avcodec_copy_context(pAudioCodecCtx, pAudioCodecCtxOrig) != 0)
exit_msg("Couldn't copy audio codec context");
// do SDL audio stoof
wanted_spec.freq = pAudioCodecCtx->sample_rate;
wanted_spec.format = AUDIO_S16SYS;
wanted_spec.channels = pAudioCodecCtx->channels;
wanted_spec.silence = 0;
wanted_spec.samples = 1024;
wanted_spec.callback = audio_callback;
wanted_spec.userdata = pAudioCodecCtx;
//open audio
if (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
exit_msg("SDL_OpenAudio failed");
}
*/
// open video codec
if(avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
exit_msg("Couldn't open Codec");
// allocate video frame
pFrame = av_frame_alloc();
if(pFrame == NULL)
exit_msg("Couldn't allocate pFrame");
// allocate RGBA video frame
pFrameRGBA = av_frame_alloc();
if(pFrameRGBA == NULL)
exit_msg("Couldn't allocate pFrameRGBA");
// determine required buffer size and allocate buffer
numBytes = avpicture_get_size(AV_PIX_FMT_RGBA, pCodecCtx->width, pCodecCtx->height);
imageBuffer = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t));
avpicture_fill((AVPicture *)pFrameRGBA, imageBuffer, AV_PIX_FMT_RGBA, pCodecCtx->width, pCodecCtx->height);
// setup context
sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,
AV_PIX_FMT_RGBA,
SWS_BILINEAR,
NULL,
NULL,
NULL);
// setup SDL
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
exit_msg("Couldn't init SDL");
// setup SDL window
window = SDL_CreateWindow("SDL Ascii Viewer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
// set window as full screen
if(full_screen)
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
renderer = SDL_CreateRenderer(window, -1, 0);
if(window == NULL)
exit_msg("Couldn't init SDL Window");
if(TTF_Init() == -1)
exit_msg("Couldn't init SDL TTF");
// open font
TTF_Font *font = TTF_OpenFont(font_path, font_size);
if(font == NULL)
exit_msg("Failed to open font");
// get text size
TTF_SizeText(font, "a", &font_width, &font_height);
// get screen surface
//screen = SDL_GetWindowSurface(window);
// create screen surface
screen = SDL_CreateRGBSurface(0, SCREEN_WIDTH, SCREEN_HEIGHT, 32, 0, 0, 0, 0);
// create screen texture
screen_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT);
// setup libcaca
cv = caca_create_canvas(screen->w / font_width, screen->h / font_height);
caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK);
caca_put_str(cv, 0, 0, "caca failed");
// create dither
dither = caca_create_dither(32, pCodecCtx->width, pCodecCtx->height, pCodecCtx->width * 4, 0x0000ff, 0x00ff00, 0xff0000, 0);
int id = -1;
char *msg = NULL;
while(1) {
while(av_read_frame(pFormatCtx, &packet) >= 0) {
msg = SDL_GetError();
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) {
cleanup();
return 0;
} else if(event.type == SDL_MOUSEWHEEL) {
if(event.wheel.y > 0)
zoom += 0.025;
if(event.wheel.y < 0)
zoom -= 0.025;
} else if (event.type == SDL_KEYDOWN) {
switch(event.key.keysym.sym) {
case 'q':
cleanup();
return 0;
break;
}
}
}
if(packet.stream_index == videoStream) {
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
if(frameFinished) {
// convert current frame into a ARGB frame
sws_scale(sws_ctx, (uint8_t const * const *)pFrame->data,
pFrame->linesize, 0, pCodecCtx->height,
pFrameRGBA->data, pFrameRGBA->linesize);
//Fill background with black
SDL_FillRect(screen, NULL, 0x000000);
// clear canvas
caca_clear_canvas(cv);
// dither the frame
caca_dither_bitmap(cv, 0, 0, (screen->w / font_width) * zoom, (screen->h / font_height) * zoom, dither, pFrameRGBA->data[0]);
// get libcaca internal state
chars = caca_get_canvas_chars(cv);
attrs = caca_get_canvas_attrs(cv);
pos.x = 0;
pos.y = 0;
#pragma omp parallel for collapse(2)
for(i = 0; i < screen->h / font_height; i++) {
for(j = 0; j < screen->w / font_width; j++) {
msg = SDL_GetError();
//id = omp_get_thread_num();
ch[0] = *chars++;
ch[1] = '\0';
bg = caca_attr_to_rgb12_bg(*attrs);
fg = caca_attr_to_rgb12_fg(*attrs);
text_color.r = ((fg & 0xf00) >> 8) * 8;
text_color.g = ((fg & 0x0f0) >> 4) * 8;
text_color.b = (fg & 0x00f) * 8;
text_color.a = 0xff;
background_color.r = ((bg & 0xf00) >> 8) * 8;
background_color.g = ((bg & 0x0f0) >> 4) * 8;
background_color.b = (bg & 0x00f) * 8;
background_color.a = 0xff;
text = TTF_RenderText_Shaded(font, &ch, text_color, background_color);
SDL_BlitSurface(text, NULL, screen, &pos);
SDL_FreeSurface(text);
pos.x += font_width;
attrs++;
}
pos.x = 0;
pos.y += font_height;
}
//Update the window
//SDL_UpdateWindowSurface(window);
// update screen texture
SDL_UpdateTexture(screen_texture, NULL, screen->pixels, screen->pitch);
// update the window
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, screen_texture, NULL, NULL);
SDL_RenderPresent(renderer);
msg = SDL_GetError();
}
}
// free packet
av_free_packet(&packet);
}
// reset to beginning of file
stream_index = av_find_default_stream_index(pFormatCtx);
start_time = pFormatCtx->streams[stream_index]->time_base.num;
av_seek_frame(pFormatCtx, stream_index, start_time, AVSEEK_FLAG_BACKWARD);
}
// cleanup
cleanup();
return 0;
}