// File: OtherCrashOverride[P]xu4enc/./M2M.h
// Function: M2M::M2M(int, int, int, int, int)
M2M(int width, int height, int fps, int bitrate, int gop)
		: width(width), height(height), fps(fps), bitrate(bitrate), gop(gop)
	{
		// O_NONBLOCK prevents deque operations from blocking if no buffers are ready
		mfc_fd = open(decoderName, O_RDWR | O_NONBLOCK, 0);
		if (mfc_fd < 0)
		{
			throw Exception("Failed to open MFC");
		}


		// Check device capabilities
		v4l2_capability cap = { 0 };

		int ret = ioctl(mfc_fd, VIDIOC_QUERYCAP, &cap);
		if (ret != 0)
		{
			throw Exception("VIDIOC_QUERYCAP failed.");
		}

		if ((cap.capabilities & V4L2_CAP_VIDEO_M2M_MPLANE) == 0 ||
			(cap.capabilities & V4L2_CAP_STREAMING) == 0)
		{
			printf("V4L2_CAP_VIDEO_M2M_MPLANE=%d\n", (cap.capabilities & V4L2_CAP_VIDEO_M2M_MPLANE) != 0);
			printf("V4L2_CAP_STREAMING=%d\n", (cap.capabilities & V4L2_CAP_STREAMING) != 0);

			throw Exception("Insufficient capabilities of MFC device.");
		}


		fprintf(stderr, "V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE formats:\n");
		EnumFormats(V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
		
		fprintf(stderr, "-------------------------------------------\n");

		fprintf(stderr, "V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE formats:\n");
		EnumFormats(V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);


#if 0	// only V4L2_CID_MIN_BUFFERS_FOR_OUTPUT is supported for MFC

		// Get the number of buffers required
		v4l2_control ctrl = { 0 };
		ctrl.id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT;

		ret = ioctl(mfc_fd, VIDIOC_G_CTRL, &ctrl);
		if (ret != 0)
		{
			fprintf(stderr, "V4L2_CID_MIN_BUFFERS_FOR_OUTPUT VIDIOC_G_CTRL failed.\n");
		}
		else
		{
			fprintf(stderr, "V4L2_CID_MIN_BUFFERS_FOR_OUTPUT=%d\n", ctrl.value);
		}

		ctrl = { 0 };
		ctrl.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE;

		ret = ioctl(mfc_fd, VIDIOC_G_CTRL, &ctrl);
		if (ret != 0)
		{
			fprintf(stderr, "V4L2_CID_MIN_BUFFERS_FOR_CAPTURE VIDIOC_G_CTRL failed.\n");
		}
		else
		{
			fprintf(stderr, "V4L2_CID_MIN_BUFFERS_FOR_CAPTURE=%d\n", ctrl.value);
		}

#endif

		ApplyInputSettings();
		CreateInputBuffers();

		ApplyOutputSettings();
		CreateOutputBuffers();

		// Start output stream
		int val = (int)V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;

		ret = ioctl(mfc_fd, VIDIOC_STREAMON, &val);
		if (ret != 0)
		{
			throw Exception("VIDIOC_STREAMON failed.");
		}
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movl %r8d, -0x18(%rbp)
movl %r9d, -0x1c(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xb0(%rbp)
leaq 0x4809(%rip), %rax      # 0x717b
movq %rax, (%rdi)
movl -0xc(%rbp), %eax
movl %eax, 0xc(%rdi)
movl -0x10(%rbp), %eax
movl %eax, 0x10(%rdi)
movl -0x14(%rbp), %eax
movl %eax, 0x14(%rdi)
movl -0x18(%rbp), %eax
movl %eax, 0x18(%rdi)
movl -0x1c(%rbp), %eax
movl %eax, 0x1c(%rdi)
addq $0x20, %rdi
movq %rdi, -0xc0(%rbp)
callq 0x34d0
movq -0xb0(%rbp), %rdi
addq $0x38, %rdi
movq %rdi, -0xb8(%rbp)
callq 0x34d0
movq -0xb0(%rbp), %rdi
movb $0x0, 0x50(%rdi)
addq $0x58, %rdi
movq %rdi, -0xa8(%rbp)
callq 0x34f0
jmp 0x29d7
movq -0xb0(%rbp), %rax
movq (%rax), %rdi
xorl %edx, %edx
movb %dl, %al
movl $0x802, %esi            # imm = 0x802
callq 0x20d0
movl %eax, -0xc4(%rbp)
jmp 0x29f7
movq -0xb0(%rbp), %rax
movl -0xc4(%rbp), %ecx
movl %ecx, 0x8(%rax)
cmpl $0x0, 0x8(%rax)
jge 0x2a94
movl $0x8, %edi
callq 0x2090
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xd0(%rbp)
leaq 0x4759(%rip), %rsi      # 0x7188
callq 0x28c0
jmp 0x2a36
movq -0xd0(%rbp), %rdi
leaq 0x832c(%rip), %rsi      # 0xad70
leaq -0x12b(%rip), %rdx      # 0x2920
callq 0x21b0
jmp 0x2d63
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x2d42
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x2d36
movq -0xd0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x20c0
jmp 0x2d36
leaq -0x94(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x20b0
movq -0xb0(%rbp), %rax
movl 0x8(%rax), %edi
movl $0x80685600, %esi       # imm = 0x80685600
leaq -0x94(%rbp), %rdx
movb $0x0, %al
callq 0x2120
movl %eax, -0x98(%rbp)
cmpl $0x0, -0x98(%rbp)
je 0x2b34
movl $0x8, %edi
callq 0x2090
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xd8(%rbp)
leaq 0x46aa(%rip), %rsi      # 0x719b
callq 0x28c0
jmp 0x2af8
movq -0xd8(%rbp), %rdi
leaq 0x826a(%rip), %rsi      # 0xad70
leaq -0x1ed(%rip), %rdx      # 0x2920
callq 0x21b0
jmp 0x2d63
movq -0xd8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x20c0
jmp 0x2d36
movl -0x40(%rbp), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x0, %eax
je 0x2b52
movl -0x40(%rbp), %eax
andl $0x4000000, %eax        # imm = 0x4000000
cmpl $0x0, %eax
jne 0x2be5
movl -0x40(%rbp), %esi
shrl $0xe, %esi
andl $0x1, %esi
leaq 0x4651(%rip), %rdi      # 0x71b3
xorl %eax, %eax
callq 0x2040
jmp 0x2b6b
movl -0x40(%rbp), %esi
shrl $0x1a, %esi
andl $0x1, %esi
leaq 0x4656(%rip), %rdi      # 0x71d1
xorl %eax, %eax
callq 0x2040
jmp 0x2b84
movl $0x8, %edi
callq 0x2090
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xe0(%rbp)
leaq 0x4646(%rip), %rsi      # 0x71e8
callq 0x28c0
jmp 0x2ba9
movq -0xe0(%rbp), %rdi
leaq 0x81b9(%rip), %rsi      # 0xad70
leaq -0x29e(%rip), %rdx      # 0x2920
callq 0x21b0
jmp 0x2d63
movq -0xe0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x20c0
jmp 0x2d36
movq 0x83f4(%rip), %rax      # 0xafe0
movq (%rax), %rdi
leaq 0x461b(%rip), %rsi      # 0x7211
xorl %eax, %eax
callq 0x2130
movq -0xb0(%rbp), %rdi
movl $0xa, %esi
callq 0x3520
jmp 0x2c10
movq 0x83c9(%rip), %rax      # 0xafe0
movq %rax, -0xf0(%rbp)
movq (%rax), %rdi
leaq 0x4615(%rip), %rsi      # 0x723d
xorl %eax, %eax
movb %al, -0xe1(%rbp)
callq 0x2130
movq -0xf0(%rbp), %rcx
movb -0xe1(%rbp), %al
movq (%rcx), %rdi
leaq 0x461e(%rip), %rsi      # 0x726a
callq 0x2130
movq -0xb0(%rbp), %rdi
movl $0x9, %esi
callq 0x3520
jmp 0x2c64
movq -0xb0(%rbp), %rdi
callq 0x36d0
jmp 0x2c72
movq -0xb0(%rbp), %rdi
callq 0x3900
jmp 0x2c80
movq -0xb0(%rbp), %rdi
callq 0x3ce0
jmp 0x2c8e
movq -0xb0(%rbp), %rdi
callq 0x3e10
jmp 0x2c9c
movq -0xb0(%rbp), %rax
movl $0x9, -0x9c(%rbp)
movl 0x8(%rax), %edi
movl $0x40045612, %esi       # imm = 0x40045612
leaq -0x9c(%rbp), %rdx
movb $0x0, %al
callq 0x2120
movl %eax, -0x98(%rbp)
cmpl $0x0, -0x98(%rbp)
je 0x2d2d
movl $0x8, %edi
callq 0x2090
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xf8(%rbp)
leaq 0x45a7(%rip), %rsi      # 0x7297
callq 0x28c0
jmp 0x2cf7
movq -0xf8(%rbp), %rdi
leaq 0x806b(%rip), %rsi      # 0xad70
leaq -0x3ec(%rip), %rdx      # 0x2920
callq 0x21b0
jmp 0x2d63
movq -0xf8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x20c0
jmp 0x2d36
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0xa8(%rbp), %rdi
callq 0x41b0
movq -0xb8(%rbp), %rdi
callq 0x41d0
movq -0xc0(%rbp), %rdi
callq 0x41d0
movq -0x28(%rbp), %rdi
callq 0x21c0
nopw %cs:(%rax,%rax)
nopl (%rax)