// File: /workspace/github_c_makefile_star2_O2/tvlabs[P]edge264/edge264_headers.c
// Function: parse_pic_parameter_set_log
int ADD_VARIANT(parse_pic_parameter_set)(Edge264Decoder *dec, int non_blocking,  void(*free_cb)(void*,int), void *free_arg)
{
	static const char * const weighted_pred_names[3] = {"average", "explicit", "implicit"};
	
	// temp storage, committed if entire NAL is correct
	Edge264PicParameterSet pps = {
		.transform_8x8_mode_flag = 0,
		.weightScale4x4_v = {},
		.weightScale8x8_v = {},
	};
	
	// Actual streams never use more than 4 PPSs (I, P, B, b).
	int pic_parameter_set_id = get_ue16(&dec->_gb, 255);
	get_ue16(&dec->_gb, 31); // seq_parameter_set_id
	pps.entropy_coding_mode_flag = get_u1(&dec->_gb);
	pps.bottom_field_pic_order_in_frame_present_flag = get_u1(&dec->_gb);
	int num_slice_groups = get_ue16(&dec->_gb, 7) + 1;
	log_dec(dec, "  pic_parameter_set_id: %u%s\n"
		"  entropy_coding_mode_flag: %u # %s\n"
		"  bottom_field_pic_order_in_frame_present_flag: %u\n"
		"  num_slice_groups: %u%s\n",
		pic_parameter_set_id, unsup_if(pic_parameter_set_id >= 4),
		pps.entropy_coding_mode_flag, pps.entropy_coding_mode_flag ? "CABAC" : "CAVLC",
		pps.bottom_field_pic_order_in_frame_present_flag,
		num_slice_groups, unsup_if(num_slice_groups > 1));
	
	// Let's be nice enough to print the headers for unsupported stuff.
	if (num_slice_groups > 1) {
		int slice_group_map_type = get_ue16(&dec->_gb, 6);
		log_dec(dec, "  slice_group_map_type: %u\n", slice_group_map_type);
		if (slice_group_map_type == 0) {
			log_dec(dec, "  run_lengths: [");
			for (int i = num_slice_groups; i-- > 0; ) {
				int run_length = get_ue32(&dec->_gb, 139263) + 1; // level 6.2
				log_dec(dec, i ? "%u," : "%u]\n", run_length);
			}
		} else if (slice_group_map_type == 2) {
			log_dec(dec, "  top_lefts_bottom_rights: [");
			for (int i = num_slice_groups - 1; i-- > 0; ) {
				int top_left = get_ue32(&dec->_gb, 139264);
				int bottom_right = get_ue32(&dec->_gb, 139264);
				log_dec(dec, i ? "%u-%u," : "%u-%u]\n", top_left, bottom_right);
			}
		} else if (0b111000 & 1 << slice_group_map_type) { // 3, 4 or 5
			int slice_group_change_direction_flag = get_u1(&dec->_gb);
			int slice_group_change_rate = get_ue32(&dec->_gb, 139263) + 1;
			log_dec(dec, "  slice_group_change_direction_flag: %u\n"
				"  slice_group_change_rate: %u\n",
				slice_group_change_direction_flag,
				slice_group_change_rate);
		} else if (slice_group_map_type == 6) {
			int PicSizeInMapUnits = get_ue32(&dec->_gb, 139263) + 1;
			log_dec(dec, "  slice_group_ids: [");
			int bits = WORD_BIT - __builtin_clz(num_slice_groups - 1);
			for (int i = PicSizeInMapUnits; i-- > 0; ) {
				int slice_group_id = get_uv(&dec->_gb, bits);
				log_dec(dec, i ? "%u" : "%u]\n", slice_group_id);
			}
		}
	}
	
	// (num_ref_idx_active[0] != 0) is used as indicator that the PPS is initialised.
	pps.num_ref_idx_active[0] = get_ue16(&dec->_gb, 31) + 1;
	pps.num_ref_idx_active[1] = get_ue16(&dec->_gb, 31) + 1;
	pps.weighted_pred_flag = get_u1(&dec->_gb);
	pps.weighted_bipred_idc = get_uv(&dec->_gb, 2);
	pps.QPprime_Y = get_se16(&dec->_gb, -26, 25) + 26; // FIXME QpBdOffset
	get_se16(&dec->_gb, -26, 25); // pic_init_qs
	pps.second_chroma_qp_index_offset = pps.chroma_qp_index_offset = get_se16(&dec->_gb, -12, 12);
	pps.deblocking_filter_control_present_flag = get_u1(&dec->_gb);
	pps.constrained_intra_pred_flag = get_u1(&dec->_gb);
	int redundant_pic_cnt_present_flag = get_u1(&dec->_gb);
	log_dec(dec, "  num_ref_idx_default_active: {l0: %u, l1: %u}\n"
		"  weighted_pred_flag: %u # %s\n"
		"  weighted_bipred_idc: %u # %s\n"
		"  pic_init_qp: %u\n"
		"  chroma_qp_index_offset: %d\n"
		"  deblocking_filter_control_present_flag: %u\n"
		"  constrained_intra_pred_flag: %u%s\n"
		"  redundant_pic_cnt_present_flag: %u%s\n",
		pps.num_ref_idx_active[0], pps.num_ref_idx_active[1],
		pps.weighted_pred_flag, weighted_pred_names[pps.weighted_pred_flag],
		pps.weighted_bipred_idc, weighted_pred_names[pps.weighted_bipred_idc],
		pps.QPprime_Y,
		pps.chroma_qp_index_offset,
		pps.deblocking_filter_control_present_flag,
		pps.constrained_intra_pred_flag, unsup_if(pps.constrained_intra_pred_flag),
		redundant_pic_cnt_present_flag, unsup_if(redundant_pic_cnt_present_flag));
	
	if (!rbsp_end(&dec->_gb)) {
		pps.transform_8x8_mode_flag = get_u1(&dec->_gb);
		log_dec(dec, "  transform_8x8_mode_flag: %u\n",
			pps.transform_8x8_mode_flag);
		pps.pic_scaling_matrix_present_flag = get_u1(&dec->_gb);
		if (pps.pic_scaling_matrix_present_flag) {
			log_dec(dec, "  pic_scaling_matrix:\n");
			parse_scaling_lists(dec, pps.weightScale4x4_v, pps.weightScale8x8_v, pps.transform_8x8_mode_flag, dec->sps.chroma_format_idc);
		}
		pps.second_chroma_qp_index_offset = get_se16(&dec->_gb, -12, 12);
		log_dec(dec, "  second_chroma_qp_index_offset: %d\n",
			pps.second_chroma_qp_index_offset);
	}
	
	// check for trailing_bits before unsupported features (in case errors enabled them)
	if (!rbsp_end(&dec->_gb))
		return EBADMSG;
	if (print_dec(dec) || pic_parameter_set_id >= 4 ||
		num_slice_groups > 1 || pps.constrained_intra_pred_flag ||
		redundant_pic_cnt_present_flag)
		return ENOTSUP;
	dec->PPS[pic_parameter_set_id] = pps;
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movq %rdi, %rbx
leaq 0x30(%rsp), %r14
movl $0x1f0, %edx            # imm = 0x1F0
movq %r14, %rdi
xorl %esi, %esi
callq 0x1090
movq %rbx, %rdi
movl $0xff, %esi
callq 0x1f4be
movl %eax, %r12d
pushq $0x1f
popq %rsi
movq %rbx, %rdi
callq 0x1f4be
movq %rbx, %rdi
callq 0x1f662
movl %eax, %ebp
movb %bpl, (%r14)
movq %rbx, %rdi
callq 0x1f662
movl %eax, %r15d
movb %r15b, 0x1(%r14)
pushq $0x7
popq %rsi
movq %rbx, %rdi
callq 0x1f4be
movl %eax, %r14d
leal 0x1(%r14), %r13d
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x1f9ea
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
cmpl $0x4, %r12d
leaq 0x14efc(%rip), %rax     # 0x3488f
leaq 0x11c18(%rip), %r10     # 0x315b2
movq %r10, %r8
cmovlq %rax, %r8
movsbl %bpl, %r9d
testb %bpl, %bpl
leaq 0x142be(%rip), %rcx     # 0x33c6d
leaq 0x142b1(%rip), %r11     # 0x33c67
cmoveq %rcx, %r11
movsbl %r15b, %r15d
cmpl $0x2, %r13d
cmovlq %rax, %r10
leaq 0x1420c(%rip), %rdx     # 0x33bd9
movl %r12d, %ecx
xorl %eax, %eax
pushq %r10
pushq %r13
pushq %r15
pushq %r11
callq 0x1070
addq $0x20, %rsp
addw %ax, 0x7cb0(%rbx)
movl %r12d, 0x14(%rsp)
movq %r13, 0x20(%rsp)
cmpl $0x2, %r13d
jl 0x1fcdb
pushq $0x6
popq %rsi
movq %rbx, %rdi
callq 0x1f4be
movl %eax, %ebp
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x1fa4b
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %esi           # imm = 0x2542
subq %rax, %rsi
leaq 0x1423f(%rip), %rdx     # 0x33c73
movl %ebp, %ecx
xorl %eax, %eax
callq 0x1070
addw 0x7cb0(%rbx), %ax
movw %ax, 0x7cb0(%rbx)
cmpl $0x2, %ebp
je 0x1fb01
testl %ebp, %ebp
movq 0x20(%rsp), %r13
jne 0x1fbb1
cmpw $0x2541, %ax            # imm = 0x2541
ja 0x1fa94
movzwl %ax, %ecx
leaq (%rbx,%rcx), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
leaq 0x14209(%rip), %rdx     # 0x33c8f
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
leaq 0x7cb2(%rbx), %r14
leaq 0x14d13(%rip), %r15     # 0x347b5
leaq 0x14d66(%rip), %r12     # 0x3480f
movl %r13d, %ebp
testl %ebp, %ebp
jle 0x1fcdb
movq %rbx, %rdi
movl $0x21fff, %esi          # imm = 0x21FFF
callq 0x1f4be
movzwl 0x7cb0(%rbx), %ecx
cmpq $0x2541, %rcx           # imm = 0x2541
ja 0x1fafd
incl %eax
movq %rcx, %rdi
addq %r14, %rdi
movl $0x2542, %edx           # imm = 0x2542
subl %ecx, %edx
movzwl %dx, %esi
cmpl $0x1, %ebp
movq %r12, %rdx
cmoveq %r15, %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
decl %ebp
jmp 0x1faac
cmpw $0x2541, %ax            # imm = 0x2541
ja 0x1fb34
movzwl %ax, %ecx
leaq (%rbx,%rcx), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
leaq 0x1417a(%rip), %rdx     # 0x33ca0
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
leaq 0x7cb2(%rbx), %r15
leaq 0x14182(%rip), %r12     # 0x33cc4
leaq 0x14174(%rip), %r13     # 0x33cbd
testl %r14d, %r14d
jle 0x1fcdb
movq %rbx, %rdi
movl $0x22000, %esi          # imm = 0x22000
callq 0x1f4be
movl %eax, %ebp
movq %rbx, %rdi
movl $0x22000, %esi          # imm = 0x22000
callq 0x1f4be
movzwl 0x7cb0(%rbx), %ecx
cmpq $0x2541, %rcx           # imm = 0x2541
ja 0x1fbac
movq %rcx, %rdi
addq %r15, %rdi
movl $0x2542, %edx           # imm = 0x2542
subl %ecx, %edx
movzwl %dx, %esi
cmpl $0x1, %r14d
movq %r13, %rdx
cmoveq %r12, %rdx
movl %ebp, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
decl %r14d
jmp 0x1fb49
pushq $0x38
popq %rax
btl %ebp, %eax
jae 0x1fc1a
movq %rbx, %rdi
callq 0x1f662
movl %eax, %ebp
movq %rbx, %rdi
movl $0x21fff, %esi          # imm = 0x21FFF
callq 0x1f4be
movzwl 0x7cb0(%rbx), %ecx
cmpq $0x2541, %rcx           # imm = 0x2541
ja 0x1fcdb
incl %eax
leaq (%rbx,%rcx), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %edx           # imm = 0x2542
subl %ecx, %edx
movzwl %dx, %esi
leaq 0x140ca(%rip), %rdx     # 0x33ccc
movl %ebp, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
jmp 0x1fcdb
cmpl $0x6, %ebp
jne 0x1fcdb
movq %rbx, %rdi
movl $0x21fff, %esi          # imm = 0x21FFF
callq 0x1f4be
movl %eax, %ebp
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x1fc6b
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
leaq 0x140b6(%rip), %rdx     # 0x33d13
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
incl %ebp
lzcntl %r14d, %eax
pushq $0x20
popq %r14
subl %eax, %r14d
leaq 0x7cb2(%rbx), %r15
leaq 0x14b2e(%rip), %r12     # 0x347b5
leaq 0x13e21(%rip), %r13     # 0x33aaf
testl %ebp, %ebp
jle 0x1fcdb
movq %rbx, %rdi
movl %r14d, %esi
callq 0x1539a
movzwl 0x7cb0(%rbx), %ecx
cmpq $0x2541, %rcx           # imm = 0x2541
ja 0x1fcd7
movq %rcx, %rdi
addq %r15, %rdi
movl $0x2542, %edx           # imm = 0x2542
subl %ecx, %edx
movzwl %dx, %esi
cmpl $0x1, %ebp
movq %r13, %rdx
cmoveq %r12, %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
decl %ebp
jmp 0x1fc8e
pushq $0x1f
popq %rbp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1f4be
movl %eax, %r15d
incb %r15b
movb %r15b, 0x32(%rsp)
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1f4be
movl %eax, %r13d
incb %r13b
movb %r13b, 0x33(%rsp)
movq %rbx, %rdi
callq 0x1f662
movl %eax, (%rsp)
movb %al, 0x34(%rsp)
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x1539a
movl %eax, 0x8(%rsp)
movb %al, 0x35(%rsp)
pushq $-0x1a
popq %r14
pushq $0x19
popq %r12
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x1f6a0
movl %eax, %ebp
addb $0x1a, %bpl
movb %bpl, 0x36(%rsp)
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x1f6a0
pushq $-0xc
popq %rsi
pushq $0xc
popq %rdx
movq %rbx, %rdi
callq 0x1f6a0
movl %eax, %r12d
movb %r12b, 0x37(%rsp)
movb %r12b, 0x3c(%rsp)
movq %rbx, %rdi
callq 0x1f662
movl %eax, 0x10(%rsp)
movb %al, 0x38(%rsp)
movq %rbx, %rdi
callq 0x1f662
movl %eax, %r14d
movb %r14b, 0x39(%rsp)
movq %rbx, %rdi
callq 0x1f662
movq %rax, 0x18(%rsp)
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x1fe6f
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
movsbl %r15b, %ecx
movsbl %r13b, %r8d
movl (%rsp), %eax
movsbl %al, %r9d
movsbq %al, %rax
movq %rax, (%rsp)
movsbl %r14b, %r13d
testb $-0x1, %r13b
leaq 0x14aa0(%rip), %rax     # 0x3488f
leaq 0x117bc(%rip), %r10     # 0x315b2
movq %r10, %r11
cmoveq %rax, %r11
movq 0x18(%rsp), %r15
testl %r15d, %r15d
cmoveq %rax, %r10
movl 0x8(%rsp), %eax
movsbl %al, %edx
movq %rdx, 0x28(%rsp)
movsbq %al, %rax
movq %rax, 0x8(%rsp)
movsbl %bpl, %ebp
movsbl %r12b, %r12d
movsbl 0x10(%rsp), %r14d
leaq 0x13ef5(%rip), %rdx     # 0x33d28
xorl %eax, %eax
pushq %r10
pushq %r15
pushq %r11
pushq %r13
pushq %r14
pushq %r12
pushq %rbp
leaq 0x19f27(%rip), %r11     # 0x39d70
movq 0x40(%rsp), %r10
pushq (%r11,%r10,8)
pushq 0x68(%rsp)
movq 0x48(%rsp), %r10
pushq (%r11,%r10,8)
callq 0x1070
addq $0x50, %rsp
addw %ax, 0x7cb0(%rbx)
movq 0x10(%rbx), %rcx
movq 0x18(%rbx), %rdx
movq %rdx, %rax
shrq $0x3f, %rax
shlxq %rax, %rdx, %rdx
blsrq %rcx, %rsi
orq %rdx, %rsi
movl 0x14(%rsp), %r14d
movq 0x20(%rsp), %r15
jne 0x1feb4
tzcntq %rcx, %rcx
movl (%rbx), %edx
subl 0x8(%rbx), %edx
addl %eax, %ecx
leal (%rcx,%rdx,8), %eax
addl $0xffffff78, %eax       # imm = 0xFFFFFF78
cmpl $-0x11, %eax
ja 0x1ffb9
movq %rbx, %rdi
callq 0x1f662
movl %eax, %ebp
movb %bpl, 0x3a(%rsp)
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x1ff00
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
movsbl %bpl, %ecx
leaq 0x13f4a(%rip), %rdx     # 0x33e3c
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
movq %rbx, %rdi
callq 0x1f662
movb %al, 0x3b(%rsp)
testb %al, %al
je 0x1ff6a
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x1ff49
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
leaq 0x13f20(%rip), %rdx     # 0x33e5b
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
leaq 0x40(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
movsbl %bpl, %ecx
movsbl 0x530(%rbx), %r8d
movq %rbx, %rdi
callq 0x20068
pushq $-0xc
popq %rsi
pushq $0xc
popq %rdx
movq %rbx, %rdi
callq 0x1f6a0
movb %al, 0x3c(%rsp)
movzwl 0x7cb0(%rbx), %ecx
cmpq $0x2541, %rcx           # imm = 0x2541
ja 0x1ffb9
leaq (%rbx,%rcx), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %edx           # imm = 0x2542
subl %ecx, %edx
movzwl %dx, %esi
movsbl %al, %ecx
leaq 0x13ec7(%rip), %rdx     # 0x33e72
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
movq 0x10(%rbx), %rdx
movq 0x18(%rbx), %rax
movq %rax, %rcx
shrq $0x3f, %rcx
shlxq %rcx, %rax, %rsi
blsrq %rdx, %rdi
pushq $0x4a
popq %rax
orq %rsi, %rdi
jne 0x20056
tzcntq %rdx, %rdx
movl (%rbx), %esi
subl 0x8(%rbx), %esi
addl %ecx, %edx
leal (%rdx,%rsi,8), %ecx
addl $0xffffff78, %ecx       # imm = 0xFFFFFF78
cmpl $-0x10, %ecx
jb 0x20056
cmpl $0x2, %r15d
setge %bpl
movq %rbx, %rdi
callq 0x15361
testl %eax, %eax
setne %al
cmpl $0x4, %r14d
setge %cl
orb %bpl, %cl
orb %al, %cl
cmpb $0x0, 0x39(%rsp)
setne %al
cmpl $0x0, 0x18(%rsp)
setne %dl
orb %cl, %dl
orb %al, %dl
je 0x20030
pushq $0x5f
popq %rax
jmp 0x20056
movslq %r14d, %rax
imulq $0x1f0, %rax, %rax      # imm = 0x1F0
leaq (%rbx,%rax), %rdi
addq $0xd50, %rdi            # imm = 0xD50
leaq 0x30(%rsp), %rsi
movl $0x1f0, %edx            # imm = 0x1F0
callq 0x10c0
xorl %eax, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq