// File: /workspace/github_c_makefile_star2_O3/phoboslab[P]pl_mpeg/pl_mpeg_player_sdl.c
// Function: app_create
app_t * app_create(const char *filename) {
	app_t *self = (app_t *)malloc(sizeof(app_t));
	memset(self, 0, sizeof(app_t));
	
	// 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_RESIZABLE
	);

	// Create SDL Renderer with vsync
	self->renderer = SDL_CreateRenderer(
		self->window,
		-1,
		SDL_RENDERER_PRESENTVSYNC
	);

	// Create SDL texture with YUV format
	self->texture = SDL_CreateTexture(
		self->renderer,
		SDL_PIXELFORMAT_IYUV,
		SDL_TEXTUREACCESS_STREAMING,
		plm_get_width(self->plm),
 		plm_get_height(self->plm)
	);

	// Adjust rectangle to video size
	self->rectangle.w = plm_get_width(self->plm);
	self->rectangle.h = plm_get_height(self->plm);

	// Renderer will keep the aspect ratio of the video and center the content if the window is resized
	SDL_RenderSetLogicalSize(self->renderer, plm_get_width(self->plm), plm_get_height(self->plm));

	// Mouse position based on the renderer logical size
	SDL_SetHint(SDL_HINT_MOUSE_RELATIVE_SCALING, "1");

	// Best render scale quality
	SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2");

	return self;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x30, %rsp
movq %rdi, %r15
movl $0x1, %edi
movl $0x48, %esi
callq 0x2110
movq %rax, %rbx
movq %r15, %rdi
callq 0x23ac
movq %rax, (%rbx)
testq %rax, %rax
je 0x89bc
movq %rax, %r14
movl $0x4e2000, %esi         # imm = 0x4E2000
movq %rax, %rdi
callq 0x2c2c
testl %eax, %eax
je 0x89c5
movq %r14, %rdi
callq 0x2f54
movl %eax, %ebp
movq %r14, %rdi
callq 0x2ead
movsd %xmm0, 0x8(%rsp)
movq %r14, %rdi
callq 0x2f54
movl %eax, %r12d
movq (%r14), %rdi
movl $0xe0, %esi
callq 0x2fc3
movapd %xmm0, %xmm1
leaq 0x1f66(%rip), %rdi      # 0xa717
movq %r15, %rsi
movsd 0x8(%rsp), %xmm0
movl %r12d, %edx
movb $0x2, %al
callq 0x2200
leaq 0x215(%rip), %rax       # 0x89e0
movq %rax, 0x60(%r14)
movq %rbx, 0x68(%r14)
leaq 0x233(%rip), %rax       # 0x8a0d
movq %rax, 0x70(%r14)
movq %rbx, 0x78(%r14)
movl $0x1, %eax
movl %eax, 0x14(%r14)
movl %eax, 0x38(%r14)
movq %r14, %rdi
callq 0x25e1
xorl %ecx, %ecx
testl %eax, %eax
je 0x880d
cmpq $0x0, 0x58(%r14)
je 0x880d
movl $0xc0, %ecx
addl 0x3c(%r14), %ecx
movl %ecx, 0x40(%r14)
movq (%rbx), %rdi
movl $0x0, 0x3c(%rdi)
movl 0x38(%rdi), %esi
callq 0x2d81
movq (%rbx), %rax
movq (%rax), %r14
movq %r14, %rdi
callq 0x27a8
testl %eax, %eax
je 0x88d1
cmpl $0x0, 0x48(%r14)
jle 0x88d1
movl $0x30, %edi
callq 0x2150
leaq 0x10(%rsp), %r14
movl $0x20, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x20d0
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 0x21e0
movl %eax, 0x40(%rbx)
testl %eax, %eax
jne 0x88a9
callq 0x2130
leaq 0x1eb3(%rip), %rdi      # 0xa74f
movq %rax, %rsi
xorl %eax, %eax
callq 0x2200
movl 0x40(%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 0x2e17
movl %eax, %ebp
movq (%rbx), %rdi
callq 0x2e62
leaq 0x1e85(%rip), %rdi      # 0xa76f
movl $0x2fff0000, %esi       # imm = 0x2FFF0000
movl $0x2fff0000, %edx       # imm = 0x2FFF0000
movl %ebp, %ecx
movl %eax, %r8d
movl $0x24, %r9d
callq 0x2060
movq %rax, 0x18(%rbx)
movq %rax, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x4, %edx
callq 0x2050
movq %rax, %r14
movq %rax, 0x20(%rbx)
movq (%rbx), %rdi
callq 0x2e17
movl %eax, %ebp
movq (%rbx), %rdi
callq 0x2e62
movq %r14, %rdi
movl $0x56555949, %esi       # imm = 0x56555949
movl $0x1, %edx
movl %ebp, %ecx
movl %eax, %r8d
callq 0x2280
movq %rax, 0x28(%rbx)
movq (%rbx), %rdi
callq 0x2e17
movl %eax, 0x38(%rbx)
movq (%rbx), %rdi
callq 0x2e62
movl %eax, 0x3c(%rbx)
movq (%rbx), %rdi
movq 0x20(%rbx), %r14
callq 0x2e17
movl %eax, %ebp
movq (%rbx), %rdi
callq 0x2e62
movq %r14, %rdi
movl %ebp, %esi
movl %eax, %edx
callq 0x2090
leaq 0x1dea(%rip), %rdi      # 0xa777
leaq 0x1dfe(%rip), %rsi      # 0xa792
callq 0x2270
leaq 0x1df4(%rip), %rdi      # 0xa794
leaq 0x1e06(%rip), %rsi      # 0xa7ad
callq 0x2270
movq %rbx, %rax
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d18(%rip), %rdi      # 0xa6db
jmp 0x89cc
leaq 0x1d20(%rip), %rdi      # 0xa6ec
movq %r15, %rsi
xorl %eax, %eax
callq 0x2200
movl $0x1, %edi
callq 0x21d0