// File: /workspace/github_c_makefile_star2_O2/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, %r14
pushq $0x1
popq %rdi
pushq $0x60
popq %rsi
callq 0x2110
movq %rax, %rbx
movl %ebp, 0x3c(%rax)
movq %r14, %rdi
callq 0x239c
movq %rax, (%rbx)
testq %rax, %rax
je 0x7d72
movq %rax, %r15
movl $0x4e2000, %esi         # imm = 0x4E2000
movq %rax, %rdi
callq 0x29ec
testl %eax, %eax
je 0x7d7b
movq %r15, %rdi
callq 0x2c52
movl %eax, %ebp
movq %r15, %rdi
callq 0x2bd7
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
callq 0x2c52
movl %eax, %r12d
movq %r15, %rdi
callq 0x2ca6
movapd %xmm0, %xmm1
leaq 0x2ed8(%rip), %rdi      # 0xa9f8
movq %r14, %rsi
movsd 0x8(%rsp), %xmm0
movl %r12d, %edx
movb $0x2, %al
callq 0x2200
leaq 0x25a(%rip), %rax       # 0x7d94
movq %rax, 0x60(%r15)
movq %rbx, 0x68(%r15)
leaq 0x339(%rip), %rax       # 0x7e82
movq %rax, 0x70(%r15)
movq %rbx, 0x78(%r15)
movl $0x1, 0x14(%r15)
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x2ae6
movq (%rbx), %rdi
xorl %esi, %esi
callq 0x2b1a
movq (%rbx), %rdi
callq 0x2c4a
testl %eax, %eax
jle 0x7c03
pushq $0x30
popq %rdi
callq 0x2160
leaq 0x10(%rsp), %r14
pushq $0x20
popq %rdx
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 0x7bdf
callq 0x2120
leaq 0x2e5e(%rip), %rdi      # 0xaa30
movq %rax, %rsi
xorl %eax, %eax
callq 0x2200
movl 0x20(%rbx), %eax
movl %eax, %edi
xorl %esi, %esi
callq 0x21f0
movq (%rbx), %rdi
movzwl 0x18(%rsp), %eax
cvtsi2sd %eax, %xmm0
cvtsi2sd %ebp, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rdi)
jmp 0x7c06
movq (%rbx), %rdi
callq 0x2b6b
movl %eax, %ebp
movq (%rbx), %rdi
callq 0x2ba1
leaq 0x2e34(%rip), %rdi      # 0xaa50
pushq $0x26
popq %r9
movl $0x2fff0000, %esi       # imm = 0x2FFF0000
movl $0x2fff0000, %edx       # imm = 0x2FFF0000
movl %ebp, %ecx
movl %eax, %r8d
callq 0x2040
movq %rax, 0x18(%rbx)
movq %rax, %rdi
callq 0x2090
movq %rax, 0x28(%rbx)
pushq $0x1
popq %rdi
callq 0x20a0
movq 0x635d(%rip), %rax      # 0xdfb0
movb $0x1, (%rax)
callq 0x2140
cmpl $0x1, 0x3c(%rbx)
leaq 0x2b02(%rip), %rax      # 0xa768
leaq 0x2cc7(%rip), %rdx      # 0xa934
cmoveq %rax, %rdx
movl $0x8b30, %esi           # imm = 0x8B30
callq 0x7e96
movl %eax, 0x38(%rbx)
leaq 0x2a1a(%rip), %rdx      # 0xa69f
movl $0x8b31, %esi           # imm = 0x8B31
callq 0x7e96
movl %eax, 0x34(%rbx)
movq 0x62ef(%rip), %rax      # 0xdf88
callq *(%rax)
movl %eax, 0x30(%rbx)
movq 0x633b(%rip), %r14      # 0xdfe0
movl 0x34(%rbx), %esi
movl %eax, %edi
callq *(%r14)
movl 0x30(%rbx), %edi
movl 0x38(%rbx), %esi
callq *(%r14)
movq 0x630b(%rip), %rax      # 0xdfc8
movl 0x30(%rbx), %edi
callq *(%rax)
movq 0x6307(%rip), %rax      # 0xdfd0
movl 0x30(%rbx), %edi
callq *(%rax)
cmpl $0x1, 0x3c(%rbx)
jne 0x7d14
leaq 0x2d7d(%rip), %rdx      # 0xaa58
movq %rbx, %rdi
xorl %esi, %esi
callq 0x7f2a
movl %eax, 0x40(%rbx)
leaq 0x2d73(%rip), %rdx      # 0xaa62
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x7f2a
movl %eax, 0x44(%rbx)
leaq 0x2d69(%rip), %rdx      # 0xaa6d
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x7f2a
movl %eax, 0x48(%rbx)
jmp 0x7d4c
leaq 0x2d5d(%rip), %rdx      # 0xaa78
movq %rbx, %rdi
xorl %esi, %esi
callq 0x7f2a
movl %eax, 0x50(%rbx)
movq (%rbx), %rdi
callq 0x2b6b
movl %eax, %ebp
movq (%rbx), %rdi
callq 0x2ba1
imull %ebp, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x2210
movq %rax, 0x58(%rbx)
movq 0x6205(%rip), %rax      # 0xdf58
movl 0x30(%rbx), %edi
leaq 0x2d27(%rip), %rsi      # 0xaa84
callq *(%rax)
movl %eax, 0x4c(%rbx)
movq %rbx, %rax
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2c43(%rip), %rdi      # 0xa9bc
jmp 0x7d82
leaq 0x2c4b(%rip), %rdi      # 0xa9cd
movq %r14, %rsi
xorl %eax, %eax
callq 0x2200
pushq $0x1
popq %rdi
callq 0x21c0