Android uses surface to display YUV data directly

Source: Internet
Author: User

After studying the surface system of Android for some time, I have been insisting that all the images displayed on surface or on screen must be converted to RGB to display, and YUV data will be converted to RGB by color space. Can be recently in the study of stagefright video display, found that the OMX is not found after the conversion of the YUV is converted to RGB code, YUV data in the render after the whereabouts can not be found, but the screen is really real show out, This has since overturned the truth that YUV must be converted to RGB to display.

A little look at the Asomeplayer code, it is not difficult to find that each frame of the video is called by the Softwarerenderer to render the display, I also try to use the Softwarerenderer to directly render YUV data display, unexpectedly successful, This is a big breakthrough, such as the next camera captured YUV, can be directly lost data to the surface display, without time-consuming and efficient YUV to RGB.

Code original, posted out to share with you: Android 4.4 Platform (where YUV data address can be downloaded from here Click the Open link, put into the/mnt/sdcard directory)

#include <include/SoftwareRenderer.h> #include <cutils/memory.h> #include <unistd.h> #include < utils/log.h> #include <binder/IPCThreadState.h> #include <binder/ProcessState.h> #include <binder/ iservicemanager.h> #include <gui/Surface.h> #include <gui/SurfaceComposerClient.h> #include <gui/ isurfacecomposer.h> #include <ui/DisplayInfo.h> #include <android/native_window.h> #include <media /stagefright/metadata.h> 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<processstate> proc (processstate::self ()); Processstate::self ()->startthreadpool ();//Create a client to Surfaceflinger sp<surfacecomposerclient> Client = new Surfacecomposerclient ();sp<ibinder> DtokEn (Surfacecomposerclient::getbuiltindisplay (isurfacecomposer::edisplayidmain));D Isplayinfo dinfo;//get wide and high information about the screen status_t status = Surfacecomposerclient::getdisplayinfo (Dtoken, &dinfo);p rintf ("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 surface, some systems may error, DINFO.W and dinfo.h can also be written as fixed value sp<surfacecontrol> Surfacecontrol = CLI Ent->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;/********************* configuring surface    *******************************************************************/Surfacecomposerclient::openglobaltransaction (); Surfacecontrol->setlayer (100000);//Set z-coordinate surfacecontrol->setposition (100, 100);//with upper left corner (0,0) Set the display position surfacecontrol->setsize (width, height);//Set the video display size surfacecomposerclient::closeglobaltransaction (); SP <Surface> Surface = Surfacecontrol->getsurface ();p rintf ("[%s][%d]\n", __file__,__line__);/*************** /sp<metadata> meta = new MetaData; Meta->setint32 (kkeywidth, width), meta->setint32 (kkeyheight, height);/* Specify YUV format, support the following YUV format * omx_color_ Formatyuv420planar: * Omx_ti_color_formatyuv420packedsemiplanar: * HAL_PIXEL_FORMAT_YV12: * Other seems to convert to Omx_color_for mat16bitrgb565 */meta->setint32 (Kkeycolorformat, HAL_PIXEL_FORMAT_YV12);//setrect don't, I don't know what it's for, what the principle is,                    But set, the parameters must be correct 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);//key here, show to screen delete[] data;printf ("[%s][%d]\n", __file__,__line__); Ipcthreadstate::self ( )->jointhreadpool ();//can ensure that the screen has been displayed, otherwise instantly disappear ipcthreadstate::self ()->stopprocess (); return 0;}

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 Libstagefrightlibstagefright_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.