// File: /workspace/github_c_makefile_star2_O3/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 %r12
pushq %rbx
subq $0x30, %rsp
movl %esi, %ebp
movq %rdi, %r15
movl $0x1, %edi
movl $0x60, %esi
callq 0x2110
movq %rax, %rbx
movl %ebp, 0x3c(%rax)
movq %r15, %rdi
callq 0x239c
movq %rax, (%rbx)
testq %rax, %rax
je 0x8a3d
movq %rax, %r14
movl $0x4e2000, %esi         # imm = 0x4E2000
movq %rax, %rdi
callq 0x2c1c
testl %eax, %eax
je 0x8a46
movq %r14, %rdi
callq 0x2f44
movl %eax, %ebp
movq %r14, %rdi
callq 0x2e9d
movsd %xmm0, 0x8(%rsp)
movq %r14, %rdi
callq 0x2f44
movl %eax, %r12d
movq (%r14), %rdi
movl $0xe0, %esi
callq 0x2fb3
movapd %xmm0, %xmm1
leaq 0x2292(%rip), %rdi      # 0xaa38
movq %r15, %rsi
movsd 0x8(%rsp), %xmm0
movl %r12d, %edx
movb $0x2, %al
callq 0x2200
leaq 0x2a1(%rip), %rax       # 0x8a61
movq %rax, 0x60(%r14)
movq %rbx, 0x68(%r14)
leaq 0x382(%rip), %rax       # 0x8b51
movq %rax, 0x70(%r14)
movq %rbx, 0x78(%r14)
movl $0x1, %eax
movl %eax, 0x14(%r14)
movl %eax, 0x38(%r14)
movq %r14, %rdi
callq 0x25d1
xorl %ecx, %ecx
testl %eax, %eax
je 0x8802
cmpq $0x0, 0x58(%r14)
je 0x8802
movl $0xc0, %ecx
addl 0x3c(%r14), %ecx
movl %ecx, 0x40(%r14)
movq (%rbx), %rdi
movl $0x0, 0x3c(%rdi)
movl 0x38(%rdi), %esi
callq 0x2d71
movq (%rbx), %rax
movq (%rax), %r14
movq %r14, %rdi
callq 0x2798
testl %eax, %eax
je 0x88c6
cmpl $0x0, 0x48(%r14)
jle 0x88c6
movl $0x30, %edi
callq 0x2160
leaq 0x10(%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 0x21d0
movl %eax, 0x20(%rbx)
testl %eax, %eax
jne 0x889e
callq 0x2120
leaq 0x21df(%rip), %rdi      # 0xaa70
movq %rax, %rsi
xorl %eax, %eax
callq 0x2200
movl 0x20(%rbx), %eax
movl %eax, %edi
xorl %esi, %esi
callq 0x21f0
movq (%rbx), %rax
movzwl 0x18(%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 0x2e07
movl %eax, %ebp
movq (%rbx), %rdi
callq 0x2e52
leaq 0x21b1(%rip), %rdi      # 0xaa90
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 0x569e(%rip), %rax      # 0xdfb8
movb $0x1, (%rax)
callq 0x2140
cmpl $0x1, 0x3c(%rbx)
leaq 0x1e7b(%rip), %rax      # 0xa7a8
leaq 0x2040(%rip), %rdx      # 0xa974
cmoveq %rax, %rdx
movl $0x8b30, %esi           # imm = 0x8B30
callq 0x8b65
movl %eax, 0x38(%rbx)
leaq 0x1d93(%rip), %rdx      # 0xa6df
movl $0x8b31, %esi           # imm = 0x8B31
callq 0x8b65
movl %eax, 0x34(%rbx)
movq 0x5630(%rip), %rax      # 0xdf90
callq *(%rax)
movl %eax, 0x30(%rbx)
movq 0x567c(%rip), %r14      # 0xdfe8
movl 0x34(%rbx), %esi
movl %eax, %edi
callq *(%r14)
movl 0x30(%rbx), %edi
movl 0x38(%rbx), %esi
callq *(%r14)
movq 0x564c(%rip), %rax      # 0xdfd0
movl 0x30(%rbx), %edi
callq *(%rax)
movq 0x5648(%rip), %rax      # 0xdfd8
movl 0x30(%rbx), %edi
callq *(%rax)
cmpl $0x1, 0x3c(%rbx)
jne 0x89df
leaq 0x20f6(%rip), %rdx      # 0xaa98
movq %rbx, %rdi
xorl %esi, %esi
callq 0x8bfb
movl %eax, 0x40(%rbx)
leaq 0x20ec(%rip), %rdx      # 0xaaa2
movq %rbx, %rdi
movl $0x1, %esi
callq 0x8bfb
movl %eax, 0x44(%rbx)
leaq 0x20e0(%rip), %rdx      # 0xaaad
movq %rbx, %rdi
movl $0x2, %esi
callq 0x8bfb
movl %eax, 0x48(%rbx)
jmp 0x8a17
leaq 0x20d2(%rip), %rdx      # 0xaab8
movq %rbx, %rdi
xorl %esi, %esi
callq 0x8bfb
movl %eax, 0x50(%rbx)
movq (%rbx), %rdi
callq 0x2e07
movl %eax, %ebp
movq (%rbx), %rdi
callq 0x2e52
imull %ebp, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x2210
movq %rax, 0x58(%rbx)
movq 0x5542(%rip), %rax      # 0xdf60
movl 0x30(%rbx), %edi
leaq 0x209c(%rip), %rsi      # 0xaac4
callq *(%rax)
movl %eax, 0x4c(%rbx)
movq %rbx, %rax
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1fb8(%rip), %rdi      # 0xa9fc
jmp 0x8a4d
leaq 0x1fc0(%rip), %rdi      # 0xaa0d
movq %r15, %rsi
xorl %eax, %eax
callq 0x2200
movl $0x1, %edi
callq 0x21c0