<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic BMP to YUV420 to H264 encode problem with odd-sized images in Intel® Integrated Performance Primitives</title>
    <link>https://community.intel.com/t5/Intel-Integrated-Performance/BMP-to-YUV420-to-H264-encode-problem-with-odd-sized-images/m-p/816950#M4385</link>
    <description>I am trying to convert some .bmp into h264 to be streamed. Basically a bitmap bytestream is sent over to be encoded.&lt;DIV&gt;I looked thru the samples/forum and got enough ideas to get started and so far I got some pretty good results.&lt;DIV&gt;The only problem I have right now is bitmaps with non-even width/height is not encoded correctly - in VLC it's just a green screen. With even-numbered width/height images the h264 file is correct.&lt;/DIV&gt;&lt;DIV&gt;I tried passing-in the stride of the bitmap (via C# BitmapData.Stride) but still getting the green image.&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;The C/IPP dll:&lt;/DIV&gt;&lt;DIV&gt;[cpp]void ConvertBGRtoYUV420(Ipp8u* rgbData, Ipp8u* yuvData, int imgWidth, int imgHeight, int stride, int frameNumber)
{
	Ipp8u* yuv[3];
	int yuvStep[3] = {imgWidth, imgWidth/2, imgWidth/2};
	int srcStep = stride;
	IppiSize srcSize = {imgWidth, imgHeight};

	for(int i = 0; i &amp;lt; frameNumber; ++i)
	{
		yuv[0] = yuvData + i * imgWidth * imgHeight * 3/2;
		yuv[1] = yuv[0] + imgWidth * imgHeight;
		yuv[2] = yuv[1] + imgWidth * imgHeight / 4;

		ippiBGRToYCbCr420_8u_C3P3R(rgbData, srcStep, yuv, yuvStep, srcSize);
	}
}

// bmp is in BGR, not RGB!
int EncodeStream(Ipp8u* inData, int imgWidth, int imgHeight, int frameNumber, Ipp8u* encodedData, int&amp;amp; VideoDataSize, int stride) 
{
	Ipp8u *cYUVData = ippsMalloc_8u(200000000);
	ConvertBGRtoYUV420(inData, cYUVData, imgWidth, imgHeight, stride, frameNumber);
	UMC::Status status;
	UMC::MediaData DataOut; 
	UMC::VideoData DataIn;
	int FrameSize;
   
	UMC::H264EncoderParams Params;
	UMC::H264VideoEncoder H264Encoder;

	//Params.key_frame_controls.method=1;
	Params.B_frame_rate = 0;
	Params.key_interval = 1;
	Params.info.clip_info.height = imgHeight;
	Params.info.clip_info.width = imgWidth;
	Params.info.bitrate = 1000000;
	Params.numThreads = 1; 

	// roi = entire img 
	IppiSize roi;
	roi.width = imgWidth;
	roi.height = imgHeight;
	
	if((status = H264Encoder.Init(&amp;amp;Params))!=UMC::UMC_OK)
	  return -1;

   FrameSize = imgWidth * imgHeight * 3/2; 
   DataIn.Init(imgWidth,imgHeight,UMC::YUV420,8);
   DataIn.SetBufferPointer(inData,FrameSize);
   DataIn.SetDataSize(FrameSize);

   DataOut.SetBufferPointer(encodedData, 100000000);

   VideoDataSize=0;
   int nEncodedFrames=0;
   while ( nEncodedFrames &amp;lt; frameNumber)
   {
	    status = H264Encoder.GetFrame(&amp;amp;DataIn, &amp;amp;DataOut);	    
        if (status == UMC::UMC_OK)
        {   
               nEncodedFrames++;
        
               VideoDataSize+=DataOut.GetDataSize();
	           DataOut.MoveDataPointer(DataOut.GetDataSize());
   
               cYUVData += FrameSize;
	           DataIn.SetBufferPointer(cYUVData,FrameSize);
	           DataIn.SetDataSize(FrameSize);             
        }
	}
	return TRUE;
}[/cpp] &lt;/DIV&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt;The call from C#:&lt;/DIV&gt;&lt;DIV&gt;[bash]Image img = new Bitmap(@"C:\test.bmp");
            const string fileName = @"C:\output.h264";
            BinaryWriter bw = new BinaryWriter(File.Open(fileName, FileMode.Create));

            byte[] data;
            byte[] output;

            var bmd = (img as Bitmap).LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly,
                                     PixelFormat.Format24bppRgb);
            // first row of pixels
            IntPtr ptr = bmd.Scan0;

            int bytes = Math.Abs(bmd.Stride)*img.Height;
            data = new byte[bytes];

            // copy rgb values into array
            Marshal.Copy(ptr, data, 0, bytes);
            output = new byte[bytes];

            int size = 0;
            int ret = ConvertBGRtoYUV420(data, img.Width, img.Height, 100, output, ref size, bmd.Stride);
            bw.Write(output);
            bw.Close();[/bash][bash]Any ideas on what I'm doing wrong in handling non-even width/height bitmaps?[/bash]&lt;/DIV&gt;&lt;/DIV&gt;</description>
    <pubDate>Wed, 23 May 2012 09:33:37 GMT</pubDate>
    <dc:creator>cks2k2</dc:creator>
    <dc:date>2012-05-23T09:33:37Z</dc:date>
    <item>
      <title>BMP to YUV420 to H264 encode problem with odd-sized images</title>
      <link>https://community.intel.com/t5/Intel-Integrated-Performance/BMP-to-YUV420-to-H264-encode-problem-with-odd-sized-images/m-p/816950#M4385</link>
      <description>I am trying to convert some .bmp into h264 to be streamed. Basically a bitmap bytestream is sent over to be encoded.&lt;DIV&gt;I looked thru the samples/forum and got enough ideas to get started and so far I got some pretty good results.&lt;DIV&gt;The only problem I have right now is bitmaps with non-even width/height is not encoded correctly - in VLC it's just a green screen. With even-numbered width/height images the h264 file is correct.&lt;/DIV&gt;&lt;DIV&gt;I tried passing-in the stride of the bitmap (via C# BitmapData.Stride) but still getting the green image.&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;The C/IPP dll:&lt;/DIV&gt;&lt;DIV&gt;[cpp]void ConvertBGRtoYUV420(Ipp8u* rgbData, Ipp8u* yuvData, int imgWidth, int imgHeight, int stride, int frameNumber)
{
	Ipp8u* yuv[3];
	int yuvStep[3] = {imgWidth, imgWidth/2, imgWidth/2};
	int srcStep = stride;
	IppiSize srcSize = {imgWidth, imgHeight};

	for(int i = 0; i &amp;lt; frameNumber; ++i)
	{
		yuv[0] = yuvData + i * imgWidth * imgHeight * 3/2;
		yuv[1] = yuv[0] + imgWidth * imgHeight;
		yuv[2] = yuv[1] + imgWidth * imgHeight / 4;

		ippiBGRToYCbCr420_8u_C3P3R(rgbData, srcStep, yuv, yuvStep, srcSize);
	}
}

// bmp is in BGR, not RGB!
int EncodeStream(Ipp8u* inData, int imgWidth, int imgHeight, int frameNumber, Ipp8u* encodedData, int&amp;amp; VideoDataSize, int stride) 
{
	Ipp8u *cYUVData = ippsMalloc_8u(200000000);
	ConvertBGRtoYUV420(inData, cYUVData, imgWidth, imgHeight, stride, frameNumber);
	UMC::Status status;
	UMC::MediaData DataOut; 
	UMC::VideoData DataIn;
	int FrameSize;
   
	UMC::H264EncoderParams Params;
	UMC::H264VideoEncoder H264Encoder;

	//Params.key_frame_controls.method=1;
	Params.B_frame_rate = 0;
	Params.key_interval = 1;
	Params.info.clip_info.height = imgHeight;
	Params.info.clip_info.width = imgWidth;
	Params.info.bitrate = 1000000;
	Params.numThreads = 1; 

	// roi = entire img 
	IppiSize roi;
	roi.width = imgWidth;
	roi.height = imgHeight;
	
	if((status = H264Encoder.Init(&amp;amp;Params))!=UMC::UMC_OK)
	  return -1;

   FrameSize = imgWidth * imgHeight * 3/2; 
   DataIn.Init(imgWidth,imgHeight,UMC::YUV420,8);
   DataIn.SetBufferPointer(inData,FrameSize);
   DataIn.SetDataSize(FrameSize);

   DataOut.SetBufferPointer(encodedData, 100000000);

   VideoDataSize=0;
   int nEncodedFrames=0;
   while ( nEncodedFrames &amp;lt; frameNumber)
   {
	    status = H264Encoder.GetFrame(&amp;amp;DataIn, &amp;amp;DataOut);	    
        if (status == UMC::UMC_OK)
        {   
               nEncodedFrames++;
        
               VideoDataSize+=DataOut.GetDataSize();
	           DataOut.MoveDataPointer(DataOut.GetDataSize());
   
               cYUVData += FrameSize;
	           DataIn.SetBufferPointer(cYUVData,FrameSize);
	           DataIn.SetDataSize(FrameSize);             
        }
	}
	return TRUE;
}[/cpp] &lt;/DIV&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt;The call from C#:&lt;/DIV&gt;&lt;DIV&gt;[bash]Image img = new Bitmap(@"C:\test.bmp");
            const string fileName = @"C:\output.h264";
            BinaryWriter bw = new BinaryWriter(File.Open(fileName, FileMode.Create));

            byte[] data;
            byte[] output;

            var bmd = (img as Bitmap).LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly,
                                     PixelFormat.Format24bppRgb);
            // first row of pixels
            IntPtr ptr = bmd.Scan0;

            int bytes = Math.Abs(bmd.Stride)*img.Height;
            data = new byte[bytes];

            // copy rgb values into array
            Marshal.Copy(ptr, data, 0, bytes);
            output = new byte[bytes];

            int size = 0;
            int ret = ConvertBGRtoYUV420(data, img.Width, img.Height, 100, output, ref size, bmd.Stride);
            bw.Write(output);
            bw.Close();[/bash][bash]Any ideas on what I'm doing wrong in handling non-even width/height bitmaps?[/bash]&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 23 May 2012 09:33:37 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Integrated-Performance/BMP-to-YUV420-to-H264-encode-problem-with-odd-sized-images/m-p/816950#M4385</guid>
      <dc:creator>cks2k2</dc:creator>
      <dc:date>2012-05-23T09:33:37Z</dc:date>
    </item>
    <item>
      <title>BMP to YUV420 to H264 encode problem with odd-sized images</title>
      <link>https://community.intel.com/t5/Intel-Integrated-Performance/BMP-to-YUV420-to-H264-encode-problem-with-odd-sized-images/m-p/816951#M4386</link>
      <description>If I am not mistaken, width and height not divisible by 2 are not supported in H.264. The only thing you can do is crop or upscale.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 25 May 2012 11:14:33 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Integrated-Performance/BMP-to-YUV420-to-H264-encode-problem-with-odd-sized-images/m-p/816951#M4386</guid>
      <dc:creator>levicki</dc:creator>
      <dc:date>2012-05-25T11:14:33Z</dc:date>
    </item>
    <item>
      <title>BMP to YUV420 to H264 encode problem with odd-sized images</title>
      <link>https://community.intel.com/t5/Intel-Integrated-Performance/BMP-to-YUV420-to-H264-encode-problem-with-odd-sized-images/m-p/816952#M4387</link>
      <description>I see. Thanks for the insight.</description>
      <pubDate>Mon, 28 May 2012 07:43:12 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Integrated-Performance/BMP-to-YUV420-to-H264-encode-problem-with-odd-sized-images/m-p/816952#M4387</guid>
      <dc:creator>cks2k2</dc:creator>
      <dc:date>2012-05-28T07:43:12Z</dc:date>
    </item>
  </channel>
</rss>

