From 837ab8a01456ab6000f73aec90a61baae1fa6a7a Mon Sep 17 00:00:00 2001 From: ls-Brynn <87458342+ls-Brynn@users.noreply.github.com> Date: Fri, 30 Jul 2021 16:54:08 +0800 Subject: [PATCH] =?UTF-8?q?Update=20and=20rename=20037-=E8=A7=86=E9=9F=B3?= =?UTF-8?q?=E9=A2=91=E6=95=B0=E6=8D=AE=E5=A4=84=E7=90=86=E5=85=A5=E9=97=A8?= =?UTF-8?q?=EF=BC=9ARGB=E3=80=81YUV=E5=83=8F=E7=B4=A0=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=A4=84=E7=90=86.md=20to=20037-RTSP=E5=8D=8F=E8=AE=AE?= =?UTF-8?q?=E5=AD=A6=E4=B9=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- article/037-RTSP协议学习.md | 534 +++++++++++ ...音频数据处理入门:RGB、YUV像素数据处理.md | 890 ------------------ 2 files changed, 534 insertions(+), 890 deletions(-) create mode 100644 article/037-RTSP协议学习.md delete mode 100644 article/037-视音频数据处理入门:RGB、YUV像素数据处理.md diff --git a/article/037-RTSP协议学习.md b/article/037-RTSP协议学习.md new file mode 100644 index 0000000..558cc77 --- /dev/null +++ b/article/037-RTSP协议学习.md @@ -0,0 +1,534 @@ +# RTSP协议学习 + +## RTSP简介 + +RTSP(Real Time Streaming Protocol)是由Real Network和Netscape共同提出的如何有效地在IP网络上传输流媒体数据的应用层协议。RTSP对流媒体提供了诸如暂停,快进等控制,而它本身并不传输数据,RTSP的作用相当于流媒体服务器的远程控制。服务器端可以自行选择使用TCP或UDP来传送串流内容,它的语法和运作跟HTTP 1.1类似,但并不特别强调时间同步,所以比较能容忍网络延迟。 + +## RTSP和HTTP RTP(RTCP)的关系 + +### RTSP和HTTP + +* 联系:两者都用纯文本来发送消息,且rtsp协议的语法也和HTTP类似。Rtsp一开始这样设计,也是为了能够兼容使用以前写的HTTP协议分析代码 。 +* 区别:rtsp是有状态的,不同的是RTSP的命令需要知道现在正处于一个什么状态,也就是说rtsp的命令总是按照顺序来发送,某个命令总在另外一个命令之前要发送。Rtsp不管处于什么状态都不会断掉连接。而http则不保存状态,协议在发送一个命令以后,连接就会断开,且命令之间是没有依赖性。rtsp协议使用554端口,http使用80端口。 + +### RTSP和RTP(RTCP) + +* RTP:Realtime Transport Potocol 实时传输协议 +RTP提供时间标志,序列号以及其他能够保证在实时数据传输时处理时间的方法。 +* RTCP:Realtime Transport Control Potocol 实时传输控制协议 +RTCP是RTP的控制部分,用来保证服务质量和成员管理。RTP和RTCP是一起使用的。 +* RTSP:RealTime Streaming Potocol 实时流协议 +RTSP具体数据传输交给RTP,提供对流的远程控制 + +RTP是基于 UDP协议的, UDP不用建立连接,效率更高;但允许丢包, 这就要求在重新组装媒体的时候多做些工作 +RTP只是包裹内容信息,而RTCP是交换控制信息的,Qos是通过RTCP实现的 +应用程序对应的是play, seek, pause, stop等命令,RTSP则是处理这些命令,在UDP传输时并使用RTP(RTCP)来完成。如果是TCP连接则不会使用RTP(RTCP)。 + +![image](https://user-images.githubusercontent.com/87458342/127626526-1a0a0be0-c00b-45b4-beed-817eca71b79d.png) + +RTSP的client连接server通过SDP(会话描述协议)传递信息,详细请见:RTSP消息 + +RTSP消息 +RTSP的消息有两大类,一是请求消息(request),一是回应消息(response),两种消息的格式不同。 +请求消息格式: + +>方法 URI RTSP版本 CR LF
+>消息头 CR LF CR LF
+>消息体 CR LF + +方法包括:OPTIONS、SETUP、PLAY、TEARDOWN DESCRIBE +URI是接收方(服务端)的地址,例如:rtsp://192.168.22.136:5000/v0 +每行后面的CR LF表示回车换行,需要接收端有相应的解析,消息头需要有两个CR LF。 + +>DESCRIBE rtsp://192.168.1.211 RTSP/1.0
+>CSeq: 1
+>Accept: application/sdp
+>User-Agent: magnus-fc + +回应消息格式: + +>RTSP版本 状态码 解释 CR LF
+>消息头 CR LF CR LF
+>消息体 CR LF + +其中RTSP版本一般都是RTSP/1.0,状态码是一个数值,200表示成功,解释是与状态码对应的文本解释,详细请见:SDP协议介绍。 + +>RTSP/1.0 200 OK
+>CSeq: 1
+>Server: GrandStream Rtsp Server V100R001
+>Content-Type: application/sdp
+>Content-length: 256
+>Content-Base: rtsp://192.168.1.211/0
+>
+>v=0
+>o=StreamingServer 3331435948 1116907222000 IN IP4 192.168.1.211
+>s=h264.mp4
+>c=IN IP4 0.0.0.0
+>t=0 0
+>a=control:*
+>m=video 0 RTP/AVP 96
+>a=control:trackID=0
+>a=rtpmap:96 H264/90000
+>m=audio 0 RTP/AVP 97
+>a=control:trackID=1
+>a=rtpmap:97 G726-16/8000 + + +## 简单的rtsp交互过程: + +>C表示rtsp客户端, S表示rtsp服务端
+>
+>step1:
+>C->S:OPTION request //询问S有哪些方法可用
+>S->C:OPTION response //S回应信息中包括提供的所有可用方法
+>
+>step2:
+>C->S:DESCRIBE request //要求得到S提供的媒体初始化描述信息
+>S->C:DESCRIBE response //S回应媒体初始化描述信息,主要是sdp
+>
+>step3:
+>C->S:SETUP request //设置会话的属性,以及传输模式,提醒S建立会话
+>S->C:SETUP response //S建立会话,返回会话标识符,以及会话相关信息
+>
+>step4:
+>C->S:PLAY request //C请求播放
+>S->C:PLAY response //S回应该请求的信息
+>
+>S->C:发送流媒体数据
+>
+>step5:
+>C->S:TEARDOWN request //C请求关闭会话
+>S->C:TEARDOWN response //S回应该请求 + +## RTSP中常用方法 + +### OPTION + +得到服务器提供的可用方法 + +>OPTIONS rtsp://192.168.20.136:5000/xxx666 RTSP/1.0
+>CSeq: 1 //每个消息都有序号来标记,第一个包通常是option请求消息
+>User-Agent: VLC media player (LIVE555 Streaming Media v2005.11.10) + +服务器的回应信息包括提供的一些方法,例如: + +>RTSP/1.0 200 OK
+>Server: UServer 0.9.7_rc1
+>Cseq: 1 //每个回应消息的cseq数值和请求消息的cseq相对应
+>Public: OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE, SCALE,GET_PARAMETER //服务器提供的可用的方法 + +### DESCRIBE + +C向S发起DESCRIBE请求,为了得到会话描述信息(SDP): + +>DESCRIBE rtsp://192.168.20.136:5000/xxx666 RTSP/1.0
+>CSeq: 2
+>token:
+>Accept: application/sdp
+>User-Agent: VLC media player (LIVE555 Streaming Media v2005.11.10) + +服务器回应一些对此会话的描述信息(sdp): + +>RTSP/1.0 200 OK
+>Server: UServer 0.9.7_rc1
+>Cseq: 2
+>x-prev-url: rtsp://192.168.20.136:5000
+>x-next-url: rtsp://192.168.20.136:5000
+>x-Accept-Retransmit: our-retransmit
+>x-Accept-Dynamic-Rate: 1
+>Cache-Control: must-revalidate
+>Last-Modified: Fri, 10 Nov 2006 12:34:38 GMT
+>Date: Fri, 10 Nov 2006 12:34:38 GMT
+>Expires: Fri, 10 Nov 2006 12:34:38 GMT
+>Content-Base: rtsp://192.168.20.136:5000/xxx666/
+>Content-Length: 344
+>Content-Type: application/sdp
+>
+>v=0 //以下都是sdp信息
+>o=OnewaveUServerNG 1451516402 1025358037 IN IP4 192.168.20.136
+>s=/xxx666
+>u=http:///
+>e=admin@
+>c=IN IP4 0.0.0.0
+>t=0 0
+>a=isma-compliance:1,1.0,1
+>
+>a=range:npt=0-
+>m=video 0 RTP/AVP 96 //m表示媒体描述,下面是对会话中视频通道的媒体描述
+>a=rtpmap:96 MP4V-ES/90000
+>a=fmtp:96 profile-level-id=245;config=000001B0F5000001B509000001000000012000C888B0E0E0FA62D089028307 a=control:trackID=0 //trackID=0表示视频流用的是通道0 + +### SETUP + +客户端提醒服务器建立会话,并确定传输模式: + +>SETUP rtsp://192.168.20.136:5000/xxx666/trackID=0 RTSP/1.0
+>CSeq: 3
+>Transport: RTP/AVP/TCP;unicast;interleaved=0-1
+>User-Agent: VLC media player (LIVE555 Streaming Media v2005.11.10)
+>//uri中 带有trackID=0,表示对该通道进行设置。Transport参数设置了传输模式,包的结构。接下来的数据包头部第二个字节位置就是 interleaved,它的值是每个通道都不同的,trackID=0的interleaved值有两个0或1,0表示rtp包,1表示rtcp包,接收端根据interleaved的值来区别是哪种数据包。 + +服务器回应信息: + +>RTSP/1.0 200 OK
+>Server: UServer 0.9.7_rc1
+>Cseq: 3
+>Session: 6310936469860791894 //服务器回应的会话标识符
+>Cache-Control: no-cache
+>Transport: RTP/AVP/TCP;unicast;interleaved=0-1;ssrc=6B8B4567 + +### PLAY + +客户端发送播放请求: + +>PLAY rtsp://192.168.20.136:5000/xxx666 RTSP/1.0
+>CSeq: 4
+>Session: 6310936469860791894
+>Range: npt=0.000- //设置播放时间的范围
+>User-Agent: VLC media player (LIVE555 Streaming Media v2005.11.10) + + +服务器回应信息: + +>RTSP/1.0 200 OK
+>Server: UServer 0.9.7_rc1
+>Cseq: 4
+>Session: 6310936469860791894
+>Range: npt=0.000000-
+>RTP-Info: url=trackID=0;seq=17040;rtptime=1467265309
+>//seq和rtptime都是rtp包中的信息 + + ### TEARDOWN + + 客户端发起关闭请求: + +>TEARDOWN rtsp://192.168.20.136:5000/xxx666 RTSP/1.0
+>CSeq: 5
+>Session: 6310936469860791894
+>User-Agent: VLC media player (LIVE555 Streaming Media v2005.11.10) + +服务器回应: + +>RTSP/1.0 200 OK
+>Server: UServer 0.9.7_rc1
+>Cseq: 5
+>Session: 6310936469860791894 + +## SDP协议 + +sdp的格式: + +>v=
+>o=

+>s=
+>i=
+>u=
+>e=
+>p=
+>c=

+>b=:
+>t=
+>r=
+>z= ....
+>k=
+>k=:
+>a=
+>a=:
+>m= + +>v = (协议版本)
+>o = (所有者/创建者和会话标识符)
+>s = (会话名称)
+>i = * (会话信息)
+>u = * (URI 描述)
+>e = * (Email 地址)
+>p = * (电话号码)
+>c = * (连接信息)
+>b = * (带宽信息)
+>z = * (时间区域调整)
+>k = * (加密密钥)
+>a = * (0 个或多个会话属性行) + +* 时间描述: +t = (会话活动时间) +r = * (0或多次重复次数) + +* 媒体描述: +m = (媒体名称和传输地址) +i = * (媒体标题) +c = * (连接信息 — 如果包含在会话层则该字段可选) +b = * (带宽信息) +k = * (加密密钥) +a = * (0 个或多个媒体属性行) + +SDP一会话描述协议一描述SAP、SIP和RTSR会话的协议,是一种文件描述协议,是由服务器生成的描述媒体文件编码信息以及所在服务器的链接等的信息。在多媒体会话 中sDP传送有关媒体流的信息,使会话描述的参人方加人会话。SDP主要用于Intemet网中,但也可以在其它网络环境下使用。SDP十分通用,可描述其它网络环境中的会话,但主要用 于Intemet中。在Intemet环境下,SDP有两个主要目的:一是表明会话存在,二是传送足够信息给接收方,以便能加人、参加该会话。SDP所传达的信息包括:会话名称和目的,会话 活动时间,组成会话媒体种类,接收这些媒体的控制信息(如地址、端口、格式、带宽和会议管理人员资料等)。 + +总结:在RTSP交互过程中,只要在客户端发出Describe请求的时候,服务端回应的时候会有SDP消息发出,用SDP来描述会话情况和内容,方便客户端能够加入该会话。 + + +## RTSP基于libcurl代码实现 + +```C++ +/* + * A basic RTSP transfer + * + */ + +#include +#include +#include +#include + +#if defined (WIN32) +#include /* _getch() */ +#else +#include +#include + +#define VERSION_STR "V1.0" + +/* error handling macros */ +#define my_curl_easy_setopt(A, B, C) \ + res = curl_easy_setopt((A), (B), (C)); \ + if(!res) \ + fprintf(stderr, "curl_easy_setopt(%s, %s, %s) failed: %d\n", \ + #A, #B, #C, res); + +#define my_curl_easy_perform(A) \ + res = curl_easy_perform(A); \ + if(!res) \ + fprintf(stderr, "curl_easy_perform(%s) failed: %d\n", #A, res); + +static int _getch(void) +{ + struct termios oldt, newt; + int ch; + tcgetattr(STDIN_FILENO, &oldt); + newt = oldt; + newt.c_lflag &= ~( ICANON | ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &newt); + ch = getchar(); + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); + return ch; +} +#endif + +/* send RTSP OPTIONS request */ +static void rtsp_options(CURL *curl, const char *uri) +{ + CURLcode res = CURLE_OK; + printf("\nRTSP: OPTIONS %s\n", uri); + my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri); + my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_OPTIONS); + my_curl_easy_perform(curl); +} + +/* send RTSP DESCRIBE request and write sdp response to a file */ +static void rtsp_describe(CURL *curl, const char *uri, + const char *sdp_filename) +{ + CURLcode res = CURLE_OK; + FILE *sdp_fp = fopen(sdp_filename, "wb"); + printf("\nRTSP: DESCRIBE %s\n", uri); + if(sdp_fp == NULL) { + fprintf(stderr, "Could not open '%s' for writing\n", sdp_filename); + sdp_fp = stdout; + } + else { + printf("Writing SDP to '%s'\n", sdp_filename); + } + my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, sdp_fp); + my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_DESCRIBE); + my_curl_easy_perform(curl); + my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout); + if(sdp_fp != stdout) { + fclose(sdp_fp); + } +} + +/* send RTSP SETUP request */ +static void rtsp_setup(CURL *curl, const char *uri, const char *transport) +{ + CURLcode res = CURLE_OK; + printf("\nRTSP: SETUP %s\n", uri); + printf(" TRANSPORT %s\n", transport); + my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri); + my_curl_easy_setopt(curl, CURLOPT_RTSP_TRANSPORT, transport); + my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_SETUP); + my_curl_easy_perform(curl); +} + +/* send RTSP PLAY request */ +static void rtsp_play(CURL *curl, const char *uri, const char *range) +{ + CURLcode res = CURLE_OK; + printf("\nRTSP: PLAY %s\n", uri); + my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri); + my_curl_easy_setopt(curl, CURLOPT_RANGE, range); + my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_PLAY); + my_curl_easy_perform(curl); +} + +/* send RTSP TEARDOWN request */ +static void rtsp_teardown(CURL *curl, const char *uri) +{ + CURLcode res = CURLE_OK; + printf("\nRTSP: TEARDOWN %s\n", uri); + my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_TEARDOWN); + my_curl_easy_perform(curl); +} + +/* convert url into an sdp filename */ +static void get_sdp_filename(const char *url, char *sdp_filename, + size_t namelen) +{ + const char *s = strrchr(url, '/'); + strcpy(sdp_filename, "video.sdp"); + if(s != NULL) { + s++; + if(s[0] != '\0') { + snprintf(sdp_filename, namelen, "%s.sdp", s); + } + } +} + +/* scan sdp file for media control attribute */ +static void get_media_control_attribute(const char *sdp_filename, + char *control) +{ + int max_len = 256; + char *s = malloc(max_len); + FILE *sdp_fp = fopen(sdp_filename, "rb"); + control[0] = '\0'; + if(sdp_fp != NULL) { + while(fgets(s, max_len - 2, sdp_fp) != NULL) { + sscanf(s, " a = control: %s", control); + } + fclose(sdp_fp); + } + free(s); +} + +/* main app */ +int main(int argc, char * const argv[]) +{ +#if 1 + const char *transport = "RTP/AVP;unicast;client_port=1234-1235"; /* UDP */ +#else + /* TCP */ + const char *transport = "RTP/AVP/TCP;unicast;client_port=1234-1235"; +#endif + const char *range = "0.000-"; + int rc = EXIT_SUCCESS; + char *base_name = NULL; + + printf("\nRTSP request %s\n", VERSION_STR); + printf(" Project web site: http://code.google.com/p/rtsprequest/\n"); + printf(" Requires curl V7.20 or greater\n\n"); + + /* check command line */ + if((argc != 2) && (argc != 3)) { + base_name = strrchr(argv[0], '/'); + if(base_name == NULL) { + base_name = strrchr(argv[0], '\\'); + } + if(base_name == NULL) { + base_name = argv[0]; + } + else { + base_name++; + } + printf("Usage: %s url [transport]\n", base_name); + printf(" url of video server\n"); + printf(" transport (optional) specifier for media stream" + " protocol\n"); + printf(" default transport: %s\n", transport); + printf("Example: %s rtsp://192.168.0.2/media/video1\n\n", base_name); + rc = EXIT_FAILURE; + } + else { + const char *url = argv[1]; + char *uri = malloc(strlen(url) + 32); + char *sdp_filename = malloc(strlen(url) + 32); + char *control = malloc(strlen(url) + 32); + CURLcode res; + get_sdp_filename(url, sdp_filename, strlen(url) + 32); + if(argc == 3) { + transport = argv[2]; + } + + /* initialize curl */ + res = curl_global_init(CURL_GLOBAL_ALL); + if(res == CURLE_OK) { + curl_version_info_data *data = curl_version_info(CURLVERSION_NOW); + CURL *curl; + fprintf(stderr, " curl V%s loaded\n", data->version); + + /* initialize this curl session */ + curl = curl_easy_init(); + if(curl != NULL) { + my_curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); + my_curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); + my_curl_easy_setopt(curl, CURLOPT_HEADERDATA, stdout); + my_curl_easy_setopt(curl, CURLOPT_URL, url); + + /* request server options */ + snprintf(uri, strlen(url) + 32, "%s", url); + rtsp_options(curl, uri); + + /* request session description and write response to sdp file */ + rtsp_describe(curl, uri, sdp_filename); + + /* get media control attribute from sdp file */ + get_media_control_attribute(sdp_filename, control); + + /* setup media stream */ + snprintf(uri, strlen(url) + 32, "%s/%s", url, control); + rtsp_setup(curl, uri, transport); + + /* start playing media stream */ + snprintf(uri, strlen(url) + 32, "%s/", url); + rtsp_play(curl, uri, range); + printf("Playing video, press any key to stop ..."); + _getch(); + printf("\n"); + + /* teardown session */ + rtsp_teardown(curl, uri); + + /* cleanup */ + curl_easy_cleanup(curl); + curl = NULL; + } + else { + fprintf(stderr, "curl_easy_init() failed\n"); + } + curl_global_cleanup(); + } + else { + fprintf(stderr, "curl_global_init(%s) failed: %d\n", + "CURL_GLOBAL_ALL", res); + } + free(control); + free(sdp_filename); + free(uri); + } + + return rc; +} +``` + + +
+
+
+
+
+
+
+
+
+
+ + +原文作者: lory17 diff --git a/article/037-视音频数据处理入门:RGB、YUV像素数据处理.md b/article/037-视音频数据处理入门:RGB、YUV像素数据处理.md deleted file mode 100644 index a743e49..0000000 --- a/article/037-视音频数据处理入门:RGB、YUV像素数据处理.md +++ /dev/null @@ -1,890 +0,0 @@ -# 视音频数据处理入门:RGB、YUV像素数据处理 - -本文记录RGB/YUV视频像素数据的处理方法。视频像素数据在视频播放器的解码流程中的位置如下图所示。 - -![image](https://user-images.githubusercontent.com/87458342/127621050-4f585dde-fea8-4524-a964-c4a7da92c942.png) - -本文分别介绍如下几个RGB/YUV视频像素数据处理函数: - 分离YUV420P像素数据中的Y、U、V分量 - 分离YUV444P像素数据中的Y、U、V分量 - 将YUV420P像素数据去掉颜色(变成灰度图) - 将YUV420P像素数据的亮度减半 - 将YUV420P像素数据的周围加上边框 - 生成YUV420P格式的灰阶测试图 - 计算两个YUV420P像素数据的PSNR - 分离RGB24像素数据中的R、G、B分量 - 将RGB24格式像素数据封装为BMP图像 - 将RGB24格式像素数据转换为YUV420P格式像素数据 - 生成RGB24格式的彩条测试图 -本文中的RGB/YUV文件需要使用RGB/YUV播放器才能查看。YUV播放器种类比较多,例如YUV Player Deluxe,或者开源播放器(参考文章《修改了一个YUV/RGB播放器》)等。 - -## 1. 函数列表 - -### 1.1 分离YUV420P像素数据中的Y、U、V分量 - -本程序中的函数可以将YUV420P数据中的Y、U、V三个分量分离开来并保存成三个文件。函数的代码如下所示。 - -```C++ -/** - * Split Y, U, V planes in YUV420P file. - * @param url Location of Input YUV file. - * @param w Width of Input YUV file. - * @param h Height of Input YUV file. - * @param num Number of frames to process. - * - */ -int simplest_yuv420_split(char *url, int w, int h,int num){ - FILE *fp=fopen(url,"rb+"); - FILE *fp1=fopen("output_420_y.y","wb+"); - FILE *fp2=fopen("output_420_u.y","wb+"); - FILE *fp3=fopen("output_420_v.y","wb+"); - - unsigned char *pic=(unsigned char *)malloc(w*h*3/2); - - for(int i=0;i* output_420_y.y:纯Y数据,分辨率为256x256。 ->* output_420_u.y:纯U数据,分辨率为128x128。 ->* output_420_v.y:纯V数据,分辨率为128x128。 - -注:本文中像素的采样位数一律为8bit。由于1Byte=8bit,所以一个像素的一个分量的采样值占用1Byte。 - -程序输入的原图如下所示。 - -![image](https://user-images.githubusercontent.com/87458342/127621291-777790a8-39c9-4b15-9c99-0352dd6d4262.png) - -lena_256x256_yuv420p.yuv - -程序输出的三个文件的截图如下图所示。在这里需要注意输出的U、V分量在YUV播放器中也是当做Y分量进行播放的。 - -![image](https://user-images.githubusercontent.com/87458342/127621328-1249e21d-bf32-4aba-a450-5e4c4bca70ec.png) - -![image](https://user-images.githubusercontent.com/87458342/127621359-f290504c-56c8-4bc9-8be9-09abd63665fa.png) - -### 1.2 分离YUV444P像素数据中的Y、U、V分量 - -本程序中的函数可以将YUV444P数据中的Y、U、V三个分量分离开来并保存成三个文件。函数的代码如下所示。 - -```C++ -/** - * Split Y, U, V planes in YUV444P file. - * @param url Location of YUV file. - * @param w Width of Input YUV file. - * @param h Height of Input YUV file. - * @param num Number of frames to process. - * - */ -int simplest_yuv444_split(char *url, int w, int h,int num){ - FILE *fp=fopen(url,"rb+"); - FILE *fp1=fopen("output_444_y.y","wb+"); - FILE *fp2=fopen("output_444_u.y","wb+"); - FILE *fp3=fopen("output_444_v.y","wb+"); - unsigned char *pic=(unsigned char *)malloc(w*h*3); - - for(int i=0;i* output_444_y.y:纯Y数据,分辨率为256x256。 ->* output_444_u.y:纯U数据,分辨率为256x256。 ->* output_444_v.y:纯V数据,分辨率为256x256。 - -输入的原图如下所示。 -![image](https://user-images.githubusercontent.com/87458342/127621621-55a4cdf2-96c0-40dc-8489-1753bb60630f.png) -输出的三个文件的截图如下图所示。 -![image](https://user-images.githubusercontent.com/87458342/127621903-97a52af8-98fa-4019-85e1-5bf8501d60d7.png) -output_444_y.y -![image](https://user-images.githubusercontent.com/87458342/127621932-b5ab3d43-9b17-4de3-82ba-bd877e87f7c4.png) -output_444_u.y -![image](https://user-images.githubusercontent.com/87458342/127621959-a5f51091-a3dc-4a67-ad40-a127774c56b9.png) -output_444_v.y - -### 1.3 将YUV420P像素数据去掉颜色(变成灰度图) - -本程序中的函数可以将YUV420P格式像素数据的彩色去掉,变成纯粹的灰度图。函数的代码如下。 -```C++ -/** - * Convert YUV420P file to gray picture - * @param url Location of Input YUV file. - * @param w Width of Input YUV file. - * @param h Height of Input YUV file. - * @param num Number of frames to process. - */ -int simplest_yuv420_gray(char *url, int w, int h,int num){ - FILE *fp=fopen(url,"rb+"); - FILE *fp1=fopen("output_gray.yuv","wb+"); - unsigned char *pic=(unsigned char *)malloc(w*h*3/2); - - for(int i=0;i(w-border)||j(h-border)){ - pic[j*w+k]=255; - //pic[j*w+k]=0; - } - } - } - fwrite(pic,1,w*h*3/2,fp1); - } - - free(pic); - fclose(fp); - fclose(fp1); - - return 0; -} -``` - -调用上面函数的方法如下所示。 - -```C++ -simplest_yuv420_border("lena_256x256_yuv420p.yuv",256,256,20,1); -``` - -从代码可以看出,图像的边框的宽度为border,本程序将距离图像边缘border范围内的像素的亮度分量Y的取值设置成了亮度最大值255。上述调用函数的代码运行后,将会把一张分辨率为256x256的名称为lena_256x256_yuv420p.yuv的YUV420P格式的像素数据文件处理成名称为output_border.yuv的YUV420P格式的像素数据文件。输入的原图如下所示。 - -![image](https://user-images.githubusercontent.com/87458342/127622416-47a62f52-3000-4e60-bf77-b15a2920b1c4.png) - -处理后的图像如下所示。 - -![image](https://user-images.githubusercontent.com/87458342/127622443-48b4a985-22b4-40f9-88a4-dfd7bf976b33.png) - -### 1.6 生成YUV420P格式的灰阶测试图 - -本程序中的函数可以生成一张YUV420P格式的灰阶测试图。函数代码如下所示。 - -```C++ -/** - * Generate YUV420P gray scale bar. - * @param width Width of Output YUV file. - * @param height Height of Output YUV file. - * @param ymin Max value of Y - * @param ymax Min value of Y - * @param barnum Number of bars - * @param url_out Location of Output YUV file. - */ -int simplest_yuv420_graybar(int width, int height,int ymin,int ymax,int barnum,char *url_out){ - int barwidth; - float lum_inc; - unsigned char lum_temp; - int uv_width,uv_height; - FILE *fp=NULL; - unsigned char *data_y=NULL; - unsigned char *data_u=NULL; - unsigned char *data_v=NULL; - int t=0,i=0,j=0; - - barwidth=width/barnum; - lum_inc=((float)(ymax-ymin))/((float)(barnum-1)); - uv_width=width/2; - uv_height=height/2; - - data_y=(unsigned char *)malloc(width*height); - data_u=(unsigned char *)malloc(uv_width*uv_height); - data_v=(unsigned char *)malloc(uv_width*uv_height); - - if((fp=fopen(url_out,"wb+"))==NULL){ - printf("Error: Cannot create file!"); - return -1; - } - - //Output Info - printf("Y, U, V value from picture's left to right:\n"); - for(t=0;t<(width/barwidth);t++){ - lum_temp=ymin+(char)(t*lum_inc); - printf("%3d, 128, 128\n",lum_temp); - } - //Gen Data - for(j=0;j* output_r.y:R数据,分辨率为256x256。 ->* output_g.y:G数据,分辨率为256x256。 ->* output_b.y:B数据,分辨率为256x256。 - -输入的原图是一张标准的CIE 1931色度图。该色度图右下为红色,上方为绿色,左下为蓝色,如下所示。 - -![image](https://user-images.githubusercontent.com/87458342/127623095-009a68ec-8379-40bc-916e-94dd6c734390.png) - -R数据图像如下所示。 - -![image](https://user-images.githubusercontent.com/87458342/127623123-cd9af91a-4e59-4ef5-bf1a-8b2faa0afc72.png) - -G数据图像如下所示。 - -![image](https://user-images.githubusercontent.com/87458342/127623145-0eb999d2-08cb-4489-a542-1b91fdcebaac.png) - -B数据图像如下所示。 - -![image](https://user-images.githubusercontent.com/87458342/127623175-8b4d140b-a758-4ad0-b998-7040d22e2f19.png) - -### 1.9 将RGB24格式像素数据封装为BMP图像 - -BMP图像内部实际上存储的就是RGB数据。本程序实现了对RGB像素数据的封装处理。通过本程序中的函数,可以将RGB数据封装成为一张BMP图像。 - -```C++ -/** - * Convert RGB24 file to BMP file - * @param rgb24path Location of input RGB file. - * @param width Width of input RGB file. - * @param height Height of input RGB file. - * @param url_out Location of Output BMP file. - */ -int simplest_rgb24_to_bmp(const char *rgb24path,int width,int height,const char *bmppath){ - typedef struct - { - long imageSize; - long blank; - long startPosition; - }BmpHead; - - typedef struct - { - long Length; - long width; - long height; - unsigned short colorPlane; - unsigned short bitColor; - long zipFormat; - long realSize; - long xPels; - long yPels; - long colorUse; - long colorImportant; - }InfoHead; - - int i=0,j=0; - BmpHead m_BMPHeader={0}; - InfoHead m_BMPInfoHeader={0}; - char bfType[2]={'B','M'}; - int header_size=sizeof(bfType)+sizeof(BmpHead)+sizeof(InfoHead); - unsigned char *rgb24_buffer=NULL; - FILE *fp_rgb24=NULL,*fp_bmp=NULL; - - if((fp_rgb24=fopen(rgb24path,"rb"))==NULL){ - printf("Error: Cannot open input RGB24 file.\n"); - return -1; - } - if((fp_bmp=fopen(bmppath,"wb"))==NULL){ - printf("Error: Cannot open output BMP file.\n"); - return -1; - } - - rgb24_buffer=(unsigned char *)malloc(width*height*3); - fread(rgb24_buffer,1,width*height*3,fp_rgb24); - - m_BMPHeader.imageSize=3*width*height+header_size; - m_BMPHeader.startPosition=header_size; - - m_BMPInfoHeader.Length=sizeof(InfoHead); - m_BMPInfoHeader.width=width; - //BMP storage pixel data in opposite direction of Y-axis (from bottom to top). - m_BMPInfoHeader.height=-height; - m_BMPInfoHeader.colorPlane=1; - m_BMPInfoHeader.bitColor=24; - m_BMPInfoHeader.realSize=3*width*height; - - fwrite(bfType,1,sizeof(bfType),fp_bmp); - fwrite(&m_BMPHeader,1,sizeof(m_BMPHeader),fp_bmp); - fwrite(&m_BMPInfoHeader,1,sizeof(m_BMPInfoHeader),fp_bmp); - - //BMP save R1|G1|B1,R2|G2|B2 as B1|G1|R1,B2|G2|R2 - //It saves pixel data in Little Endian - //So we change 'R' and 'B' - for(j =0;j* BITMAPFILEHEADER ->* BITMAPINFOHEADER ->* RGB像素数据 - -其中前两部分的结构如下所示。在写入BMP文件头的时候给其中的每个字段赋上合适的值就可以了。 - -```C++ -typedef struct tagBITMAPFILEHEADER -{ -unsigned short int bfType; //位图文件的类型,必须为BM -unsigned long bfSize; //文件大小,以字节为单位 -unsigned short int bfReserverd1; //位图文件保留字,必须为0 -unsigned short int bfReserverd2; //位图文件保留字,必须为0 -unsigned long bfbfOffBits; //位图文件头到数据的偏移量,以字节为单位 -}BITMAPFILEHEADER; -typedef struct tagBITMAPINFOHEADER -{ -long biSize; //该结构大小,字节为单位 -long biWidth; //图形宽度以象素为单位 -long biHeight; //图形高度以象素为单位 -short int biPlanes; //目标设备的级别,必须为1 -short int biBitcount; //颜色深度,每个象素所需要的位数 -short int biCompression; //位图的压缩类型 -long biSizeImage; //位图的大小,以字节为单位 -long biXPelsPermeter; //位图水平分辨率,每米像素数 -long biYPelsPermeter; //位图垂直分辨率,每米像素数 -long biClrUsed; //位图实际使用的颜色表中的颜色数 -long biClrImportant; //位图显示过程中重要的颜色数 -}BITMAPINFOHEADER; -``` - -BMP采用的是小端(Little Endian)存储方式。这种存储方式中“RGB24”格式的像素的分量存储的先后顺序为B、G、R。由于RGB24格式存储的顺序是R、G、B,所以需要将“R”和“B”顺序作一个调换再进行存储。 -下图为输入的RGB24格式的图像lena_256x256_rgb24.rgb。 - -![image](https://user-images.githubusercontent.com/87458342/127623445-0ddb76e2-8377-4d58-a78a-d44cf546e7be.png) - -下图分封装为BMP格式后的图像output_lena.bmp。封装后的图像使用普通的看图软件就可以查看。 - -![image](https://user-images.githubusercontent.com/87458342/127623473-885478d0-137b-4141-93a5-3c73802706ae.png) - -### 1.10 将RGB24格式像素数据转换为YUV420P格式像素数据 - -本程序中的函数可以将RGB24格式的像素数据转换为YUV420P格式的像素数据。函数的代码如下所示。 - -```C++ -unsigned char clip_value(unsigned char x,unsigned char min_val,unsigned char max_val){ - if(x>max_val){ - return max_val; - }else if(x> 8) + 16 ; - u = (unsigned char)( ( -38 * r - 74 * g + 112 * b + 128) >> 8) + 128 ; - v = (unsigned char)( ( 112 * r - 94 * g - 18 * b + 128) >> 8) + 128 ; - *(ptrY++) = clip_value(y,0,255); - if (j%2==0&&i%2 ==0){ - *(ptrU++) =clip_value(u,0,255); - } - else{ - if (i%2==0){ - *(ptrV++) =clip_value(v,0,255); - } - } - } - } - return true; -} - -/** - * Convert RGB24 file to YUV420P file - * @param url_in Location of Input RGB file. - * @param w Width of Input RGB file. - * @param h Height of Input RGB file. - * @param num Number of frames to process. - * @param url_out Location of Output YUV file. - */ -int simplest_rgb24_to_yuv420(char *url_in, int w, int h,int num,char *url_out){ - FILE *fp=fopen(url_in,"rb+"); - FILE *fp1=fopen(url_out,"wb+"); - - unsigned char *pic_rgb24=(unsigned char *)malloc(w*h*3); - unsigned char *pic_yuv420=(unsigned char *)malloc(w*h*3/2); - - for(int i=0;i -
-
-
-
-
-
-
-
-
-
-
- - -原文作者: 雷霄骅