// File: /workspace/github_c_makefile_star2_O1/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 0x2536c
movl %eax, 0xc(%rsp)
movq %rbx, %rdi
movl $0x1f, %esi
callq 0x2536c
movq %rbx, %rdi
callq 0x2551d
movl %eax, %ebp
movb %bpl, (%r14)
movq %rbx, %rdi
callq 0x2551d
movl %eax, %r15d
movb %r15b, 0x1(%r14)
movq %rbx, %rdi
movl $0x7, %esi
callq 0x2536c
movl %eax, %r14d
leal 0x1(%r14), %r12d
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x258fe
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movswq %cx, %rsi
movl 0xc(%rsp), %ecx
cmpl $0x4, %ecx
leaq 0x14fb5(%rip), %rax     # 0x3a85f
leaq 0x11cd9(%rip), %r10     # 0x3758a
movq %r10, %r8
cmovlq %rax, %r8
movsbl %bpl, %r9d
testb %bpl, %bpl
leaq 0x14377(%rip), %rdx     # 0x39c3d
leaq 0x1436a(%rip), %r11     # 0x39c37
cmoveq %rdx, %r11
movsbl %r15b, %r15d
cmpl $0x2, %r12d
cmovlq %rax, %r10
leaq 0x142c5(%rip), %rdx     # 0x39ba9
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 0x25c12
movq %rbx, %rdi
movl $0x6, %esi
callq 0x2536c
movl %eax, %ebp
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x25958
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movswq %cx, %rsi
leaq 0x142fb(%rip), %rdx     # 0x39c43
movl %ebp, %ecx
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
cmpl $0x2, %ebp
je 0x25a14
testl %ebp, %ebp
jne 0x25ad8
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x259a3
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movswq %cx, %rsi
leaq 0x142ca(%rip), %rdx     # 0x39c5f
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
leaq 0x7cb2(%rbx), %r14
leaq 0x14dd4(%rip), %r15     # 0x3a785
leaq 0x14e27(%rip), %r12     # 0x3a7df
movq 0x30(%rsp), %rax
movl %eax, %ebp
movq %rbx, %rdi
movl $0x21fff, %esi          # imm = 0x21FFF
callq 0x2536c
movzwl 0x7cb0(%rbx), %ecx
cmpq $0x2541, %rcx           # imm = 0x2541
ja 0x25a09
incl %eax
movq %rcx, %rdi
addq %r14, %rdi
movl $0x2542, %edx           # imm = 0x2542
subl %ecx, %edx
movswq %dx, %rsi
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 0x259bf
jmp 0x25c12
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x25a4e
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movswq %cx, %rsi
leaq 0x14230(%rip), %rdx     # 0x39c70
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
testl %r14d, %r14d
jle 0x25c12
leaq 0x7cb2(%rbx), %r15
incl %r14d
leaq 0x1422c(%rip), %r12     # 0x39c94
leaq 0x1421e(%rip), %r13     # 0x39c8d
movq %rbx, %rdi
movl $0x22000, %esi          # imm = 0x22000
callq 0x2536c
movl %eax, %ebp
movq %rbx, %rdi
movl $0x22000, %esi          # imm = 0x22000
callq 0x2536c
movzwl 0x7cb0(%rbx), %ecx
cmpq $0x2541, %rcx           # imm = 0x2541
ja 0x25aca
movq %rcx, %rdi
addq %r15, %rdi
movl $0x2542, %edx           # imm = 0x2542
subl %ecx, %edx
movswq %dx, %rsi
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
jg 0x25a6f
jmp 0x25c12
movl $0x38, %eax
btl %ebp, %eax
jae 0x25b44
movq %rbx, %rdi
callq 0x2551d
movl %eax, %ebp
movq %rbx, %rdi
movl $0x21fff, %esi          # imm = 0x21FFF
callq 0x2536c
movzwl 0x7cb0(%rbx), %ecx
cmpq $0x2541, %rcx           # imm = 0x2541
ja 0x25c12
incl %eax
leaq (%rbx,%rcx), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %edx           # imm = 0x2542
subl %ecx, %edx
movswq %dx, %rsi
leaq 0x14170(%rip), %rdx     # 0x39c9c
movl %ebp, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
jmp 0x25c12
cmpl $0x6, %ebp
jne 0x25c12
movq %rbx, %rdi
movl $0x21fff, %esi          # imm = 0x21FFF
callq 0x2536c
movl %eax, %r15d
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x25b97
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movswq %cx, %rsi
leaq 0x1415a(%rip), %rdx     # 0x39ce3
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
cmpl $0x7ffffffe, %r15d      # imm = 0x7FFFFFFE
ja 0x25c12
lzcntl %r14d, %eax
movl $0x20, %ebp
subl %eax, %ebp
leaq 0x7cb2(%rbx), %r14
incl %r15d
leaq 0x14bc8(%rip), %r12     # 0x3a785
leaq 0x13ebb(%rip), %r13     # 0x39a7f
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1875d
movzwl 0x7cb0(%rbx), %ecx
cmpq $0x2541, %rcx           # imm = 0x2541
ja 0x25c0a
movq %rcx, %rdi
addq %r14, %rdi
movl $0x2542, %edx           # imm = 0x2542
subl %ecx, %edx
movswq %dx, %rsi
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 0x25bc4
movq %rbx, %rdi
movl $0x1f, %esi
callq 0x2536c
movl %eax, %r15d
incb %r15b
movb %r15b, 0x42(%rsp)
movq %rbx, %rdi
movl $0x1f, %esi
callq 0x2536c
movl %eax, %r13d
incb %r13b
movb %r13b, 0x43(%rsp)
movq %rbx, %rdi
callq 0x2551d
movl %eax, 0x10(%rsp)
movb %al, 0x44(%rsp)
movq %rbx, %rdi
movl $0x2, %esi
callq 0x1875d
movl %eax, 0x18(%rsp)
movb %al, 0x45(%rsp)
movq %rbx, %rdi
movl $0xffffffe6, %esi       # imm = 0xFFFFFFE6
movl $0x19, %edx
callq 0x2555d
movl %eax, %ebp
addb $0x1a, %bpl
movb %bpl, 0x46(%rsp)
movq %rbx, %rdi
movl $0xffffffe6, %esi       # imm = 0xFFFFFFE6
movl $0x19, %edx
callq 0x2555d
movq %rbx, %rdi
movl $0xfffffff4, %esi       # imm = 0xFFFFFFF4
movl $0xc, %edx
callq 0x2555d
movl %eax, %r12d
movb %r12b, 0x47(%rsp)
movb %r12b, 0x4c(%rsp)
movq %rbx, %rdi
callq 0x2551d
movl %eax, 0x24(%rsp)
movb %al, 0x48(%rsp)
movq %rbx, %rdi
callq 0x2551d
movl %eax, %r14d
movb %r14b, 0x49(%rsp)
movq %rbx, %rdi
callq 0x2551d
movq %rax, 0x28(%rsp)
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x25db3
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movswq %cx, %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 0x14b2c(%rip), %rax     # 0x3a85f
leaq 0x11850(%rip), %r10     # 0x3758a
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 0x13f81(%rip), %rdx     # 0x39cf8
xorl %eax, %eax
pushq %r10
pushq %r15
pushq %r11
pushq %r13
pushq %r14
pushq %r12
pushq %rbp
leaq 0x18fe3(%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 %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
movq 0x30(%rsp), %r14
jne 0x25df3
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 0x25eff
movq %rbx, %rdi
callq 0x2551d
movl %eax, %ebp
movb %bpl, 0x4a(%rsp)
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x25e40
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movswq %cx, %rsi
movsbl %bpl, %ecx
leaq 0x13fda(%rip), %rdx     # 0x39e0c
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
movq %rbx, %rdi
callq 0x2551d
movb %al, 0x4b(%rsp)
testb %al, %al
je 0x25eab
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x25e8a
leaq (%rbx,%rax), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movswq %cx, %rsi
leaq 0x13faf(%rip), %rdx     # 0x39e2b
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 0x25fe3
movq %rbx, %rdi
movl $0xfffffff4, %esi       # imm = 0xFFFFFFF4
movl $0xc, %edx
callq 0x2555d
movb %al, 0x4c(%rsp)
movzwl 0x7cb0(%rbx), %ecx
cmpq $0x2541, %rcx           # imm = 0x2541
ja 0x25eff
leaq (%rbx,%rcx), %rdi
addq $0x7cb2, %rdi           # imm = 0x7CB2
movl $0x2542, %edx           # imm = 0x2542
subl %ecx, %edx
movswq %dx, %rsi
movsbl %al, %ecx
leaq 0x13f51(%rip), %rdx     # 0x39e42
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
movl $0x4a, %eax
orq %rsi, %rdi
jne 0x25fd1
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 0x25fd1
movzwl 0x7cb0(%rbx), %ebp
movw $0x0, 0x7cb0(%rbx)
cmpl $0x2541, %ebp           # imm = 0x2541
ja 0x25f70
leaq 0x7cb2(%rbx), %rdi
movq 0x7ca8(%rbx), %rsi
callq *0x7ca0(%rbx)
movzwl %bp, %eax
cmpl $0x2542, %eax           # 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, %eax
jne 0x25fd1
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 %eax, %eax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq