Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lozida · Aug 04, 2021 at 08:14 AM · renderingvideoframeratevideo streamingpicture

How to render YUV file properly in Unity?

I'm trying to render a YUV file inside of Unity. I found some code online and I managed to load the file (a 15 -20 second video) and play it, but the framerate is absolutely horrendous. I'm getting about 5 FPS on average. Is there any way for me to increase the framerate to at least about 30, but preferably 60 or above?

The Code:

{

 public RawImage img;

 int frameCount;

 int frameNow = 0;

 byte[] file;

 // Plana continuous storage, Packed interleaved storage
 // This example uses Plana YUV420 format
 void Start()
 {
     file = File.ReadAllBytes("C:/Users/Gigatron/Desktop/video.yuv");

     frameCount = GetFrameCount(file, 1280, 720); // Image width and height

     print("Frame Count : " + frameCount);

     //File.WriteAllBytes("C:/Users/Gigatron/Desktop/video.yuv", t.EncodeToPNG());
 }

 void Update()
 {
     if (frameNow >= frameCount - 1) return;

     img.texture = GetTexture(file, 1280, 720); // Image width and height

     frameNow++;

     print("Frame : " + (frameNow) + " (" + (int)(frameNow / (float)(frameCount - 1) * 100) + "%)");
 }

 int GetFrameCount(byte[] file, int width, int height)
 {
     return file.Length / ((width * height) * 3 / 2);
 }

 Texture2D t = null;

 Texture2D GetTexture(byte[] file, int width, int height)
 {
     if (t != null) Destroy(t);

     t = new Texture2D(width, height, TextureFormat.RGB24, false);

     int Ysize = t.width * t.height;
     int UorVsize = t.width * t.height / 4;

     Color[] cArr = new Color[t.width * t.height];

     byte U = 0;
     byte V = 0;

     int k = 0;

     int offset = frameNow * ((width * height) * 3 / 2);

     for (int y = 0; y < t.height; y += 2)
     {
         for (int x = 0; x < t.width; x += 2)
         {
             U = file[offset + Ysize + k++];
             V = file[offset + Ysize + k + UorVsize];

             int i = y * t.width + x;
             int mY = t.height - 1 - y;

             byte Y00 = file[offset + i];
             byte Y01 = file[offset + i + t.width];
             byte Y10 = file[offset + i + 1];
             byte Y11 = file[offset + i + t.width + 1];

             float R00 = (Y00 + 1.4075f * (V - 128)) / 255f;
             float G00 = (Y00 - 0.3455f * (U - 128) - 0.7169f * (V - 128)) / 255f;
             float B00 = (Y00 + 1.779f * (U - 128)) / 255f;

             float R01 = (Y01 + 1.4075f * (V - 128)) / 255f;
             float G01 = (Y01 - 0.3455f * (U - 128) - 0.7169f * (V - 128)) / 255f;
             float B01 = (Y01 + 1.779f * (U - 128)) / 255f;

             float R10 = (Y10 + 1.4075f * (V - 128)) / 255f;
             float G10 = (Y10 - 0.3455f * (U - 128) - 0.7169f * (V - 128)) / 255f;
             float B10 = (Y10 + 1.779f * (U - 128)) / 255f;

             float R11 = (Y11 + 1.4075f * (V - 128)) / 255f;
             float G11 = (Y11 - 0.3455f * (U - 128) - 0.7169f * (V - 128)) / 255f;
             float B11 = (Y11 + 1.779f * (U - 128)) / 255f;

             t.SetPixel(x, mY, new Color(R00, G00, B00, 1));
             t.SetPixel(x, mY - 1, new Color(R01, G01, B01, 1));
             t.SetPixel(x + 1, mY, new Color(R10, G10, B10, 1));
             t.SetPixel(x + 1, mY - 1, new Color(R11, G11, B11, 1));
         }
     }
     t.Apply();
     return t;
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

144 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

while using Handheld.PlayFullScreenMovie playing url video stream, where it store the stream? 0 Answers

How to play videos in WebGL? 1 Answer

Video Player WebGl Transparency problem - previous frames doesn't clear 1 Answer

How do you Play a different Video track within the VIdeoPlayer? 0 Answers

Android build not running at target frame rate: Semaphore.WaitForSignal 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges