I was not able to test this. But it looks like the same buffer was processed twice in case of pIn = pOutput. 
I have reordered the block to avoid it. Please double check if it is correct.