// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
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)
movaps %xmm0, 0x10(%rdx)
movq $0x0, 0x50(%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 0x3071
leaq 0x244d(%rip), %rdi      # 0x54b7
callq 0x2160
jmp 0x30b3
movl -0xa0(%rbp), %ecx
movl %ecx, -0x300(%rbp)
movq 0x88(%rbx), %rax
movq 0x98(%rbx), %rdx
addq $-0x4, %rdx
cmpq %rdx, %rax
je 0x30a3
movl %ecx, (%rax)
addq $0x4, %rax
movq %rax, 0x88(%rbx)
jmp 0x30b3
leaq 0x58(%rbx), %rdi
leaq -0x300(%rbp), %rsi
callq 0x4034
movq 0x68(%rbx), %r12
movq 0x88(%rbx), %r13
cmpq %r12, %r13
je 0x31ef
movq %r13, -0x30(%rbp)
movq %r15, -0x38(%rbp)
leaq 0x58(%rbx), %rdi
movl (%r12), %r15d
callq 0x40fc
xorps %xmm0, %xmm0
leaq -0x100(%rbp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x30(%rdx)
movaps %xmm0, 0x40(%rdx)
movaps %xmm0, 0x20(%rdx)
movaps %xmm0, 0x10(%rdx)
movq $0x0, 0x50(%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 0x3213
leaq -0x100(%rbp), %r13
movl (%r13), %eax
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)
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movslq %ecx, %rdx
movq %r15, %rdi
movq -0x38(%rbp), %rsi
callq 0x20e0
movl 0x10(%rbx), %eax
imull 0xc(%rbx), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movq 0x40(%r13), %rax
movl %ecx, 0x40(%rax)
movl 0x8(%rbx), %edi
addq $0x6, %r14
movq %r14, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2110
testl %eax, %eax
jne 0x3218
cmpb $0x0, 0x50(%rbx)
movq -0x30(%rbp), %r13
jne 0x31fb
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 0x321d
movb $0x1, 0x50(%rbx)
jmp 0x31fb
leaq 0x22f6(%rip), %rdi      # 0x54ec
callq 0x2160
cmpq %r12, %r13
setne %al
addq $0x4d8, %rsp            # imm = 0x4D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x24a1
callq 0x2435
callq 0x246b