分享一段H264视频和AAC音频的RTP封包代码
1. H264视频的RTP封包
- static int h264_parse(Track *tr, uint8_t *data, size_t len)
- {
- h264_priv *priv = tr->private_data;
- // double nal_time; // see page 9 and 7.4.1.2
- size_t nalsize = 0, index = 0;
- uint8_t *p, *q;
- if (priv->is_avc) {
- while (1) {
- unsigned int i;
- if(index >= len) break;
- //get the nal size
- nalsize = 0;
- for(i = 0; i < priv->nal_length_size; i++)
- nalsize = (nalsize << 8) | data[index++];
- if(nalsize <= 1 || nalsize > len) {
- if(nalsize == 1) {
- index++;
- continue;
- } else {
- fnc_log(FNC_LOG_VERBOSE, "[h264] AVC: nal size %d", nalsize);
- break;
- }
- }
- if (DEFAULT_MTU >= nalsize) {
- mparser_buffer_write(tr,
- tr->properties.pts,
- tr->properties.dts,
- tr->properties.frame_duration,
- 1,
- data + index, nalsize);
- fnc_log(FNC_LOG_VERBOSE, "[h264] single NAL");
- } else {
- // single NAL, to be fragmented, FU-A;
- frag_fu_a(data + index, nalsize, DEFAULT_MTU, tr);
- }
- index += nalsize;
- }
- } else {
- //seek to the first startcode
- for (p = data; p<data + len - 3; p++) {
- if (p[0] == 0 && p[1] == 0 && p[2] == 1) {
- break;
- }
- }
- if (p >= data + len) return ERR_PARSE;
- while (1) {
- //seek to the next startcode [0 0 1]
- for (q = p; q<data+len-3;q++) {
- if (q[0] == 0 && q[1] == 0 && q[2] == 1) {
- break;
- }
- }
- if (q >= data + len) break;
- if (DEFAULT_MTU >= q - p) {
- fnc_log(FNC_LOG_VERBOSE, "[h264] Sending NAL %d",p[0]&0x1f);
- mparser_buffer_write(tr,
- tr->properties.pts,
- tr->properties.dts,
- tr->properties.frame_duration,
- 1,
- p, q - p);
- fnc_log(FNC_LOG_VERBOSE, "[h264] single NAL");
- } else {
- //FU-A
- fnc_log(FNC_LOG_VERBOSE, "[h264] frags");
- frag_fu_a(p, q - p, DEFAULT_MTU, tr);
- }
- p = q;
- }
- // last NAL
- fnc_log(FNC_LOG_VERBOSE, "[h264] last NAL %d",p[0]&0x1f);
- if (DEFAULT_MTU >= len - (p - data)) {
- fnc_log(FNC_LOG_VERBOSE, "[h264] no frags");
- mparser_buffer_write(tr,
- tr->properties.pts,
- tr->properties.dts,
- tr->properties.frame_duration,
- 1,
- p, len - (p - data));
- } else {
- //FU-A
- fnc_log(FNC_LOG_VERBOSE, "[h264] frags");
- frag_fu_a(p, len - (p - data), DEFAULT_MTU, tr);
- }
- }
- fnc_log(FNC_LOG_VERBOSE, "[h264] Frame completed");
- return ERR_NOERROR;
- }
- static int h264_parse(Track *tr, uint8_t *data, size_t len)
- {
- h264_priv *priv = tr->private_data;
- // double nal_time; // see page 9 and 7.4.1.2
- size_t nalsize = 0, index = 0;
- uint8_t *p, *q;
- if (priv->is_avc) {
- while (1) {
- unsigned int i;
- if(index >= len) break;
- //get the nal size
- nalsize = 0;
- for(i = 0; i < priv->nal_length_size; i++)
- nalsize = (nalsize << 8) | data[index++];
- if(nalsize <= 1 || nalsize > len) {
- if(nalsize == 1) {
- index++;
- continue;
- } else {
- fnc_log(FNC_LOG_VERBOSE, "[h264] AVC: nal size %d", nalsize);
- break;
- }
- }
- if (DEFAULT_MTU >= nalsize) {
- mparser_buffer_write(tr,
- tr->properties.pts,
- tr->properties.dts,
- tr->properties.frame_duration,
- 1,
- data + index, nalsize);
- fnc_log(FNC_LOG_VERBOSE, "[h264] single NAL");
- } else {
- // single NAL, to be fragmented, FU-A;
- frag_fu_a(data + index, nalsize, DEFAULT_MTU, tr);
- }
- index += nalsize;
- }
- } else {
- //seek to the first startcode
- for (p = data; p<data + len - 3; p++) {
- if (p[0] == 0 && p[1] == 0 && p[2] == 1) {
- break;
- }
- }
- if (p >= data + len) return ERR_PARSE;
- while (1) {
- //seek to the next startcode [0 0 1]
- for (q = p; q<data+len-3;q++) {
- if (q[0] == 0 && q[1] == 0 && q[2] == 1) {
- break;
- }
- }
- if (q >= data + len) break;
- if (DEFAULT_MTU >= q - p) {
- fnc_log(FNC_LOG_VERBOSE, "[h264] Sending NAL %d",p[0]&0x1f);
- mparser_buffer_write(tr,
- tr->properties.pts,
- tr->properties.dts,
- tr->properties.frame_duration,
- 1,
- p, q - p);
- fnc_log(FNC_LOG_VERBOSE, "[h264] single NAL");
- } else {
- //FU-A
- fnc_log(FNC_LOG_VERBOSE, "[h264] frags");
- frag_fu_a(p, q - p, DEFAULT_MTU, tr);
- }
- p = q;
- }
- // last NAL
- fnc_log(FNC_LOG_VERBOSE, "[h264] last NAL %d",p[0]&0x1f);
- if (DEFAULT_MTU >= len - (p - data)) {
- fnc_log(FNC_LOG_VERBOSE, "[h264] no frags");
- mparser_buffer_write(tr,
- tr->properties.pts,
- tr->properties.dts,
- tr->properties.frame_duration,
- 1,
- p, len - (p - data));
- } else {
- //FU-A
- fnc_log(FNC_LOG_VERBOSE, "[h264] frags");
- frag_fu_a(p, len - (p - data), DEFAULT_MTU, tr);
- }
- }
- fnc_log(FNC_LOG_VERBOSE, "[h264] Frame completed");
- return ERR_NOERROR;
- }
2. AAC的RTP封包
- static int aac_parse(Track *tr, uint8_t *data, size_t len)
- {
- //XXX handle the last packet on EOF
- int off = 0;
- uint32_t payload = DEFAULT_MTU - AU_HEADER_SIZE;
- uint8_t *packet = g_malloc0(DEFAULT_MTU);
- if(!packet) return ERR_ALLOC;
- // trim away extradata
- // data += AAC_EXTRA;
- // len -= AAC_EXTRA;
- packet[0] = 0x00;
- packet[1] = 0x10;
- packet[2] = (len & 0x1fe0) >> 5;
- packet[3] = (len & 0x1f) << 3;
- if (len > payload) {
- while (len > payload) {
- memcpy(packet + AU_HEADER_SIZE, data + off, payload);
- mparser_buffer_write(tr,
- tr->properties.pts,
- tr->properties.dts,
- tr->properties.frame_duration,
- 0,
- packet, DEFAULT_MTU);
- len -= payload;
- off += payload;
- }
- }
- memcpy(packet + AU_HEADER_SIZE, data + off, len);
- mparser_buffer_write(tr,
- tr->properties.pts,
- tr->properties.dts,
- tr->properties.frame_duration,
- 1,
- packet, len + AU_HEADER_SIZE);
- g_free(packet);
- return ERR_NOERROR;
- }
- static int aac_parse(Track *tr, uint8_t *data, size_t len)
- {
- //XXX handle the last packet on EOF
- int off = 0;
- uint32_t payload = DEFAULT_MTU - AU_HEADER_SIZE;
- uint8_t *packet = g_malloc0(DEFAULT_MTU);
- if(!packet) return ERR_ALLOC;
- // trim away extradata
- // data += AAC_EXTRA;
- // len -= AAC_EXTRA;
- packet[0] = 0x00;
- packet[1] = 0x10;
- packet[2] = (len & 0x1fe0) >> 5;
- packet[3] = (len & 0x1f) << 3;
- if (len > payload) {
- while (len > payload) {
- memcpy(packet + AU_HEADER_SIZE, data + off, payload);
- mparser_buffer_write(tr,
- tr->properties.pts,
- tr->properties.dts,
- tr->properties.frame_duration,
- 0,
- packet, DEFAULT_MTU);
- len -= payload;
- off += payload;
- }
- }
- memcpy(packet + AU_HEADER_SIZE, data + off, len);
- mparser_buffer_write(tr,
- tr->properties.pts,
- tr->properties.dts,
- tr->properties.frame_duration,
- 1,
- packet, len + AU_HEADER_SIZE);
- g_free(packet);
- return ERR_NOERROR;
- }
上面的变量 AU_HEADER_SIZE=4
/* au header
+---------------------------------------+
| AU-size |
+---------------------------------------+
| AU-Index / AU-Index-delta |
+---------------------------------------+
| CTS-flag |
+---------------------------------------+
| CTS-delta |
+---------------------------------------+
| DTS-flag |
+---------------------------------------+
| DTS-delta |
+---------------------------------------+
| RAP-flag |
+---------------------------------------+
| Stream-state |
+---------------------------------------+
*/
分享一段H264视频和AAC音频的RTP封包代码的更多相关文章
- C#音视频网络流解码:H264视频和ACC音频
下面两种方式是直接翻译过来的,还有问题,比如指针的使用和值的传入.考虑C#和C++的差异,还是要抱着怀疑的态度去看待,不一定是对的. H264视频解码网络流: using FFmpeg.AutoGen ...
- 屏幕录制H.264视频,AAC音频,MP4复,LibRTMP现场活动
上周完成了一个屏幕录制节目,实时屏幕捕获.记录,视频H.264压缩,音频应用AAC压缩,复用MP4格公式,这使得计算机和ios设备上直接播放.支持HTML5的播放器都能够放,这是标准格式的优点.抓屏也 ...
- 基于RTP的H264视频数据打包解包类
from:http://blog.csdn.net/dengzikun/article/details/5807694 最近考虑使用RTP替换原有的高清视频传输协议,遂上网查找有关H264视频RTP打 ...
- 使用ffmpeg将BMP图片编码为x264视频文件,将H264视频保存为BMP图片,yuv视频文件保存为图片的代码
ffmpeg开源库,实现将bmp格式的图片编码成x264文件,并将编码好的H264文件解码保存为BMP文件. 实现将视频文件yuv格式保存的图片格式的測试,图像格式png,jpg, gif等等測试均O ...
- 采集音频和摄像头视频并实时H264编码及AAC编码[转]
0. 前言 我在前两篇文章中写了DirectShow捕获音视频然后生成avi,再进行264编码的方法.那种方法有一些局限性,不适合实时性质的应用,如:视频会议.视频聊天.视频监控等.本文所使用的技术, ...
- 采集音频和摄像头视频并实时H264编码及AAC编码
转自:http://www.cnblogs.com/haibindev/archive/2011/11/10/2244442.html 0. 前言 我在前两篇文章中写了DirectShow捕获音视频然 ...
- 分享一段视频关于SQL2014 Hekaton数据库的
分享一段视频关于SQL2014 Hekaton数据库的 Microsoft SQL Server In-Memory OLTP Project "Hekaton": App Dev ...
- 分享macOS平台好用的视频分割、合并视频、提取音频、分离音频、音频转码的工具CCVideo
CCVideo 是一款运行在macOS上可分割视频(可多段分割).合并视频.提取音频.分离音频.音频转码的工具,操作方便,只需简单几步,便可轻松完成. 下载地址
- 音视频编解码技术(二):AAC 音频编码技术
一.AAC编码概述 AAC是高级音频编码(Advanced Audio Coding)的缩写,出现于1997年,最初是基于MPEG-2的音频编码技术,目的是取代MP3格式.2000年,MPEG-4标准 ...
随机推荐
- DIV+CSS 基础
盒子模型:margin(边界),可被占位:border(边框):padding(填充):content(内容) 块元素: 默认占据一行,前后换行. 作为容器,装载块元素和行内元素,被装载元素的位置,会 ...
- [转载]Windows 7 IIS (HTTP Error 500.21 - Internal Server Error)解决
今天在测试网站的时候,在浏览器中输入http://localhost/时,发生如下错误: HTTP Error 500.21 - Internal Server Error Handler " ...
- Chp14: Java
1.finally keyword: finally keyword is used in association with a try/catch block and guarantees that ...
- IT 基础设施
http://www.cnblogs.com/wintersun/p/4355267.html
- 【mysql的编程专题③】内置函数
数学函数 常用 abs(x) 返回x的绝对值 floor(x) 返回小于x的最大整数值 mod(x,y) 返回x/y的模(余数) rand() 返回0到1内的随机值,可以通过提供一个参数(种子)使ra ...
- Debug过程中的mock (及display窗口的使用)
转载:http://m.blog.csdn.net/blog/u012516903/18004965 在debug的时候,有3个地方可以进行mock测试 测试代码如下: 1.使用display窗口 W ...
- java工具类系列 (四.SerializationUtils)
java工具类系列 (四.SerializationUtils) SerializationUtils该类为序列化工具类,也是lang包下的工具,主要用于序列化操作 import java.io.Se ...
- 专家谈国产CPU最新发展态势:需强化标准建设(很全面)
一.国产CPU发展现状与成就 国内已开启多技术路线并行的CPU技术产业新格局.在国家科技重大专项和国家级集成电路产业投资资金的推动之下,我国CPU产品技术研发已进入多技术路线同步推进的高速发展阶段,并 ...
- HttpServletRequest接口实例化的使用
HttpServletRequ接口的使用和jsp内置对象的request对象非常类似,request对象其实 就是HttpServletRequest接口的一个实例,不过气实例化的过程是自动的,无须自 ...
- shell编程基础(4)case 与 function
case case语句不用多说和c++里一样,用作选择,但是linux的case语句在语法上和c++还是有些不同的. ###########This is program test the Case# ...