// File: 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
subq $0x550, %rsp            # imm = 0x550
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x530(%rbp)
leaq -0x278(%rbp), %rdi
xorl %esi, %esi
movl $0x58, %edx
callq 0x20b0
movq -0x530(%rbp), %rax
movl $0xa, -0x274(%rbp)
movl $0x1, -0x23c(%rbp)
leaq -0x220(%rbp), %rcx
movq %rcx, -0x238(%rbp)
movl $0x2, -0x230(%rbp)
movl 0x8(%rax), %edi
movl $0xc0585611, %esi       # imm = 0xC0585611
leaq -0x278(%rbp), %rdx
movb $0x0, %al
callq 0x2120
movl %eax, -0x27c(%rbp)
cmpl $0x0, -0x27c(%rbp)
je 0x2e0d
leaq 0x46ea(%rip), %rdi      # 0x74ee
movb $0x0, %al
callq 0x2040
jmp 0x2e30
movq -0x530(%rbp), %rdi
addq $0x58, %rdi
movl -0x278(%rbp), %eax
movl %eax, -0x280(%rbp)
leaq -0x280(%rbp), %rsi
callq 0x6210
movq -0x530(%rbp), %rdi
addq $0x58, %rdi
callq 0x6240
testb $0x1, %al
jne 0x2e46
jmp 0x2e5d
leaq 0x46d7(%rip), %rdi      # 0x7524
movb $0x0, %al
callq 0x2040
movb $0x0, -0x19(%rbp)
jmp 0x3164
movq -0x530(%rbp), %rdi
movb $0x1, -0x19(%rbp)
addq $0x58, %rdi
callq 0x6260
movq -0x530(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x284(%rbp)
addq $0x58, %rdi
callq 0x6280
leaq -0x4e8(%rbp), %rdi
xorl %esi, %esi
movl $0x58, %edx
callq 0x20b0
movq -0x530(%rbp), %rax
movl -0x274(%rbp), %ecx
movl %ecx, -0x4e4(%rbp)
movl $0x1, -0x4ac(%rbp)
movl -0x284(%rbp), %ecx
movl %ecx, -0x4e8(%rbp)
leaq -0x490(%rbp), %rcx
movq %rcx, -0x4a8(%rbp)
movl $0x2, -0x4a0(%rbp)
movl 0x8(%rax), %edi
movl $0xc0585609, %esi       # imm = 0xC0585609
leaq -0x4e8(%rbp), %rdx
movb $0x0, %al
callq 0x2120
movl %eax, -0x4ec(%rbp)
cmpl $0x0, -0x4ec(%rbp)
jge 0x2f64
movl $0x8, %edi
callq 0x2090
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x538(%rbp)
leaq 0x450c(%rip), %rsi      # 0x742c
callq 0x28c0
jmp 0x2f27
movq -0x538(%rbp), %rdi
leaq 0x7e3b(%rip), %rsi      # 0xad70
leaq -0x61c(%rip), %rdx      # 0x2920
callq 0x21b0
movq -0x538(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4f8(%rbp)
movl %eax, -0x4fc(%rbp)
callq 0x20c0
jmp 0x3172
movq -0x530(%rbp), %rdi
addq $0x20, %rdi
movl -0x4e8(%rbp), %eax
movl %eax, %esi
callq 0x62a0
movq -0x530(%rbp), %rcx
movq (%rax), %rdx
movq %rdx, -0x520(%rbp)
movq 0x8(%rax), %rdx
movq %rdx, -0x518(%rbp)
movq 0x10(%rax), %rdx
movq %rdx, -0x510(%rbp)
movq 0x18(%rax), %rax
movq %rax, -0x508(%rbp)
movq -0x520(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl 0xc(%rcx), %eax
imull 0x10(%rcx), %eax
movslq %eax, %rdx
callq 0x20f0
movq -0x530(%rbp), %rcx
movl 0xc(%rcx), %edx
imull 0x10(%rcx), %edx
movq -0x4a8(%rbp), %rax
movl %edx, (%rax)
movq -0x510(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl 0xc(%rcx), %eax
imull 0x10(%rcx), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movslq %eax, %rdx
callq 0x20f0
movq -0x530(%rbp), %rcx
movl 0xc(%rcx), %eax
imull 0x10(%rcx), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %edx
movq -0x530(%rbp), %rax
movq -0x4a8(%rbp), %rcx
movl %edx, 0x40(%rcx)
movl 0x8(%rax), %edi
movl $0xc058560f, %esi       # imm = 0xC058560F
leaq -0x4e8(%rbp), %rdx
movb $0x0, %al
callq 0x2120
movl %eax, -0x27c(%rbp)
cmpl $0x0, -0x27c(%rbp)
je 0x30b1
movl $0x8, %edi
callq 0x2090
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x540(%rbp)
leaq 0x446d(%rip), %rsi      # 0x74da
callq 0x28c0
jmp 0x3074
movq -0x540(%rbp), %rdi
leaq 0x7cee(%rip), %rsi      # 0xad70
leaq -0x769(%rip), %rdx      # 0x2920
callq 0x21b0
movq -0x540(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4f8(%rbp)
movl %eax, -0x4fc(%rbp)
callq 0x20c0
jmp 0x3172
movq -0x530(%rbp), %rax
testb $0x1, 0x50(%rax)
jne 0x3162
movq -0x530(%rbp), %rax
movl $0xa, -0x524(%rbp)
movl 0x8(%rax), %edi
movl $0x40045612, %esi       # imm = 0x40045612
leaq -0x524(%rbp), %rdx
movb $0x0, %al
callq 0x2120
movl %eax, -0x27c(%rbp)
cmpl $0x0, -0x27c(%rbp)
je 0x3157
movl $0x8, %edi
callq 0x2090
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x548(%rbp)
leaq 0x4181(%rip), %rsi      # 0x7297
callq 0x28c0
jmp 0x311d
movq -0x548(%rbp), %rdi
leaq 0x7c45(%rip), %rsi      # 0xad70
leaq -0x812(%rip), %rdx      # 0x2920
callq 0x21b0
movq -0x548(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4f8(%rbp)
movl %eax, -0x4fc(%rbp)
callq 0x20c0
jmp 0x3172
movq -0x530(%rbp), %rax
movb $0x1, 0x50(%rax)
jmp 0x3164
movb -0x19(%rbp), %al
andb $0x1, %al
addq $0x550, %rsp            # imm = 0x550
popq %rbp
retq
movq -0x4f8(%rbp), %rdi
callq 0x21c0
nop