Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

RGB to YUV and back using AE SDK

Contributor ,
Feb 18, 2020 Feb 18, 2020

Hi. I saw the sample with YUV support in Premiere. It adds support for YUV codec. I am trying to convert RGB to YUB and back. Ideally I should get the same image. But it does not work I have checked my code, and the formula a few times. Any ideas ?

template <typename InFormat, typename OutFormat>
PF_Err RGB2YUV(void* refCon, A_long threadInd, A_long iterNum, A_long iter_Total) {
	copy_dataP refConP = static_cast<copy_dataP>(refCon);
	InFormat* in_pixel = (InFormat*)((char*)refConP->srcP->data + (iterNum * refConP->srcP->rowbytes));
	OutFormat* out_pixel = (OutFormat*)((char*)refConP->destP->data + (iterNum * refConP->destP->rowbytes));
	OutFormat R, G, B;

	for (int x = 0; x < refConP->destP->width; x++) {
		*out_pixel++ = *in_pixel++; // alpha
		R = *in_pixel++ ; // R
		G = *in_pixel++ ; // G
		B = *in_pixel++ ; // B
		
		*out_pixel++ = 0.257 * R + 0.504 * G + 0.098 * B ;
		*out_pixel++ = -0.148 * R - 0.291 * G + 0.439 * B ;
		*out_pixel++ = 0.439 * R - 0.368 * G - 0.071 * B ;
	}
	return A_Err_NONE;
}

 

template <typename InFormat, typename OutFormat>
PF_Err YUV2RGB(void* refCon, A_long threadInd, A_long iterNum, A_long iter_Total) {
	copy_dataP refConP = static_cast<copy_dataP>(refCon);

	InFormat* in_pixel = (InFormat*)((char*)refConP->srcP->data + (iterNum * refConP->srcP->rowbytes));
	OutFormat* out_pixel = (OutFormat*)((char*)refConP->destP->data + (iterNum * refConP->destP->rowbytes));
	OutFormat Y, U, V;

	for (int x = 0; x < refConP->destP->width; x++) {
		*out_pixel++ = *in_pixel++; // alpha
		Y = *in_pixel++ ; // Y
		U = *in_pixel++ ; // U
		V = *in_pixel++ ; // V

		*out_pixel++ = 1.164 * Y + 1.596 * V;
		*out_pixel++ = 1.164 * Y - 0.392 * U - 0.813 * V;
		*out_pixel++ = 1.164 * Y + 2.017 * U;
	}
	return A_Err_NONE;
}

 

copy_data refCon;
		refCon.srcP = &params[UTILITIES_INPUT]->u.ld;
		refCon.destP = float_world;

		if (deepB) 
			ERR(suites.IterateSuite1()->AEGP_IterateGeneric(output->height, &refCon, RGB2YUV<A_u_short,PF_FpShort>));
		else 
			ERR(suites.IterateSuite1()->AEGP_IterateGeneric(output->height, &refCon, RGB2YUV<A_u_char, PF_FpShort>));

		refCon.srcP = float_world;
		refCon.destP = output;
		if (deepB)
			ERR(suites.IterateSuite1()->AEGP_IterateGeneric(output->height, &refCon, YUV2RGB<PF_FpShort, A_u_short>));
		else
			ERR(suites.IterateSuite1()->AEGP_IterateGeneric(output->height, &refCon, YUV2RGB<PF_FpShort, A_u_char>));

 

TOPICS
SDK
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 18, 2020 Feb 18, 2020

Well, have you actually checked AE's workspace color settings and the footage interpretation? This may already affect the source pixels you feed into your stuff and using specific color profiles and LUTs further down will of course also have an impact. In that regard AE is more complex.

 

Mylenium

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 18, 2020 Feb 18, 2020

The code and formula seems correct, with a quick copy-and-paste test from my side. Of course in 8bpc color space, you will get some precision errors, but these should only amount to +/- 1 value difference per color channel. What exactly "does not work"?

If your plugin does not announce to PPro that it does support YUV, PPro will implicitly do the conversion for you and you only will have to deal with RGB.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 18, 2020 Feb 18, 2020

@Mylenium I am using standard 8-bit with no special interpretation. This should not be an issue as the intermediate storage is a float pixel.

@Toby__reduxFX_ , This code is for AE. And its not about YUV support, I want to write a plugin that does some computations in YUV space. As far as I know, AE does not directly provide a YUV image, nor does it provide a conversion. The closest is YIQ - hence I need to do a simple conversion.

The final image is completely different from the original image. I am using the 4-color gradient for a sample image. After both these operations, I should get the 4-color gradient with perhaps a minor shift due to the integer rounding down. I get a completely different image.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 18, 2020 Feb 18, 2020
LATEST

Hi. Its fixed, by using offsets to bring the color values..Thanks.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines