// File: /workspace/github_cpp_makefile_star2_O2/OtherCrashOverride[P]xu4enc/./M2M.h
// Function: M2M::EncodeNV12(unsigned char const*, unsigned char const*)
bool EncodeNV12(const unsigned char* y, const unsigned char* uv)
	{
		bool result;

		// Reclaim any free input buffers
		v4l2_plane planes[VIDEO_MAX_PLANES];

		v4l2_buffer dqbuf = { 0 };
		dqbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
		dqbuf.memory = V4L2_MEMORY_MMAP;
		dqbuf.m.planes = planes;
		dqbuf.length = 2;

		int ret = ioctl(mfc_fd, VIDIOC_DQBUF, &dqbuf);
		if (ret != 0)
		{
			printf("Waiting on V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE buffer.\n");
		}
		else
		{
			freeInputBuffers.push(dqbuf.index);
		}
		

		// Encode
		if (freeInputBuffers.empty())
		{
			printf("no free buffers.\n");
			result = false;
		}
		else
		{
			result = true;
			
			int index = freeInputBuffers.front();
			freeInputBuffers.pop();

			v4l2_plane planes[VIDEO_MAX_PLANES];

			v4l2_buffer buffer = { 0 };
			buffer.type = dqbuf.type;
			buffer.memory = V4L2_MEMORY_MMAP;
			buffer.index = index;
			buffer.m.planes = planes;
			buffer.length = 2;

			int io = ioctl(mfc_fd, VIDIOC_QUERYBUF, &buffer);
			if (io < 0)
			{
				throw Exception("VIDIOC_QUERYBUF failed.");
			}

			// TODO validate buffer size

			// Copy data
			BufferMapping mapping = inputBuffers[buffer.index];
			
			memcpy(mapping.Start0, y, width * height);
			buffer.m.planes[0].bytesused = width * height;

			memcpy(mapping.Start1, uv, width * height / 2);
			buffer.m.planes[1].bytesused = width * height / 2;
			

			// Re-queue buffer
			ret = ioctl(mfc_fd, VIDIOC_QBUF, &buffer);
			if (ret != 0)
			{
				throw Exception("VIDIOC_QBUF failed.");
			}


			if (!streamActive)
			{
				// Start input stream
				int val = (int)V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;

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

		return result;
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4d8, %rsp            # imm = 0x4D8
movq %rdx, %r13
movq %rsi, -0x40(%rbp)
movq %rdi, %rbx
movl $0xc0585609, %r14d      # imm = 0xC0585609
xorps %xmm0, %xmm0
leaq -0xa0(%rbp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x30(%rdx)
movaps %xmm0, 0x40(%rdx)
movaps %xmm0, 0x20(%rdx)
andq $0x0, 0x50(%rdx)
movaps %xmm0, 0x10(%rdx)
movl $0xa, 0x4(%rdx)
movl $0x1, 0x3c(%rdx)
leaq -0x500(%rbp), %rax
movq %rax, 0x40(%rdx)
movl $0x2, 0x48(%rdx)
movl 0x8(%rdi), %edi
leaq 0x8(%r14), %rsi
xorl %eax, %eax
callq 0x2110
testl %eax, %eax
je 0x296e
leaq 0x1b50(%rip), %rdi      # 0x44b7
callq 0x2160
jmp 0x2986
leaq 0x58(%rbx), %rdi
movl -0xa0(%rbp), %eax
leaq -0x300(%rbp), %rsi
movl %eax, (%rsi)
callq 0x3d00
movq 0x68(%rbx), %r12
movq 0x88(%rbx), %r15
cmpq %r12, %r15
je 0x2aba
movq %r13, -0x38(%rbp)
movq %r15, -0x30(%rbp)
leaq 0x58(%rbx), %rdi
movl (%r12), %r15d
callq 0x3da4
xorps %xmm0, %xmm0
leaq -0xf8(%rbp), %rdx
movups %xmm0, 0x38(%rdx)
movups %xmm0, 0x48(%rdx)
movups %xmm0, 0x28(%rdx)
movups %xmm0, 0x18(%rdx)
movups %xmm0, 0x8(%rdx)
movl -0x9c(%rbp), %eax
movl %eax, 0x4(%rdx)
movl $0x1, 0x3c(%rdx)
movl %r15d, (%rdx)
leaq -0x300(%rbp), %rax
movq %rax, 0x40(%rdx)
movl $0x2, 0x48(%rdx)
movl 0x8(%rbx), %edi
movl $0xc0585609, %esi       # imm = 0xC0585609
xorl %eax, %eax
callq 0x2110
testl %eax, %eax
js 0x2ade
leaq -0xf8(%rbp), %rcx
movl (%rcx), %eax
movq %rcx, %r13
movq 0x20(%rbx), %rcx
shlq $0x5, %rax
movq (%rcx,%rax), %rdi
movq 0x10(%rcx,%rax), %r15
movslq 0xc(%rbx), %rax
movslq 0x10(%rbx), %rdx
imulq %rax, %rdx
movq -0x40(%rbp), %rsi
callq 0x20e0
movl 0x10(%rbx), %eax
imull 0xc(%rbx), %eax
movq 0x40(%r13), %rcx
movl %eax, (%rcx)
pushq $0x2
popq %r13
cltd
idivl %r13d
movslq %eax, %rdx
movq %r15, %rdi
movq -0x38(%rbp), %rsi
callq 0x20e0
movl 0x10(%rbx), %eax
imull 0xc(%rbx), %eax
cltd
idivl %r13d
leaq -0xf8(%rbp), %rdx
movq 0x40(%rdx), %rcx
movl %eax, 0x40(%rcx)
movl 0x8(%rbx), %edi
addq $0x6, %r14
movq %r14, %rsi
xorl %eax, %eax
callq 0x2110
testl %eax, %eax
jne 0x2af2
cmpb $0x0, 0x50(%rbx)
movq -0x30(%rbp), %r15
jne 0x2ac6
leaq -0x44(%rbp), %rdx
movl $0xa, (%rdx)
movl 0x8(%rbx), %edi
movl $0x40045612, %esi       # imm = 0x40045612
xorl %eax, %eax
callq 0x2110
testl %eax, %eax
jne 0x2b06
movb $0x1, 0x50(%rbx)
jmp 0x2ac6
leaq 0x1a2b(%rip), %rdi      # 0x44ec
callq 0x2160
cmpq %r12, %r15
setne %al
addq $0x4d8, %rsp            # imm = 0x4D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x8
popq %rdi
callq 0x2090
movq %rax, %rbx
leaq 0x18ec(%rip), %rsi      # 0x43dc
jmp 0x2b18
pushq $0x8
popq %rdi
callq 0x2090
movq %rax, %rbx
leaq 0x1986(%rip), %rsi      # 0x448a
jmp 0x2b18
pushq $0x8
popq %rdi
callq 0x2090
movq %rax, %rbx
leaq 0x172f(%rip), %rsi      # 0x4247
movq %rax, %rdi
callq 0x264e
leaq 0x4249(%rip), %rsi      # 0x6d70
movq 0x44ba(%rip), %rdx      # 0x6fe8
movq %rbx, %rdi
callq 0x21b0