// File: /workspace/github_c_makefile_star2_O1/phoboslab[P]pl_mpeg/pl_mpeg_player_gl.c
// Function: app_create
app_t * app_create(const char *filename, int texture_mode) {
	app_t *self = (app_t *)malloc(sizeof(app_t));
	memset(self, 0, sizeof(app_t));
	
	self->texture_mode = texture_mode;
	
	// Initialize plmpeg, load the video file, install decode callbacks
	self->plm = plm_create_with_filename(filename);
	if (!self->plm) {
		SDL_Log("Couldn't open %s", filename);
		exit(1);
	}

	if (!plm_probe(self->plm, 5000 * 1024)) {
		SDL_Log("No MPEG video or audio streams found in %s", filename);
		exit(1);
	}

	int samplerate = plm_get_samplerate(self->plm);

	SDL_Log(
		"Opened %s - framerate: %f, samplerate: %d, duration: %f",
		filename, 
		plm_get_framerate(self->plm),
		plm_get_samplerate(self->plm),
		plm_get_duration(self->plm)
	);
	
	plm_set_video_decode_callback(self->plm, app_on_video, self);
	plm_set_audio_decode_callback(self->plm, app_on_audio, self);
	
	plm_set_loop(self->plm, TRUE);
	plm_set_audio_enabled(self->plm, TRUE);
	plm_set_audio_stream(self->plm, 0);

	if (plm_get_num_audio_streams(self->plm) > 0) {
		// Initialize SDL Audio
		SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
		SDL_AudioSpec audio_spec;
		SDL_memset(&audio_spec, 0, sizeof(audio_spec));
		audio_spec.freq = samplerate;
		audio_spec.format = AUDIO_F32;
		audio_spec.channels = 2;
		audio_spec.samples = 4096;

		self->audio_device = SDL_OpenAudioDevice(NULL, 0, &audio_spec, NULL, 0);
		if (self->audio_device == 0) {
			SDL_Log("Failed to open audio device: %s", SDL_GetError());
		}
		SDL_PauseAudioDevice(self->audio_device, 0);

		// Adjust the audio lead time according to the audio_spec buffer size
		plm_set_audio_lead_time(self->plm, (double)audio_spec.samples / (double)samplerate);
	}
	
	// Create SDL Window
	self->window = SDL_CreateWindow(
		"pl_mpeg",
		SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
		plm_get_width(self->plm), plm_get_height(self->plm),
		SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
	);
	self->gl = SDL_GL_CreateContext(self->window);
	
	SDL_GL_SetSwapInterval(1);

	#if defined(__APPLE__) && defined(__MACH__)
		// OSX
		// (nothing to do here)
	#else
		// Windows, Linux
		glewExperimental = GL_TRUE;
		glewInit();
	#endif
	
	
	// Setup OpenGL shaders and textures
	const char * fsh = self->texture_mode == APP_TEXTURE_MODE_YCRCB
		? APP_FRAGMENT_SHADER_YCRCB
		: APP_FRAGMENT_SHADER_RGB;
	
	self->fragment_shader = app_compile_shader(self, GL_FRAGMENT_SHADER, fsh);
	self->vertex_shader = app_compile_shader(self, GL_VERTEX_SHADER, APP_VERTEX_SHADER);
	
	self->shader_program = glCreateProgram();
	glAttachShader(self->shader_program, self->vertex_shader);
	glAttachShader(self->shader_program, self->fragment_shader);
	glLinkProgram(self->shader_program);
	glUseProgram(self->shader_program);
	
	// Create textures for YCrCb or RGB rendering
	if (self->texture_mode == APP_TEXTURE_MODE_YCRCB) {
		self->texture_y  = app_create_texture(self, 0, "texture_y");
		self->texture_cb = app_create_texture(self, 1, "texture_cb");
		self->texture_cr = app_create_texture(self, 2, "texture_cr");
	}
	else {
		self->texture_rgb = app_create_texture(self, 0, "texture_rgb");
		int num_pixels = plm_get_width(self->plm) * plm_get_height(self->plm);
		self->rgb_data = (uint8_t*)malloc(num_pixels * 3);
	}
	self->texture_crop_size = glGetUniformLocation(self->shader_program, "texture_crop_size");
	
	return self;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x28, %rsp
movl %esi, %ebp
movq %rdi, %r14
movl $0x60, %edi
callq 0x2200
movq %rax, %rbx
xorpd %xmm0, %xmm0
movupd %xmm0, 0x30(%rax)
movupd %xmm0, (%rax)
movupd %xmm0, 0x10(%rax)
movupd %xmm0, 0x20(%rax)
movupd %xmm0, 0x40(%rax)
movupd %xmm0, 0x50(%rax)
movl %ebp, 0x3c(%rax)
movq %r14, %rdi
callq 0x238c
movq %rax, (%rbx)
testq %rax, %rax
je 0x8c84
movl $0x4e2000, %esi         # imm = 0x4E2000
movq %rax, %rdi
callq 0x2d3a
testl %eax, %eax
je 0x8c8d
movq (%rbx), %rdi
callq 0x3059
movl %eax, %ebp
movq (%rbx), %rdi
callq 0x2fb2
movsd %xmm0, (%rsp)
movq (%rbx), %rdi
callq 0x3059
movl %eax, %r15d
movq (%rbx), %rax
movq (%rax), %rdi
movl $0xe0, %esi
callq 0x30c8
movapd %xmm0, %xmm1
leaq 0x2e91(%rip), %rdi      # 0xb880
movq %r14, %rsi
movsd (%rsp), %xmm0
movl %r15d, %edx
movb $0x2, %al
callq 0x21f0
movq (%rbx), %r14
leaq 0x29d(%rip), %rax       # 0x8ca8
movq %rax, 0x60(%r14)
movq %rbx, 0x68(%r14)
leaq 0x37e(%rip), %rax       # 0x8d98
movq %rax, 0x70(%r14)
movq %rbx, 0x78(%r14)
movl $0x1, %eax
movl %eax, 0x14(%r14)
movl %eax, 0x38(%r14)
movq %r14, %rdi
callq 0x266c
xorl %ecx, %ecx
testl %eax, %eax
je 0x8a4d
cmpq $0x0, 0x58(%r14)
je 0x8a4d
movl $0xc0, %ecx
addl 0x3c(%r14), %ecx
movl %ecx, 0x40(%r14)
movq (%rbx), %rdi
movl $0x0, 0x3c(%rdi)
movl 0x38(%rdi), %esi
callq 0x2e86
movq (%rbx), %rax
movq (%rax), %r14
movq %r14, %rdi
callq 0x2884
testl %eax, %eax
je 0x8b0f
cmpl $0x0, 0x48(%r14)
jle 0x8b0f
movl $0x30, %edi
callq 0x2150
leaq 0x8(%rsp), %r14
movl $0x20, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x20e0
movl %ebp, (%r14)
movw $0x8120, 0x4(%r14)      # imm = 0x8120
movb $0x2, 0x6(%r14)
movw $0x1000, 0x8(%r14)      # imm = 0x1000
xorl %edi, %edi
xorl %esi, %esi
movq %r14, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x21c0
movl %eax, 0x20(%rbx)
testl %eax, %eax
jne 0x8ae6
callq 0x2110
leaq 0x2ddc(%rip), %rdi      # 0xb8b8
movq %rax, %rsi
xorl %eax, %eax
callq 0x21f0
movl 0x20(%rbx), %edi
xorl %esi, %esi
callq 0x21e0
movq (%rbx), %rax
movzwl 0x10(%rsp), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rax)
movq (%rbx), %rdi
callq 0x2f1c
movl %eax, %ebp
movq (%rbx), %rdi
callq 0x2f67
leaq 0x2db0(%rip), %rdi      # 0xb8d8
movl $0x2fff0000, %esi       # imm = 0x2FFF0000
movl $0x2fff0000, %edx       # imm = 0x2FFF0000
movl %ebp, %ecx
movl %eax, %r8d
movl $0x26, %r9d
callq 0x2040
movq %rax, 0x18(%rbx)
movq %rax, %rdi
callq 0x2090
movq %rax, 0x28(%rbx)
movl $0x1, %edi
callq 0x20a0
movq 0x6455(%rip), %rax      # 0xefb8
movb $0x1, (%rax)
callq 0x2130
cmpl $0x1, 0x3c(%rbx)
leaq 0x2a7a(%rip), %rax      # 0xb5f0
leaq 0x2c3f(%rip), %rdx      # 0xb7bc
cmoveq %rax, %rdx
movl $0x8b30, %esi           # imm = 0x8B30
callq 0x8dac
movl %eax, 0x38(%rbx)
leaq 0x2992(%rip), %rdx      # 0xb527
movl $0x8b31, %esi           # imm = 0x8B31
callq 0x8dac
movl %eax, 0x34(%rbx)
movq 0x63e7(%rip), %rax      # 0xef90
callq *(%rax)
movl %eax, 0x30(%rbx)
movq 0x6433(%rip), %r14      # 0xefe8
movl 0x34(%rbx), %esi
movl %eax, %edi
callq *(%r14)
movl 0x30(%rbx), %edi
movl 0x38(%rbx), %esi
callq *(%r14)
movq 0x6403(%rip), %rax      # 0xefd0
movl 0x30(%rbx), %edi
callq *(%rax)
movq 0x63ff(%rip), %rax      # 0xefd8
movl 0x30(%rbx), %edi
callq *(%rax)
cmpl $0x1, 0x3c(%rbx)
jne 0x8c28
leaq 0x2cf5(%rip), %rdx      # 0xb8e0
movq %rbx, %rdi
xorl %esi, %esi
callq 0x8e42
movl %eax, 0x40(%rbx)
leaq 0x2ceb(%rip), %rdx      # 0xb8ea
movq %rbx, %rdi
movl $0x1, %esi
callq 0x8e42
movl %eax, 0x44(%rbx)
leaq 0x2cdf(%rip), %rdx      # 0xb8f5
movq %rbx, %rdi
movl $0x2, %esi
callq 0x8e42
movl %eax, 0x48(%rbx)
jmp 0x8c60
leaq 0x2cd1(%rip), %rdx      # 0xb900
movq %rbx, %rdi
xorl %esi, %esi
callq 0x8e42
movl %eax, 0x50(%rbx)
movq (%rbx), %rdi
callq 0x2f1c
movl %eax, %ebp
movq (%rbx), %rdi
callq 0x2f67
imull %ebp, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x2200
movq %rax, 0x58(%rbx)
movq 0x62f9(%rip), %rax      # 0xef60
movl 0x30(%rbx), %edi
leaq 0x2c9b(%rip), %rsi      # 0xb90c
callq *(%rax)
movl %eax, 0x4c(%rbx)
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2bb9(%rip), %rdi      # 0xb844
jmp 0x8c94
leaq 0x2bc1(%rip), %rdi      # 0xb855
movq %r14, %rsi
xorl %eax, %eax
callq 0x21f0
movl $0x1, %edi
callq 0x21b0