// File: /workspace/github_c_makefile_star2_O0/phoboslab[P]pl_mpeg/./pl_mpeg.h
// Function: plm_video_decode_sequence_header
int plm_video_decode_sequence_header(plm_video_t *self) {
	int max_header_size = 64 + 2 * 64 * 8; // 64 bit header + 2x 64 byte matrix
	if (!plm_buffer_has(self->buffer, max_header_size)) {
		return FALSE;
	}

	self->width = plm_buffer_read(self->buffer, 12);
	self->height = plm_buffer_read(self->buffer, 12);

	if (self->width <= 0 || self->height <= 0) {
		return FALSE;
	}

	// Get pixel aspect ratio
	int pixel_aspect_ratio_code;
	pixel_aspect_ratio_code = plm_buffer_read(self->buffer, 4);
	pixel_aspect_ratio_code -= 1;
	if (pixel_aspect_ratio_code < 0) {
		pixel_aspect_ratio_code = 0;
	}
	int par_last = (sizeof(PLM_VIDEO_PIXEL_ASPECT_RATIO) /
			sizeof(PLM_VIDEO_PIXEL_ASPECT_RATIO[0]) - 1);
	if (pixel_aspect_ratio_code > par_last) {
		pixel_aspect_ratio_code = par_last;
	}
	self->pixel_aspect_ratio =
		PLM_VIDEO_PIXEL_ASPECT_RATIO[pixel_aspect_ratio_code];

	// Get frame rate
	self->framerate = PLM_VIDEO_PICTURE_RATE[plm_buffer_read(self->buffer, 4)];

	// Skip bit_rate, marker, buffer_size and constrained bit
	plm_buffer_skip(self->buffer, 18 + 1 + 10 + 1);

	// Load custom intra quant matrix?
	if (plm_buffer_read(self->buffer, 1)) { 
		for (int i = 0; i < 64; i++) {
			int idx = PLM_VIDEO_ZIG_ZAG[i];
			self->intra_quant_matrix[idx] = plm_buffer_read(self->buffer, 8);
		}
	}
	else {
		memcpy(self->intra_quant_matrix, PLM_VIDEO_INTRA_QUANT_MATRIX, 64);
	}

	// Load custom non intra quant matrix?
	if (plm_buffer_read(self->buffer, 1)) { 
		for (int i = 0; i < 64; i++) {
			int idx = PLM_VIDEO_ZIG_ZAG[i];
			self->non_intra_quant_matrix[idx] = plm_buffer_read(self->buffer, 8);
		}
	}
	else {
		memcpy(self->non_intra_quant_matrix, PLM_VIDEO_NON_INTRA_QUANT_MATRIX, 64);
	}

	self->mb_width = (self->width + 15) >> 4;
	self->mb_height = (self->height + 15) >> 4;
	self->mb_size = self->mb_width * self->mb_height;

	self->luma_width = self->mb_width << 4;
	self->luma_height = self->mb_height << 4;

	self->chroma_width = self->mb_width << 3;
	self->chroma_height = self->mb_height << 3;


	// Allocate one big chunk of data for all 3 frames = 9 planes
	size_t luma_plane_size = self->luma_width * self->luma_height;
	size_t chroma_plane_size = self->chroma_width * self->chroma_height;
	size_t frame_data_size = (luma_plane_size + 2 * chroma_plane_size);

	self->frames_data = (uint8_t*)PLM_MALLOC(frame_data_size * 3);
	plm_video_init_frame(self, &self->frame_current, self->frames_data + frame_data_size * 0);
	plm_video_init_frame(self, &self->frame_forward, self->frames_data + frame_data_size * 1);
	plm_video_init_frame(self, &self->frame_backward, self->frames_data + frame_data_size * 2);

	self->has_sequence_header = TRUE;
	return TRUE;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl $0x440, -0x14(%rbp)     # imm = 0x440
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5270
cmpl $0x0, %eax
jne 0x5b78
movl $0x0, -0x4(%rbp)
jmp 0x5ee7
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rdi
movl $0xc, %esi
callq 0x5320
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rdi
movl $0xc, %esi
callq 0x5320
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jle 0x5bc8
movq -0x10(%rbp), %rax
cmpl $0x0, 0x20(%rax)
jg 0x5bd4
movl $0x0, -0x4(%rbp)
jmp 0x5ee7
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rdi
movl $0x4, %esi
callq 0x5320
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jge 0x5c02
movl $0x0, -0x18(%rbp)
movl $0xd, -0x1c(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jle 0x5c17
movl -0x1c(%rbp), %eax
movl %eax, -0x18(%rbp)
movslq -0x18(%rbp), %rcx
leaq 0x744e(%rip), %rax      # 0xd070
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rdi
movl $0x4, %esi
callq 0x5320
movslq %eax, %rcx
leaq 0x745d(%rip), %rax      # 0xd0b0
movsd (%rax,%rcx,8), %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rdi
movl $0x1e, %esi
callq 0x5450
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rdi
movl $0x1, %esi
callq 0x5320
cmpl $0x0, %eax
je 0x5ce1
movl $0x0, -0x20(%rbp)
cmpl $0x40, -0x20(%rbp)
jge 0x5cdf
movslq -0x20(%rbp), %rcx
leaq 0x7489(%rip), %rax      # 0xd130
movzbl (%rax,%rcx), %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rdi
movl $0x8, %esi
callq 0x5320
movb %al, %dl
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movb %dl, 0x278(%rax,%rcx)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x5c96
jmp 0x5cfd
movq -0x10(%rbp), %rdi
addq $0x278, %rdi            # imm = 0x278
leaq 0x747d(%rip), %rsi      # 0xd170
movl $0x40, %edx
callq 0x2130
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rdi
movl $0x1, %esi
callq 0x5320
cmpl $0x0, %eax
je 0x5d69
movl $0x0, -0x28(%rbp)
cmpl $0x40, -0x28(%rbp)
jge 0x5d67
movslq -0x28(%rbp), %rcx
leaq 0x7401(%rip), %rax      # 0xd130
movzbl (%rax,%rcx), %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rdi
movl $0x8, %esi
callq 0x5320
movb %al, %dl
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movb %dl, 0x2b8(%rax,%rcx)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x5d1e
jmp 0x5d85
movq -0x10(%rbp), %rdi
addq $0x2b8, %rdi            # imm = 0x2B8
leaq 0x7435(%rip), %rsi      # 0xd1b0
movl $0x40, %edx
callq 0x2130
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %ecx
addl $0xf, %ecx
sarl $0x4, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x10(%rbp), %rax
movl 0x20(%rax), %ecx
addl $0xf, %ecx
sarl $0x4, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x10(%rbp), %rax
movl 0x24(%rax), %ecx
movq -0x10(%rbp), %rax
imull 0x28(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x2c(%rax)
movq -0x10(%rbp), %rax
movl 0x24(%rax), %ecx
shll $0x4, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x30(%rax)
movq -0x10(%rbp), %rax
movl 0x28(%rax), %ecx
shll $0x4, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x34(%rax)
movq -0x10(%rbp), %rax
movl 0x24(%rax), %ecx
shll $0x3, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x38(%rax)
movq -0x10(%rbp), %rax
movl 0x28(%rax), %ecx
shll $0x3, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x3c(%rax)
movq -0x10(%rbp), %rax
movl 0x30(%rax), %eax
movq -0x10(%rbp), %rcx
imull 0x34(%rcx), %eax
cltq
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movl 0x38(%rax), %eax
movq -0x10(%rbp), %rcx
imull 0x3c(%rcx), %eax
cltq
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
imulq $0x3, -0x48(%rbp), %rdi
callq 0x2200
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x170(%rax)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0xb0, %rsi
movq -0x10(%rbp), %rax
movq 0x170(%rax), %rdx
imulq $0x0, -0x48(%rbp), %rax
addq %rax, %rdx
callq 0x61c0
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0xf0, %rsi
movq -0x10(%rbp), %rax
movq 0x170(%rax), %rdx
movq -0x48(%rbp), %rax
shlq $0x0, %rax
addq %rax, %rdx
callq 0x61c0
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x130, %rsi            # imm = 0x130
movq -0x10(%rbp), %rax
movq 0x170(%rax), %rdx
movq -0x48(%rbp), %rax
shlq %rax
addq %rax, %rdx
callq 0x61c0
movq -0x10(%rbp), %rax
movl $0x1, 0x70(%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq