Comments on: roll that camera, zombie: rotation and conversion from YV12 to YUV 420 Planar http://www.wordsaretoys.com/2013/10/25/roll-that-camera-zombie-rotation-and-coversion-from-yv12-to-yuv420planar/ and so on... Fri, 24 Apr 2015 02:43:40 +0000 hourly 1 http://wordpress.org/?v=4.3.1 By: chris http://www.wordsaretoys.com/2013/10/25/roll-that-camera-zombie-rotation-and-coversion-from-yv12-to-yuv420planar/#comment-834 Fri, 24 Apr 2015 02:43:40 +0000 http://www.wordsaretoys.com/?p=543#comment-834 Thanks Zenith! I haven’t worked with this code in ages, but I think you’d have to do something like

int w = width, h = height;
int wo = height, h0 = width; // add this

Then, for every transformation to output, use the (wo, ho) in place of (w, h). Example:

output[wo * yo + xo] = input[wo * yi + xi];

Sorry, I know it’s a little vague. Play around and see what you get. Good luck!

]]>
By: Zenith http://www.wordsaretoys.com/2013/10/25/roll-that-camera-zombie-rotation-and-coversion-from-yv12-to-yuv420planar/#comment-822 Thu, 23 Apr 2015 10:25:49 +0000 http://www.wordsaretoys.com/?p=543#comment-822 Hi Chris,

Thanks for the wonderful blog, very informative and helpful.
The rotateNV21toYUV420 you provided works great but the dimensions of the byte[] are kept intact, I mean byte[] obtained from onPreviewFrame() in portrait mode is 176*144 and after applying the method you provided the dimensions are still 176*144, could you please point to me what changes would be needed to make it 144*176 dimensions.

]]>
By: chris http://www.wordsaretoys.com/2013/10/25/roll-that-camera-zombie-rotation-and-coversion-from-yv12-to-yuv420planar/#comment-323 Thu, 19 Mar 2015 00:59:59 +0000 http://www.wordsaretoys.com/?p=543#comment-323 Hi Hieu,

The output format is IYUV, which is supposed to be compatible with I420. However, see http://www.fourcc.org/yuv.php#IYUV for a discussion of this topic…it isn’t clear if they really are the same. Wish I could tell you more.

Chris

]]>
By: Hieu Phan http://www.wordsaretoys.com/2013/10/25/roll-that-camera-zombie-rotation-and-coversion-from-yv12-to-yuv420planar/#comment-309 Wed, 18 Mar 2015 08:28:20 +0000 http://www.wordsaretoys.com/?p=543#comment-309 Hi, is YUV420 in your post I420? I use output of rotateNV21toYUV420 as I420 but it is incorrect.

]]>
By: chris http://www.wordsaretoys.com/2013/10/25/roll-that-camera-zombie-rotation-and-coversion-from-yv12-to-yuv420planar/#comment-108 Sat, 20 Sep 2014 01:27:05 +0000 http://www.wordsaretoys.com/?p=543#comment-108 Hi Domagoj,

You could try playing with the swap/flip flags. Invert one (flip = !flip) then the other (swap = !swap) and see what you get.

Chris

]]>
By: Domagoj http://www.wordsaretoys.com/2013/10/25/roll-that-camera-zombie-rotation-and-coversion-from-yv12-to-yuv420planar/#comment-107 Fri, 19 Sep 2014 11:54:47 +0000 http://www.wordsaretoys.com/?p=543#comment-107 Hi Chris,

I used Your function to rotate bytes in ‘onPreviewFrame’. And the thing is that the image gets rotated for 90 degrees but is also mirrored and flipped. Do You have any suggestions on how to just get image rotated.

Regards,
Domagoj

]]>
By: chris http://www.wordsaretoys.com/2013/10/25/roll-that-camera-zombie-rotation-and-coversion-from-yv12-to-yuv420planar/#comment-106 Wed, 02 Jul 2014 17:37:40 +0000 http://www.wordsaretoys.com/?p=543#comment-106 Hi TwoForest,

Sorry, my mistake! The NV21 format has the UV planes interleaved, so my original statement was wrong. I’ve just written and tested the following code on an NV21 stream, and it worked fine.

public static void rotateNV21toYUV420(byte[] input, byte[] output, int width, int height, int rotation) {
	boolean swap = (rotation == 90 || rotation == 270);
	boolean flip = (rotation == 90 || rotation == 180);
	for (int x = 0; x < width; x++) {
		for (int y = 0; y < height; y++) {
			int xo = x, yo = y;
			int w = width, h = height;
			int xi = xo, yi = yo;
			if (swap) {
				xi = w * yo / h;
				yi = h * xo / w;
			}
			if (flip) {
				xi = w - xi - 1;
				yi = h - yi - 1;
			}
			output[w * yo + xo] = input[w * yi + xi];
			int fs = w * h;
			int qs = (fs >> 2);
			xi = (xi >> 1);
			yi = (yi >> 1);
			xo = (xo >> 1);
			yo = (yo >> 1);
			w = (w >> 1);
			h = (h >> 1);
			// adjust for interleave here
			int ui = fs + (w * yi + xi) * 2;
			int uo = fs + w * yo + xo;
			// and here
			int vi = ui + 1;
			int vo = qs + uo;
			output[uo] = input[vi]; 
			output[vo] = input[ui]; 
		}
	}
}	

Hope that works for you!

Regards,
Chris

]]>
By: twoforest http://www.wordsaretoys.com/2013/10/25/roll-that-camera-zombie-rotation-and-coversion-from-yv12-to-yuv420planar/#comment-105 Wed, 02 Jul 2014 04:07:07 +0000 http://www.wordsaretoys.com/?p=543#comment-105 Hi chris.

In the NV21,

output[uo] = input[vi];
output[vo] = input[ui];

to read:

output[uo] = input[ui];
output[vo] = input[vi]

not work well.How can I do?

]]>
By: chris http://www.wordsaretoys.com/2013/10/25/roll-that-camera-zombie-rotation-and-coversion-from-yv12-to-yuv420planar/#comment-104 Wed, 16 Apr 2014 14:46:39 +0000 http://www.wordsaretoys.com/?p=543#comment-104 Hi Kirtan,

NV21 should be very close to YV12, except the U and V color planes are swapped. You should be able to get it working just by changing these lines:

output[uo] = input[vi];
output[vo] = input[ui];

to read:

output[uo] = input[ui];
output[vo] = input[vi];

Give that a try.

Regards,
Chris

]]>
By: Kirtan http://www.wordsaretoys.com/2013/10/25/roll-that-camera-zombie-rotation-and-coversion-from-yv12-to-yuv420planar/#comment-103 Wed, 16 Apr 2014 14:38:57 +0000 http://www.wordsaretoys.com/?p=543#comment-103 Hi , I read http://www.wordsaretoys.com/2013/10/25/roll-that-camera-zombie-rotation-and-coversion-from-yv12-to-yuv420planar/

where you rotate NV12 image I want to rotate NV21 image comes from android camera I checked your function but I loose color information of image using that.May be due to NV12 and I pass NV21 to function as input. can you help lil bit in this please ? its very useful function for me.

]]>