// File: /workspace/github_c_makefile_star2_O3/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 $0x238, %rsp            # imm = 0x238
movq %rdi, %rbx
leaq 0x40(%rsp), %r14
movl $0x1f0, %edx            # imm = 0x1F0
movq %r14, %rdi
xorl %esi, %esi
callq 0x1090
movq %rbx, %rdi
movl $0xff, %esi
callq 0x25872
movl %eax, 0xc(%rsp)
movq %rbx, %rdi
movl $0x1f, %esi
callq 0x25872
movq %rbx, %rdi
callq 0x25dac
movl %eax, %ebp
movb %bpl, (%r14)
movq %rbx, %rdi
callq 0x25dac
movl %eax, %r15d
movb %r15b, 0x1(%r14)
movq %rbx, %rdi
movl $0x7, %esi
callq 0x25872
movl %eax, %r14d
leal 0x1(%r14), %r12d
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x261b7
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
movl 0xc(%rsp), %ecx
cmpl $0x4, %ecx
leaq 0x1487c(%rip), %rax     # 0x3a9df
leaq 0x11590(%rip), %r10     # 0x376fa
movq %r10, %r8
cmovlq %rax, %r8
movsbl %bpl, %r9d
testb %bpl, %bpl
leaq 0x13c3e(%rip), %rdx     # 0x39dbd
leaq 0x13c31(%rip), %r11     # 0x39db7
cmoveq %rdx, %r11
movsbl %r15b, %r15d
cmpl $0x2, %r12d
cmovlq %rax, %r10
leaq 0x13b8c(%rip), %rdx     # 0x39d29
xorl %eax, %eax
pushq %r10
pushq %r12
pushq %r15
pushq %r11
callq 0x1070
addq $0x20, %rsp
addw %ax, 0x7cb0(%rbx)
movq %r12, 0x30(%rsp)
cmpl $0x2, %r12d
jl 0x264b6
movq %rbx, %rdi
movl $0x6, %esi
callq 0x25872
movl %eax, %ebp
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x26215
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %esi           # imm = 0x2542
subq %rax, %rsi
leaq 0x13bc5(%rip), %rdx     # 0x39dc3
movl %ebp, %ecx
xorl %eax, %eax
callq 0x1070
addw 0x7cb0(%rbx), %ax
movw %ax, 0x7cb0(%rbx)
cmpl $0x2, %ebp
je 0x262cb
testl %ebp, %ebp
jne 0x2637f
movzwl %ax, %ecx
cmpl $0x2541, %ecx           # imm = 0x2541
ja 0x2625b
leaq (%rbx,%rcx), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
leaq 0x13b92(%rip), %rdx     # 0x39ddf
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
leaq 0x7cb2(%rbx), %r14
leaq 0x1469c(%rip), %r15     # 0x3a905
leaq 0x146ef(%rip), %r12     # 0x3a95f
movq 0x30(%rsp), %rax
movl %eax, %ebp
movq %rbx, %rdi
movl $0x21fff, %esi          # imm = 0x21FFF
callq 0x25872
movzwl 0x7cb0(%rbx), %ecx
cmpq $0x2541, %rcx           # imm = 0x2541
ja 0x262c0
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
testl %ebp, %ebp
jg 0x26277
jmp 0x264b6
movzwl %ax, %eax
cmpl $0x2541, %eax           # imm = 0x2541
ja 0x262ff
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
leaq 0x13aff(%rip), %rdx     # 0x39df0
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
leaq 0x7cb2(%rbx), %r15
incl %r14d
leaq 0x13b04(%rip), %r12     # 0x39e14
leaq 0x13af6(%rip), %r13     # 0x39e0d
movq %rbx, %rdi
movl $0x22000, %esi          # imm = 0x22000
callq 0x25872
movl %eax, %ebp
movq %rbx, %rdi
movl $0x22000, %esi          # imm = 0x22000
callq 0x25872
movzwl 0x7cb0(%rbx), %ecx
cmpq $0x2541, %rcx           # imm = 0x2541
ja 0x26371
movq %rcx, %rdi
addq %r15, %rdi
movl $0x2542, %edx           # imm = 0x2542
subl %ecx, %edx
movzwl %dx, %esi
cmpl $0x2, %r14d
movq %r13, %rdx
cmoveq %r12, %rdx
movl %ebp, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
decl %r14d
cmpl $0x1, %r14d
ja 0x26317
jmp 0x264b6
movl $0x38, %eax
btl %ebp, %eax
jae 0x263ea
movq %rbx, %rdi
callq 0x25dac
movl %eax, %ebp
movq %rbx, %rdi
movl $0x21fff, %esi          # imm = 0x21FFF
callq 0x25872
movzwl 0x7cb0(%rbx), %ecx
cmpq $0x2541, %rcx           # imm = 0x2541
ja 0x264b6
incl %eax
leaq (%rbx,%rcx), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %edx           # imm = 0x2542
subl %ecx, %edx
movzwl %dx, %esi
leaq 0x13a4a(%rip), %rdx     # 0x39e1c
movl %ebp, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
jmp 0x264b6
cmpl $0x6, %ebp
jne 0x264b6
movq %rbx, %rdi
movl $0x21fff, %esi          # imm = 0x21FFF
callq 0x25872
movl %eax, %r15d
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x2643c
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
leaq 0x13a35(%rip), %rdx     # 0x39e63
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
cmpl $0x7ffffffe, %r15d      # imm = 0x7FFFFFFE
ja 0x264b6
lzcntl %r14d, %eax
movl $0x20, %ebp
subl %eax, %ebp
leaq 0x7cb2(%rbx), %r14
incl %r15d
leaq 0x144a3(%rip), %r12     # 0x3a905
leaq 0x13796(%rip), %r13     # 0x39bff
movq %rbx, %rdi
movl %ebp, %esi
callq 0x18ad7
movzwl 0x7cb0(%rbx), %ecx
cmpq $0x2541, %rcx           # imm = 0x2541
ja 0x264ae
movq %rcx, %rdi
addq %r14, %rdi
movl $0x2542, %edx           # imm = 0x2542
subl %ecx, %edx
movzwl %dx, %esi
cmpl $0x1, %r15d
movq %r13, %rdx
cmoveq %r12, %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
decl %r15d
testl %r15d, %r15d
jg 0x26469
movq %rbx, %rdi
movl $0x1f, %esi
callq 0x25872
movl %eax, %r15d
incb %r15b
movb %r15b, 0x42(%rsp)
movq %rbx, %rdi
movl $0x1f, %esi
callq 0x25872
movl %eax, %r13d
incb %r13b
movb %r13b, 0x43(%rsp)
movq %rbx, %rdi
callq 0x25dac
movl %eax, 0x10(%rsp)
movb %al, 0x44(%rsp)
movq %rbx, %rdi
movl $0x2, %esi
callq 0x18ad7
movl %eax, 0x18(%rsp)
movb %al, 0x45(%rsp)
movq %rbx, %rdi
movl $0xffffffe6, %esi       # imm = 0xFFFFFFE6
movl $0x19, %edx
callq 0x25dec
movl %eax, %ebp
addb $0x1a, %bpl
movb %bpl, 0x46(%rsp)
movq %rbx, %rdi
movl $0xffffffe6, %esi       # imm = 0xFFFFFFE6
movl $0x19, %edx
callq 0x25dec
movq %rbx, %rdi
movl $0xfffffff4, %esi       # imm = 0xFFFFFFF4
movl $0xc, %edx
callq 0x25dec
movl %eax, %r12d
movb %r12b, 0x47(%rsp)
movb %r12b, 0x4c(%rsp)
movq %rbx, %rdi
callq 0x25dac
movl %eax, 0x24(%rsp)
movb %al, 0x48(%rsp)
movq %rbx, %rdi
callq 0x25dac
movl %eax, %r14d
movb %r14b, 0x49(%rsp)
movq %rbx, %rdi
callq 0x25dac
movq %rax, 0x28(%rsp)
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x2665b
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %esi           # imm = 0x2542
subq %rax, %rsi
movsbl %r15b, %ecx
movsbl %r13b, %r8d
movl 0x10(%rsp), %eax
movsbl %al, %r9d
movsbq %al, %rax
movq %rax, 0x10(%rsp)
movsbl %r14b, %r13d
testb $-0x1, %r13b
leaq 0x1440b(%rip), %rax     # 0x3a9df
leaq 0x1111f(%rip), %r10     # 0x376fa
movq %r10, %r11
cmoveq %rax, %r11
movq 0x28(%rsp), %r15
testl %r15d, %r15d
cmoveq %rax, %r10
movl 0x18(%rsp), %eax
movsbl %al, %edx
movq %rdx, 0x38(%rsp)
movsbq %al, %rax
movq %rax, 0x18(%rsp)
movsbl %bpl, %ebp
movsbl %r12b, %r12d
movsbl 0x24(%rsp), %r14d
leaq 0x13860(%rip), %rdx     # 0x39e78
xorl %eax, %eax
pushq %r10
pushq %r15
pushq %r11
pushq %r13
pushq %r14
pushq %r12
pushq %rbp
leaq 0x18742(%rip), %r11     # 0x3ed70
movq 0x50(%rsp), %r10
pushq (%r11,%r10,8)
pushq 0x78(%rsp)
movq 0x58(%rsp), %r10
pushq (%r11,%r10,8)
callq 0x1070
addq $0x50, %rsp
addw 0x7cb0(%rbx), %ax
movw %ax, 0x7cb0(%rbx)
movq 0x10(%rbx), %rdx
movq 0x18(%rbx), %rsi
movq %rsi, %rcx
shrq $0x3f, %rcx
shlxq %rcx, %rsi, %rsi
blsrq %rdx, %rdi
orq %rsi, %rdi
movq 0x30(%rsp), %r14
jne 0x2669c
tzcntq %rdx, %rdx
movl (%rbx), %esi
subl 0x8(%rbx), %esi
addl %ecx, %edx
leal (%rdx,%rsi,8), %ecx
addl $0xffffff78, %ecx       # imm = 0xFFFFFF78
cmpl $-0x11, %ecx
ja 0x267ab
movq %rbx, %rdi
callq 0x25dac
movl %eax, %ebp
movb %bpl, 0x4a(%rsp)
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x266e8
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
movsbl %bpl, %ecx
leaq 0x138b2(%rip), %rdx     # 0x39f8c
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
movq %rbx, %rdi
callq 0x25dac
movb %al, 0x4b(%rsp)
testb %al, %al
je 0x26752
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x26731
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
leaq 0x13888(%rip), %rdx     # 0x39fab
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
leaq 0x50(%rsp), %rsi
leaq 0xb0(%rsp), %rdx
movsbl %bpl, %ecx
movsbl 0x530(%rbx), %r8d
movq %rbx, %rdi
callq 0x2688b
movq %rbx, %rdi
movl $0xfffffff4, %esi       # imm = 0xFFFFFFF4
movl $0xc, %edx
callq 0x25dec
movl %eax, %ecx
movb %cl, 0x4c(%rsp)
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x267ab
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %esi           # imm = 0x2542
subq %rax, %rsi
movsbl %cl, %ecx
leaq 0x1382c(%rip), %rdx     # 0x39fc2
xorl %eax, %eax
callq 0x1070
addw 0x7cb0(%rbx), %ax
movw %ax, 0x7cb0(%rbx)
movq 0x10(%rbx), %rsi
movq 0x18(%rbx), %rcx
movq %rcx, %rdx
shrq $0x3f, %rdx
shlxq %rdx, %rcx, %rdi
blsrq %rsi, %r8
movl $0x4a, %ecx
orq %rdi, %r8
jne 0x26877
tzcntq %rsi, %rsi
movl (%rbx), %edi
subl 0x8(%rbx), %edi
addl %edx, %esi
leal (%rsi,%rdi,8), %edx
addl $0xffffff78, %edx       # imm = 0xFFFFFF78
cmpl $-0x10, %edx
jb 0x26877
movw $0x0, 0x7cb0(%rbx)
movzwl %ax, %ebp
cmpl $0x2541, %ebp           # imm = 0x2541
ja 0x26818
leaq 0x7cb2(%rbx), %rdi
movq 0x7ca8(%rbx), %rsi
callq *0x7ca0(%rbx)
cmpl $0x2542, %ebp           # imm = 0x2542
setae %al
cmpl $0x2, %r14d
setge %cl
cmpl $0x4, 0xc(%rsp)
setge %dl
orb %cl, %dl
cmpb $0x0, 0x49(%rsp)
setne %cl
orb %dl, %cl
orb %al, %cl
cmpl $0x0, 0x28(%rsp)
setne %al
orb %cl, %al
movl $0x5f, %ecx
jne 0x26877
movslq 0xc(%rsp), %rax
imulq $0x1f0, %rax, %rax      # imm = 0x1F0
leaq (%rbx,%rax), %rdi
addq $0xd50, %rdi            # imm = 0xD50
leaq 0x40(%rsp), %rsi
movl $0x1f0, %edx            # imm = 0x1F0
callq 0x10c0
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq