YUV使用总结 —— Android常用的几种格式:NV21/NV12/YV12/YUV420P的区别

本文主要是介绍YUV使用总结 —— Android常用的几种格式:NV21/NV12/YV12/YUV420P的区别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


http://doc.okbase.net/raomengyang/archive/186891.html


工作问题接触到图像这一块,需要对手机摄像头采集的原始帧做Rotate或者scale,但无奈对此的了解少之又少,于是网上搜了一顿,完事后将最近所学总结一下,以方便之后的人别踩太多坑。
首先想要了解YUV为何物,请猛戳: https://msdn.microsoft.com/en-us/library/aa904813(VS.80).aspx
上面的链接中,微软已经写的很详细了,国内大部分文章都是翻译这篇文章的,如果还有疑问的同学可以参考下面这些大神的博客:
  • 最简单的基于FFmpeg的libswscale的示例(YUV转RGB)  http://blog.csdn.net/leixiaohua1020/article/details/42134965
  • 图文详解YUV420数据格式:http://www.cnblogs.com/azraelly/archive/2013/01/01/2841269.htm
  • ANDROID 高性能图形处理: http://tangzm.com/blog/?p=18

 

  看完上面的文章应该都会有所了解和认识了,因为在Android API <= 20(Android5.0之前的版本)中Google支持的Camera Preview Callback的YUV常用格式有两种:一个是NV21,一个是YV12,在此针对这两种格式做分析。

NV21:

  先贴一段微软的叙述:

4:2:0 Formats, 12 Bits per Pixel

  Four 4:2:0 12-bpp formats are recommended, with the following FOURCC codes:

  • IMC2
  • IMC4
  • YV12
  • NV12

  In all of these formats, the chroma channels are subsampled by a factor of two in both the horizontal and vertical dimensions. 

YV12

  All of the Y samples appear first in memory as an array of unsigned char values. This array is followed immediately by all of the V (Cr) samples. The stride of the V plane is half the stride of the Y plane, and the V plane contains half as many lines as the Y plane. The V plane is followed immediately by all of the U (Cb) samples, with the same stride and number of lines as the V plane (Figure 12).

Figure 12. YV12 memory layout

Figure 12. YV12 memory layout

NV12

  All of the Y samples are found first in memory as an array of unsigned char values with an even number of lines. The Y plane is followed immediately by an array of unsigned char values that contains packed U (Cb) and V (Cr) samples, as shown in Figure 13. When the combined U-V array is addressed as an array of little-endian WORD values, the LSBs contain the U values, and the MSBs contain the V values. NV12 is the preferred 4:2:0 pixel format for DirectX VA. It is expected to be an intermediate-term requirement for DirectX VA accelerators supporting 4:2:0 video.

Figure 13. NV12 memory layout

Figure 13. NV12 memory layout

  从上可知YV12和NV12所占内存是12bits/Pixel,因为每个Y就是一个像素点,注意红色加粗的叙述,YUV值在内存中是按照数组的形式存放的,而由于YV12和NV21都是属于planar格式,也就是Y值和UV值是独立采样的:

  In a planar format, the Y, U, and V components are stored as three separate planes.

  既然Y、U、V值都是独立的,那就意味着我们可以分别处理相应的值,比如在YV12中,排列方式是这样的,每4个Y共用一对UV值,而U、V值又是按照如下格式排列(下面是YV12格式中,宽为16,高为4像素的排列) :

Y第一行:| Y  Y    |    Y  Y   |   Y  Y  |   Y  Y  |
Y第二行:| Y  Y    |    Y  Y   |   Y  Y  |   Y  Y  |
--------------------------------------------------------------
Y第三行:| Y  Y    |    Y  Y   |   Y  Y  |   Y  Y  |
Y第四行:| Y  Y    |    Y  Y   |   Y  Y  |   Y  Y  |
--------------------------------------------------------------
V第一行:  V0    V1    V2     V3    |
U第一行:  U0    U1       U2       U3    |
--------------------------------------------------------------
V第二行:  V4    V5    V6     V7    |
U第二行:  U4    U5     U6     U7    |
--------------------------------------------------------------
16x4像素的YV12排列
知道了YUV值的结构,我们就可以任性的对此图像做Rotate,scale等等。这里我以480*270 (16:9)的一张原始帧图像举例,贴出部分代码示例:
  随便设定的一个带有onPreviewFrame的类,CameraPreviewFrame.java:  
/*** 获取preview的原始帧:* * 这里有个前提,因为Android camera preview默认格式为NV21的,所以需要* 调用setPreviewFormat()方法设置为我们需要的格式* */@Override
public void onPreviewFrame(byte[] data, Camera camera) {// 假设这里的data为480x270原始帧
String SRC_FRAME_WIDTH = 480;String SRC_FRAME_HEIGHT = 270;String DES_FRAME_WIDTH = 480;String DES_FRAME_HEIGHT = 270;// 此处将data数组保存在了指定的路径,保存类型为jpeg格式,但是普通的图片浏// 览器是无法打开的,需要使用RawView等专业的工具打开。
        saveImageData(data);// 定义与原始帧大小一样的outputData,因为YUV420所占内存是12Bits/Pixel,// 每个Y为一个像素8bit=1Byte,U=2bit=1/4(Byte),V=2bit=1/4(Byte),// Y值数量为480*270,则U=V=480*270*(1/4)byte[] outputData = new byte[DES_FRAME_WIDTH * DES_FRAME_HEIGHT * 3 / 2]; // call the JNI method to rotate frame data clockwise 90 degreesYuvUtil.DealYV12(data, outputData, SRC_FRAME_WIDTH, SRC_FRAME_HEIGHT, 90);saveImageData(outputData);}
}// save image to sdcard path: Pictures/MyTestImage/
public void saveImageData(byte[] imageData) {File imageFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);if (imageFile == null) {return;}try {FileOutputStream fos = new FileOutputStream(imageFile);fos.write(imageData);fos.close();} catch (FileNotFoundException e) {e.printStackTrace();Log.e(TAG, "File not found: " + e.getMessage());} catch (IOException e) {e.printStackTrace();Log.e(TAG, "Error accessing file: " + e.getMessage());}}public static File getOutputMediaFile(int type) {File imageFileDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyTestImage");if (!imageFileDir.exists()) {if (!imageFileDir.mkdirs()) {Log.e(TAG, "can't makedir for imagefile");return null;}}// Create a media file nameString timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());File imageFile;if (type == MEDIA_TYPE_IMAGE) {imageFile = new File(imageFileDir.getPath() + File.separator +"IMG_" + timeStamp + ".jpg");} else if (type == MEDIA_TYPE_VIDEO) {imageFile = new File(imageFileDir.getPath() + File.separator +"VID_" + timeStamp + ".mp4");} else {return null;}return imageFile;
}

  上面的代码中可以看到我调用了Jni的方法:YuvUtil.RotateYV12();

  YuvUtil.java

public class YuvUtil {// 初始化,为data分配相应大小的内存public static native void initYV12(int length, int scale_length);public static native void DealYV12(byte[] src_data, byte[] dst_data, int width, int height, int rotation);
}

   对应的Jni的C代码如下:

  com_example_jni_YuvUtil.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class _Included_com_example_jni_YuvUtil */#ifndef _Included_com_example_jni_YuvUtil
#define _Included_com_example_jni_YuvUtil
#ifdef __cplusplus
extern "C" {
#endif
/** Class:     com_example_jni_YuvUtil* Method:    initYV12* Signature: (II)V*/
JNIEXPORT void JNICALL Java_com_example_jni_YuvUtil_initYV12(JNIEnv *, jclass, jint, jint);/** Class:     com_example_jni_YuvUtil* Method:    DealYV12* Signature: ([B[BIIIII)V*/
JNIEXPORT void JNICALL Java_com_example_jni_YuvUtil_DealYV12(JNIEnv *, jclass, jbyteArray, jbyteArray, jint, jint, jint, jint, jint);#ifdef __cplusplus
}
#endif
#endif

  com_example_jni_YuvUtil.c

#include "com_example_jni_YuvUtil.h"
#include <android/log.h>
#include <string.h>
#include <jni.h>
#include <stdlib.h>#define TAG "jni-log-jni" // 这个是自定义的LOG的标识
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG ,__VA_ARGS__) // 定义LOGD类型
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,TAG ,__VA_ARGS__) // 定义LOGI类型
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,TAG ,__VA_ARGS__) // 定义LOGW类型
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,TAG ,__VA_ARGS__) // 定义LOGE类型
#define LOGF(...) __android_log_print(ANDROID_LOG_FATAL,TAG ,__VA_ARGS__) // 定义LOGF类型char *input_src_data, *output_src_data, *src_y_data,*src_u_data, *src_v_data, *dst_y_data, *dst_v_data;
int src_data_width, src_data_height, len_src;/** Class:     com_example_jni_YuvUtil*/
JNIEXPORT void JNICALL Java_com_example_jni_YuvUtil_initYV12
(JNIEnv *env, jclass jcls, jint length, jint scaleDataLength) {
len_src = length;
len_scale = scaleDataLength;
LOGD("########## len_src  = %d, len_scale = %d \n", len_src, len_scale);input_src_data = malloc(sizeof(char) * len_src);
LOGD("########## input_src_data  = %d \n", input_src_data);src_y_data = malloc(sizeof(char) * (len_src * 2 / 3));
src_u_data = malloc(sizeof(char) * (len_src / 6));
src_v_data = malloc(sizeof(char) * (len_src / 6));dst_y_data = malloc(sizeof(char) * (len_src * 2 / 3));
dst_u_data = malloc(sizeof(char) * (len_src / 6));
dst_v_data = malloc(sizeof(char) * (len_src / 6));}JNIEXPORT void JNICALL Java_com_example_jni_YuvUtil_DealYV12
(JNIEnv *env, jclass jcls, jbyteArray src_data,jbyteArray dst_data, jint width, jint height, jint rotation, jint dst_width, jint dst_height) {
src_data_width = width;
src_data_height = height;// 将src_data的数据传给input_src_data
(*env)->GetByteArrayRegion (env, src_data, 0, len_src, (jbyte*)(input_src_data));/*以下三个memcpy分别将Y、U、V值从src_data中提取出来,将YUV值分别scale或者rotate,则可得到对应格式的图像数据*/
// get y plane
memcpy(src_y_data, input_src_data , (len_src * 2 /3));
// get u plane
memcpy(src_u_data, input_src_data + (len_src * 2 / 3), len_src / 6);
// get v plane
memcpy(src_v_data, input_src_data + (len_src * 5 / 6 ), len_src / 6);
/*获取yuv三个值的数据可以做相应操作*/
// ......... 
// .........// 例:将Y值置为0,则得到没有灰度的图像;
memset(input_src_data + src_data_width * src_data_height, 0, src_data_width * src_data_height);// 将input_src_data的数据返回给dst_data输出
// output to the dst_data
(*env)->SetByteArrayRegion (env, dst_data, 0, len_src, (jbyte*)(input_src_data));}/*** free memory*/
JNIEXPORT void JNICALL Java_com_example_jni_YuvUtil_ReleaseYV12
(JNIEnv *env , jclass jcls) {
free(output_src_data);
free(input_src_data);
}

  注意:以上代码不是完全的,只是用于说明而已,如果需要更多的操作还请各位朋友自己完善,因为没怎么写过这类博客,很多地方很乱,表述的不清楚,有问题的朋友可以问我。 

  

  

  

原文链接: http://www.cnblogs.com/raomengyang/p/4924787.html


这篇关于YUV使用总结 —— Android常用的几种格式:NV21/NV12/YV12/YUV420P的区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/680529

相关文章

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

在.NET平台使用C#为PDF添加各种类型的表单域的方法

《在.NET平台使用C#为PDF添加各种类型的表单域的方法》在日常办公系统开发中,涉及PDF处理相关的开发时,生成可填写的PDF表单是一种常见需求,与静态PDF不同,带有**表单域的文档支持用户直接在... 目录引言使用 PdfTextBoxField 添加文本输入域使用 PdfComboBoxField

Git可视化管理工具(SourceTree)使用操作大全经典

《Git可视化管理工具(SourceTree)使用操作大全经典》本文详细介绍了SourceTree作为Git可视化管理工具的常用操作,包括连接远程仓库、添加SSH密钥、克隆仓库、设置默认项目目录、代码... 目录前言:连接Gitee or github,获取代码:在SourceTree中添加SSH密钥:Cl

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib