// File: /workspace/github_c_makefile_star2_O1/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 %rbx
subq $0x28, %rsp
movq %rdi, %r14
movl $0x48, %edi
callq 0x2200
movq %rax, %rbx
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movupd %xmm0, 0x10(%rax)
movupd %xmm0, 0x20(%rax)
movupd %xmm0, 0x30(%rax)
movq $0x0, 0x40(%rax)
movq %r14, %rdi
callq 0x239c
movq %rax, (%rbx)
testq %rax, %rax
je 0x8c01
movl $0x4e2000, %esi         # imm = 0x4E2000
movq %rax, %rdi
callq 0x2d4a
testl %eax, %eax
je 0x8c0a
movq (%rbx), %rdi
callq 0x3069
movl %eax, %ebp
movq (%rbx), %rdi
callq 0x2fc2
movsd %xmm0, (%rsp)
movq (%rbx), %rdi
callq 0x3069
movl %eax, %r15d
movq (%rbx), %rax
movq (%rax), %rdi
movl $0xe0, %esi
callq 0x30d8
movapd %xmm0, %xmm1
leaq 0x1b67(%rip), %rdi      # 0xa55f
movq %r14, %rsi
movsd (%rsp), %xmm0
movl %r15d, %edx
movb $0x2, %al
callq 0x21f0
movq (%rbx), %r14
leaq 0x211(%rip), %rax       # 0x8c25
movq %rax, 0x60(%r14)
movq %rbx, 0x68(%r14)
leaq 0x22f(%rip), %rax       # 0x8c52
movq %rax, 0x70(%r14)
movq %rbx, 0x78(%r14)
movl $0x1, %eax
movl %eax, 0x14(%r14)
movl %eax, 0x38(%r14)
movq %r14, %rdi
callq 0x267c
xorl %ecx, %ecx
testl %eax, %eax
je 0x8a56
cmpq $0x0, 0x58(%r14)
je 0x8a56
movl $0xc0, %ecx
addl 0x3c(%r14), %ecx
movl %ecx, 0x40(%r14)
movq (%rbx), %rdi
movl $0x0, 0x3c(%rdi)
movl 0x38(%rdi), %esi
callq 0x2e96
movq (%rbx), %rax
movq (%rax), %r14
movq %r14, %rdi
callq 0x2894
testl %eax, %eax
je 0x8b18
cmpl $0x0, 0x48(%r14)
jle 0x8b18
movl $0x30, %edi
callq 0x2140
leaq 0x8(%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 0x21d0
movl %eax, 0x40(%rbx)
testl %eax, %eax
jne 0x8aef
callq 0x2120
leaq 0x1ab2(%rip), %rdi      # 0xa597
movq %rax, %rsi
xorl %eax, %eax
callq 0x21f0
movl 0x40(%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 0x2f2c
movl %eax, %ebp
movq (%rbx), %rdi
callq 0x2f77
leaq 0x1a86(%rip), %rdi      # 0xa5b7
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 0x2f2c
movl %eax, %ebp
movq (%rbx), %rdi
callq 0x2f77
movq %r14, %rdi
movl $0x56555949, %esi       # imm = 0x56555949
movl $0x1, %edx
movl %ebp, %ecx
movl %eax, %r8d
callq 0x2270
movq %rax, 0x28(%rbx)
movq (%rbx), %rdi
callq 0x2f2c
movl %eax, 0x38(%rbx)
movq (%rbx), %rdi
callq 0x2f77
movl %eax, 0x3c(%rbx)
movq (%rbx), %rdi
movq 0x20(%rbx), %r14
callq 0x2f2c
movl %eax, %ebp
movq (%rbx), %rdi
callq 0x2f77
movq %r14, %rdi
movl %ebp, %esi
movl %eax, %edx
callq 0x2090
leaq 0x19eb(%rip), %rdi      # 0xa5bf
leaq 0x19ff(%rip), %rsi      # 0xa5da
callq 0x2260
leaq 0x19f5(%rip), %rdi      # 0xa5dc
leaq 0x1a07(%rip), %rsi      # 0xa5f5
callq 0x2260
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x191b(%rip), %rdi      # 0xa523
jmp 0x8c11
leaq 0x1923(%rip), %rdi      # 0xa534
movq %r14, %rsi
xorl %eax, %eax
callq 0x21f0
movl $0x1, %edi
callq 0x21c0