// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movl $0x48, %edi
callq 0x2200
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x48, %edx
callq 0x20e0
movq -0x8(%rbp), %rdi
callq 0x23a0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
jne 0xc0c6
movq -0x8(%rbp), %rsi
leaq 0x24ae(%rip), %rdi      # 0xe563
movb $0x0, %al
callq 0x21f0
movl $0x1, %edi
callq 0x21c0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl $0x4e2000, %esi         # imm = 0x4E2000
callq 0x3040
cmpl $0x0, %eax
jne 0xc0f8
movq -0x8(%rbp), %rsi
leaq 0x248d(%rip), %rdi      # 0xe574
movb $0x0, %al
callq 0x21f0
movl $0x1, %edi
callq 0x21c0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x35d0
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x3490
movsd %xmm0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x35d0
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x36d0
movq -0x50(%rbp), %rsi
movl -0x44(%rbp), %edx
movaps %xmm0, %xmm1
movsd -0x40(%rbp), %xmm0
leaq 0x244e(%rip), %rdi      # 0xe59f
movb $0x2, %al
callq 0x21f0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x246(%rip), %rsi       # 0xc3b0
callq 0x3a90
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x29f(%rip), %rsi       # 0xc420
callq 0x3ac0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl $0x1, %esi
callq 0x3a50
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl $0x1, %esi
callq 0x3210
movq -0x10(%rbp), %rax
movq (%rax), %rdi
xorl %esi, %esi
callq 0x3280
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x35b0
cmpl $0x0, %eax
jle 0xc265
movl $0x30, %edi
callq 0x2140
leaq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x20d0
movl -0x14(%rbp), %eax
movl %eax, -0x38(%rbp)
movw $0x8120, -0x34(%rbp)    # imm = 0x8120
movb $0x2, -0x32(%rbp)
movw $0x1000, -0x30(%rbp)    # imm = 0x1000
xorl %eax, %eax
movl %eax, %ecx
xorl %r8d, %r8d
leaq -0x38(%rbp), %rdx
movq %rcx, %rdi
movl %r8d, %esi
callq 0x21d0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x40(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x40(%rax)
jne 0xc23a
callq 0x2120
movq %rax, %rsi
leaq 0x23a4(%rip), %rdi      # 0xe5d7
movb $0x0, %al
callq 0x21f0
movq -0x10(%rbp), %rax
movl 0x40(%rax), %edi
xorl %esi, %esi
callq 0x21e0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movzwl -0x30(%rbp), %eax
cvtsi2sd %eax, %xmm0
cvtsi2sdl -0x14(%rbp), %xmm1
divsd %xmm1, %xmm0
callq 0x3690
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x3370
movl %eax, -0x74(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x3400
movl -0x74(%rbp), %ecx
movl %eax, %r8d
leaq 0x236a(%rip), %rdi      # 0xe5f7
movl $0x2fff0000, %edx       # imm = 0x2FFF0000
movl $0x24, %r9d
movl %edx, %esi
callq 0x2060
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x4, %edx
callq 0x2050
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x3370
movl %eax, -0x64(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x3400
movq -0x70(%rbp), %rdi
movl -0x64(%rbp), %ecx
movl %eax, %r8d
movl $0x56555949, %esi       # imm = 0x56555949
movl $0x1, %edx
callq 0x2270
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x3370
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x38(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x3400
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x3c(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x3370
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x3400
movq -0x60(%rbp), %rdi
movl -0x54(%rbp), %esi
movl %eax, %edx
callq 0x2090
leaq 0x2282(%rip), %rdi      # 0xe5ff
leaq 0x2296(%rip), %rsi      # 0xe61a
callq 0x2260
leaq 0x228c(%rip), %rdi      # 0xe61c
leaq 0x229e(%rip), %rsi      # 0xe635
callq 0x2260
movq -0x10(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)