Android uses surface to directly display yuv data

Source: Internet
Author: User

After studying the surface system of Android for a while, I persistently think that all the pictures displayed on the surface or screen must be converted to RGB for display, yuv data can be displayed only when the color space is converted to RGB. Recently, when studying stagefright video display, we found that we could not find how the yuv decoded by omx was converted to RGB code, and yuv data could not be found after render, the screen is indeed displayed, which subverts the truth that yuv must be converted to RGB to display.

Taking a look at the AsomePlayer code, it is not difficult to find that each frame of the video is rendered and displayed by calling SoftwareRenderer. I also try to use SoftwareRenderer to directly display render yuv data, this is a big breakthrough. For example, the yuv data collected by the camera in the future can be directly lost to the surface display, without the need to convert the time-consuming yuv to RGB.

The code is original and posted to share with you: Android 4.4 platform (here, you can download the yuv data address and click here to open the link and put it in the/mnt/sdcard directory)

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           
             # Include
            
              Using namespace android; bool getYV12Data (const char * path, unsigned char * pYUVData, int size) {FILE * fp = fopen (path, "rb "); if (fp = NULL) {printf ("read % s fail !!!!!!!!!!!!!!!!!!! \ N ", path); return false;} fread (pYUVData, size, 1, fp); fclose (fp); return true;} int main (void) {// set up the thread-pool sp
             
               Proc (ProcessState: self (); ProcessState: self ()-> startThreadPool (); // create a client to surfaceflinger sp
              
                Client = new SurfaceComposerClient (); sp
               
                 Dtoken (SurfaceComposerClient: getBuiltInDisplay (metadata: eDisplayIdMain); DisplayInfo dinfo; // gets the screen width and height information status_t status = SurfaceComposerClient: getDisplayInfo (dtoken, & dinfo ); printf ("w = % d, h = % d, xdpi = % f, ydpi = % f, fps = % f, ds = % f \ n", dinfo. w, dinfo. h, dinfo. xdpi, dinfo. ydpi, dinfo. fps, dinfo. density); if (status) return-1; // create a surface. Some systems may report an error, dinfo. w and dinfo. h can also be written as a fixed value sp
                
                  SurfaceControl = client-> createSurface (String8 ("showYUV"), dinfo. w, dinfo. h, PIXEL_FORMAT_RGBA_8888, 0);/************************* get yuv data from file; **************************************** /printf ("[% s] [% d] \ n ", __file __,__ LINE _); int width, height; width = 320; height = 240; int size = width * height * 3/2; unsigned char * data = new unsigned char [size]; char * path = "/mnt/sdcard/yuv_320_240.yuv"; getYV12Data (path, data, size ); // get yuv data from file; /********************* configure the surface **************** **************************************** * **********/SurfaceComposerClient:: openGlobalTransaction (); surfaceControl-> setLayer (100000); // set zcoordinate surfaceControl-> setPosition (100,100); // set the upper left corner) set the display position surfaceControl-> setSize (width, height); // set the video display size SurfaceComposerClient: closeGlobalTransaction (); sp
                 
                   Surface = surfaceControl-> getSurface (); printf ("[% s] [% d] \ n" ,__ FILE __,__ LINE __); /*************************************** **************************************** * ********/sp
                  
                    Meta = new MetaData; meta-> setInt32 (kKeyWidth, width); meta-> setInt32 (kKeyHeight, height);/* specify the yuv format and support the following yuv format: * OMX_COLOR_FormatYUV420Planar: * metadata: * HAL_PIXEL_FORMAT_YV12: * others may be converted to OMX_COLOR_Format16bitRGB565 */meta-> setInt32 (kKeyColorFormat, HAL_PIXEL_FORMAT_YV12). // setRect is not required, I do not know what is the purpose and principle of the settings, but set the parameters correctly. meta-> setRect (kKeyCropRect, 0, // left 0, // top width-1, // right height-1); // bottomprintf ("[% s] [% d] \ n" ,__ FILE __, __line _); SoftwareRenderer * sr = new SoftwareRenderer (surface, meta); // initialize printf ("[% s] [% d] \ n" ,__ FILE __, __line _); sr-> render (data, size, NULL); // The Key is displayed here, and delete [] data; printf ("[% s] [% d] \ n" ,__ FILE __,__ LINE _); IPCThreadState: self ()-> joinThreadPool (); // ensure that the image is always displayed; otherwise, IPCThreadState: self ()-> stopProcess (); return 0;} will disappear instantly ;}
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 

LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_SRC_FILES:= \main.cppLOCAL_STATIC_LIBRARIES := \libstagefright_color_conversionLOCAL_SHARED_LIBRARIES := \libcutils \libutils \libbinder \    libui \    libgui \libstagefright\libstagefright_foundationLOCAL_C_INCLUDES := \frameworks/native/include/media/openmax \    frameworks/av/media/libstagefrightLOCAL_MODULE:= showYUVLOCAL_MODULE_TAGS := testsinclude $(BUILD_EXECUTABLE)



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.