您的位置:首页 > 移动开发 > Android开发

Android 直接显示yuv数据 通过Awesomeplayer方式直接显示(三)

2016-09-19 10:11 555 查看
研究一段时间Android的surface系统,一直执着地认为所有在surface或者屏幕上显示的画面,必须要转换成RGB才能显示,yuv数据也要通过颜色空间转换成RGB才能显示。可最近在研究stagefright视频显示时发现,根本找不到omx解码后的yuv是怎么转换成RGB的代码,yuv数据在render之后就找不到去向了,可画面确确实实的显示出来了,这从此颠覆了yuv必须要转换成RGB才能显示的真理了。

    稍微看一下AsomePlayer的代码,不难发现,视频的每一帧是通过调用了SoftwareRenderer来渲染显示的,我也尝试用利用SoftwareRenderer来直接render yuv数据显示,竟然成功了,这是一个很大的突破,比如以后摄像头采集到的yuv,可以直接丢yuv数据到surface显示,无需耗时耗效率的yuv转RGB了。

上一篇文章主要是参照AwesomePlayer直接用SoftwareRenderer类来显示yuv,为了能用到这个类,不惜依赖了libstagefright、libstagefright_color_conversion等动态静态库,从而造成程序具有很高的耦合度,也不便于我们理解yuv数据直接显示的深层次原因。

    于是我开始研究SoftwareRenderer的具体实现,我们来提取SoftwareRenderer的核心代码,自己来实现yuv的显示。

    SoftwareRenderer就只有三个方法,一个构造函数,一个析构函数,还有一个负责显示的render方法。构造方法里有个很重要的地方native_window_set_buffers_geometry这里是配置即将申请的图形缓冲区的宽高和颜色空间,忽略了这个地方,画面将用默认的值显示,将造成显示不正确。render函数里最重要的三个地方,一个的dequeBuffer,一个是mapper,一个是queue_buffer。

[cpp] view
plain copy

 





native_window_set_buffers_geometry;//设置宽高以及颜色空间yuv420  

native_window_dequeue_buffer_and_wait;//根据以上配置申请图形缓冲区  

mapper.lock(buf->handle, GRALLOC_USAGE_SW_WRITE_OFTEN, bounds, &dst));//将申请到的图形缓冲区跨进程映射到用户空间  

memcpy(dst, data, dst_y_size + dst_c_size*2);//填充yuv数据到图形缓冲区  

mNativeWindow->queueBuffer;//显示  

以上五步是surface显示图形必不可少的五步。

有了以上分析,我们直接上代码:(yuv数据下载地址点击打开链接,放到sdcard)

main.cpp

[cpp] view
plain copy

 





#include <cutils/memory.h>  

  

#include <unistd.h>  

#include <utils/Log.h>  

  

#include <binder/IPCThreadState.h>  

#include <binder/ProcessState.h>  

#include <binder/IServiceManager.h>  

#include <media/stagefright/foundation/ADebug.h>  

#include <gui/Surface.h>  

#include <gui/SurfaceComposerClient.h>  

#include <gui/ISurfaceComposer.h>  

#include <ui/DisplayInfo.h>  

#include <android/native_window.h>  

#include <system/window.h>  

#include <ui/GraphicBufferMapper.h>  

//ANativeWindow 就是surface,对应surface.cpp里的code  

using namespace android;  

  

//将x规整为y的倍数,也就是将x按y对齐  

static int ALIGN(int x, int y) {  

    // y must be a power of 2.  

    return (x + y - 1) & ~(y - 1);  

}  

  

void render(  

        const void *data, size_t size, const sp<ANativeWindow> &nativeWindow,int width,int height) {  

    sp<ANativeWindow> mNativeWindow = nativeWindow;  

    int err;  

    int mCropWidth = width;  

    int mCropHeight = height;  

      

    int halFormat = HAL_PIXEL_FORMAT_YV12;//颜色空间  

    int bufWidth = (mCropWidth + 1) & ~1;//按2对齐  

    int bufHeight = (mCropHeight + 1) & ~1;  

      

    CHECK_EQ(0,  

            native_window_set_usage(  

            mNativeWindow.get(),  

            GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN  

            | GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP));  

  

    CHECK_EQ(0,  

            native_window_set_scaling_mode(  

            mNativeWindow.get(),  

            NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW));  

  

    // Width must be multiple of 32???  

    //很重要,配置宽高和和指定颜色空间yuv420  

    //如果这里不配置好,下面deque_buffer只能去申请一个默认宽高的图形缓冲区  

    CHECK_EQ(0, native_window_set_buffers_geometry(  

                mNativeWindow.get(),  

                bufWidth,  

                bufHeight,  

                halFormat));  

      

      

    ANativeWindowBuffer *buf;//描述buffer  

    //申请一块空闲的图形缓冲区  

    if ((err = native_window_dequeue_buffer_and_wait(mNativeWindow.get(),  

            &buf)) != 0) {  

        ALOGW("Surface::dequeueBuffer returned error %d", err);  

        return;  

    }  

  

    GraphicBufferMapper &mapper = GraphicBufferMapper::get();  

  

    Rect bounds(mCropWidth, mCropHeight);  

  

    void *dst;  

    CHECK_EQ(0, mapper.lock(//用来锁定一个图形缓冲区并将缓冲区映射到用户进程  

                buf->handle, GRALLOC_USAGE_SW_WRITE_OFTEN, bounds, &dst));//dst就指向图形缓冲区首地址  

  

    if (true){  

        size_t dst_y_size = buf->stride * buf->height;  

        size_t dst_c_stride = ALIGN(buf->stride / 2, 16);//1行v/u的大小  

        size_t dst_c_size = dst_c_stride * buf->height / 2;//u/v的大小  

          

        memcpy(dst, data, dst_y_size + dst_c_size*2);//将yuv数据copy到图形缓冲区  

    }  

  

    CHECK_EQ(0, mapper.unlock(buf->handle));  

  

    if ((err = mNativeWindow->queueBuffer(mNativeWindow.get(), buf,  

            -1)) != 0) {  

        ALOGW("Surface::queueBuffer returned error %d", err);  

    }  

    buf = NULL;  

}  

  

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));  

    DisplayInfo dinfo;  

    //获取屏幕的宽高等信息  

    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;  

    //创建surface  

    sp<SurfaceControl> surfaceControl = client->createSurface(String8("testsurface"),  

            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];  

    const char *path = "/mnt/sdcard/yuv_320_240.yuv";  

    getYV12Data(path,data,size);//get yuv data from file;  

      

/*********************配置surface*******************************************************************/  

    SurfaceComposerClient::openGlobalTransaction();  

    surfaceControl->setLayer(100000);//设定Z坐标  

    surfaceControl->setPosition(100, 100);//以左上角为(0,0)设定显示位置  

    surfaceControl->setSize(width, height);//设定视频显示大小  

    SurfaceComposerClient::closeGlobalTransaction();  

    sp<Surface> surface = surfaceControl->getSurface();  

    printf("[%s][%d]\n",__FILE__,__LINE__);  

      

/**********************显示yuv数据******************************************************************/     

    render(data,size,surface,width,height);  

    printf("[%s][%d]\n",__FILE__,__LINE__);  

      

    IPCThreadState::self()->joinThreadPool();//可以保证画面一直显示,否则瞬间消失  

    IPCThreadState::self()->stopProcess();  

    return 0;  

}  

Android.mk (这次依赖的库少了很多)

[cpp] view
plain copy

 





LOCAL_PATH:= $(call my-dir)  

include $(CLEAR_VARS)  

  

LOCAL_SRC_FILES:= \  

    main.cpp  

      

LOCAL_SHARED_LIBRARIES := \  

    libcutils \  

    libutils \  

    libbinder \  

    libui \  

    libgui \  

    libstagefright_foundation  

      

LOCAL_MODULE:= MyShowYUV  

  

LOCAL_MODULE_TAGS := tests  

  

include $(BUILD_EXECUTABLE)  

文章结尾有引用的出处,那里有相关问题的解答

渲染yuv数据的两种思考方法

思路1:在java中将Surface指针传递到jni层,lock之后就可以获得SurfaceInfo,进而取得要显示的surface格式、高度、宽度,在2.2/2.3版本,surface的Format一般都是RGB565格式,只用做一个颜色空间的转换,scaler就可以将yuv数据显示出来。

颜色空间转换和Scaler算是比较耗时的操作了。如何提高效率,scaler最好能交给android的底层函数去做,如果有gpu的,底层函数直接会利用gpu,效率非常高,又不占用cpu资源。

思路2:

   参考framework中的AwesomePlayer,里面利用AwesomeLocalRenderer/AwesomeRemoteRenderer来实现解码出来的数据显示,这个效率应该非常高,但是平台的关联性会增加很多。

   调用接口比较简单,

   首先创建一个render,

               mVideoRenderer = new AwesomeRemoteRenderer(

                mClient.interface()->createRenderer(

                        mISurface, component,

                        (OMX_COLOR_FORMATTYPE)format,

                        decodedWidth, decodedHeight,

                        mVideoWidth, mVideoHeight,

                        rotationDegrees));

  直接调用render函数就可以显示了。

    virtual void render(MediaBuffer *buffer) {

        void *id;

        if (buffer->meta_data()->findPointer(kKeyBufferID, &id)) {

            mTarget->render((IOMX::buffer_id)id);

        }

    }

  

   其它的参数都很容易获得,关键是buffer_id 怎么获得?OMXCodec.cpp中有相关的可以参考。

   实际的效果在我的S510E上跑,效率非常高,几乎不占用主控cpu资源,很可能都交给dsp和gpu去搞了。  

  

本文用Java创建UI并联合JNI层操作surface来直接显示yuv数据(yv12),开发环境为Android 4.4,全志A23平台。

[java]
view plain
copy

package com.example.myyuvviewer;  
  
import java.io.File;  
import java.io.FileInputStream;  
import android.app.Activity;  
import android.os.Bundle;  
import android.os.Environment;  
import android.util.Log;  
import android.view.Surface;  
import android.view.SurfaceHolder;  
import android.view.SurfaceHolder.Callback;  
import android.view.SurfaceView;  
  
public class MainActivity extends Activity {  
  
    final private String TAG = "MyYUVViewer";  
    final private String FILE_NAME = "yuv_320_240.yuv";  
    private int width = 320;  
    private int height = 240;  
    private int size = width * height * 3/2;  
      
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        nativeTest();  
        SurfaceView surfaceview = (SurfaceView) findViewById(R.id.surfaceView);  
        SurfaceHolder holder = surfaceview.getHolder();  
        holder.addCallback(new Callback(){  
  
            @Override  
            public void surfaceCreated(SurfaceHolder holder) {  
                // TODO Auto-generated method stub  
                Log.d(TAG,"surfaceCreated");  
                byte[]yuvArray = new byte[size];  
                readYUVFile(yuvArray, FILE_NAME);  
                nativeSetVideoSurface(holder.getSurface());  
                nativeShowYUV(yuvArray,width,height);  
            }  
  
            @Override  
            public void surfaceChanged(SurfaceHolder holder, int format,  
                    int width, int height) {  
                // TODO Auto-generated method stub  
                  
            }  
  
            @Override  
            public void surfaceDestroyed(SurfaceHolder holder) {  
                // TODO Auto-generated method stub  
                  
            }});  
    }  
      
    private boolean readYUVFile(byte[] yuvArray,String filename){  
        try {  
            // 如果手机插入了SD卡,而且应用程序具有访问SD的权限  
            if (Environment.getExternalStorageState().equals(  
                    Environment.MEDIA_MOUNTED)) {  
                // 获取SD卡对应的存储目录  
                File sdCardDir = Environment.getExternalStorageDirectory();  
                // 获取指定文件对应的输入流  
                FileInputStream fis = new FileInputStream(  
                        sdCardDir.getCanonicalPath() +"/" + filename);  
                fis.read(yuvArray, 0, size);  
                fis.close();  
                return true;  
            } else {  
                return false;  
            }  
        }catch (Exception e) {  
            e.printStackTrace();  
            return false;  
        }  
    }  
    private native void nativeTest();  
    private native boolean nativeSetVideoSurface(Surface surface);  
    private native void nativeShowYUV(byte[] yuvArray,int width,int height);  
    static {  
        System.loadLibrary("showYUV");  
    }  
}  

activity_main.xml

[html]
view plain
copy

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent"    
    android:orientation="vertical" >    
    
    <SurfaceView    
        android:id="@+id/surfaceView"    
        android:layout_width="fill_parent"    
        android:layout_height="360dp" />   
          
</LinearLayout>  

JNI层,showYUV.cpp(libshowyuv.so)采用动态注册JNI函数的方法.

[cpp]
view plain
copy

#include <jni.h>  
#include <android_runtime/AndroidRuntime.h>  
#include <android_runtime/android_view_Surface.h>  
#include <gui/Surface.h>  
#include <assert.h>  
#include <utils/Log.h>  
#include <JNIHelp.h>  
#include <media/stagefright/foundation/ADebug.h>  
#include <ui/GraphicBufferMapper.h>  
#include <cutils/properties.h>  
using namespace android;  
  
static sp<Surface> surface;  
  
static int ALIGN(int x, int y) {  
    // y must be a power of 2.  
    return (x + y - 1) & ~(y - 1);  
}  
  
static void render(  
        const void *data, size_t size, const sp<ANativeWindow> &nativeWindow,int width,int height) {  
    ALOGE("[%s]%d",__FILE__,__LINE__);  
    sp<ANativeWindow> mNativeWindow = nativeWindow;  
    int err;  
    int mCropWidth = width;  
    int mCropHeight = height;  
      
    int halFormat = HAL_PIXEL_FORMAT_YV12;//颜色空间  
    int bufWidth = (mCropWidth + 1) & ~1;//按2对齐  
    int bufHeight = (mCropHeight + 1) & ~1;  
      
    CHECK_EQ(0,  
            native_window_set_usage(  
            mNativeWindow.get(),  
            GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN  
            | GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP));  
  
    CHECK_EQ(0,  
            native_window_set_scaling_mode(  
            mNativeWindow.get(),  
            NATIVE_WINDOW_SCALING_MODE_SCALE_CROP));  
  
    // Width must be multiple of 32???  
    //很重要,配置宽高和和指定颜色空间yuv420  
    //如果这里不配置好,下面deque_buffer只能去申请一个默认宽高的图形缓冲区  
    CHECK_EQ(0, native_window_set_buffers_geometry(  
                mNativeWindow.get(),  
                bufWidth,  
                bufHeight,  
                halFormat));  
      
      
    ANativeWindowBuffer *buf;//描述buffer  
    //申请一块空闲的图形缓冲区  
    if ((err = native_window_dequeue_buffer_and_wait(mNativeWindow.get(),  
            &buf)) != 0) {  
        ALOGW("Surface::dequeueBuffer returned error %d", err);  
        return;  
    }  
  
    GraphicBufferMapper &mapper = GraphicBufferMapper::get();  
  
    Rect bounds(mCropWidth, mCropHeight);  
  
    void *dst;  
    CHECK_EQ(0, mapper.lock(//用来锁定一个图形缓冲区并将缓冲区映射到用户进程  
                buf->handle, GRALLOC_USAGE_SW_WRITE_OFTEN, bounds, &dst));//dst就指向图形缓冲区首地址  
  
    if (true){  
        size_t dst_y_size = buf->stride * buf->height;  
        size_t dst_c_stride = ALIGN(buf->stride / 2, 16);//1行v/u的大小  
        size_t dst_c_size = dst_c_stride * buf->height / 2;//u/v的大小  
          
        memcpy(dst, data, dst_y_size + dst_c_size*2);//将yuv数据copy到图形缓冲区  
    }  
  
    CHECK_EQ(0, mapper.unlock(buf->handle));  
  
    if ((err = mNativeWindow->queueBuffer(mNativeWindow.get(), buf,  
            -1)) != 0) {  
        ALOGW("Surface::queueBuffer returned error %d", err);  
    }  
    buf = NULL;  
}  
  
static void nativeTest(){  
    ALOGE("[%s]%d",__FILE__,__LINE__);  
}  
  
static jboolean  
nativeSetVideoSurface(JNIEnv *env, jobject thiz, jobject jsurface){  
    ALOGE("[%s]%d",__FILE__,__LINE__);  
    surface = android_view_Surface_getSurface(env, jsurface);  
    if(android::Surface::isValid(surface)){  
        ALOGE("surface is valid ");  
    }else {  
        ALOGE("surface is invalid ");  
        return false;  
    }  
    ALOGE("[%s][%d]\n",__FILE__,__LINE__);  
    return true;  
}  
static void  
nativeShowYUV(JNIEnv *env, jobject thiz,jbyteArray yuvData,jint width,jint height){  
    ALOGE("width = %d,height = %d",width,height);  
    jint len = env->GetArrayLength(yuvData);  
    ALOGE("len = %d",len);  
    jbyte *byteBuf = env->GetByteArrayElements(yuvData, 0);  
    render(byteBuf,len,surface,width,height);  
}  
static JNINativeMethod gMethods[] = {  
    {"nativeTest",                  "()V",                              (void *)nativeTest},  
    {"nativeSetVideoSurface",       "(Landroid/view/Surface;)Z",        (void *)nativeSetVideoSurface},  
    {"nativeShowYUV",               "([BII)V",                          (void *)nativeShowYUV},  
};  
  
static const char* const kClassPathName = "com/example/myyuvviewer/MainActivity";  
  
// This function only registers the native methods  
static int register_com_example_myyuvviewer(JNIEnv *env)  
{  
    ALOGE("[%s]%d",__FILE__,__LINE__);  
    return AndroidRuntime::registerNativeMethods(env,  
                kClassPathName, gMethods, NELEM(gMethods));  
}  
  
jint JNI_OnLoad(JavaVM* vm, void* reserved)  
{  
    ALOGE("[%s]%d",__FILE__,__LINE__);  
    JNIEnv* env = NULL;  
    jint result = -1;  
  
    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {  
        ALOGE("ERROR: GetEnv failed\n");  
        goto bail;  
    }  
    assert(env != NULL);  
    ALOGE("[%s]%d",__FILE__,__LINE__);  
   if (register_com_example_myyuvviewer(env) < 0) {  
        ALOGE("ERROR: MediaPlayer native registration failed\n");  
        goto bail;  
    }  
  
    /* success -- return valid version number */  
    result = JNI_VERSION_1_4;  
  
bail:  
    return result;  
}  

Android.mk

[plain]
view plain
copy

LOCAL_PATH:= $(call my-dir)  
include $(CLEAR_VARS)  
  
LOCAL_SRC_FILES:= \  
    showYUV.cpp  
      
LOCAL_SHARED_LIBRARIES := \  
    libcutils \  
    libutils \  
    libbinder \  
    libui \  
    libgui \  
    libandroid_runtime \  
    libstagefright_foundation  
      
LOCAL_MODULE:= libshowYUV  
  
LOCAL_MODULE_TAGS := tests  
  
include $(BUILD_SHARED_LIBRARY)  

生成的so文件复制到Java项目里 与src并列的libs/armeabi目录下,没有就手动创建目录,

这样Eclipse会自动把so库打包进apk。

转载请注明出处:http://blog.csdn.net/tung214/article/details/37762487

yuvdata下载地址:点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息