mp4 文件中的h264 avc1格式介绍

mp4 文件中的h264 avc1格式介绍转自:http://www.mworkbox.com/wp/work/314.htmlMP4的视频H264封装有2种格式:h264和avc1,对于这个细节,很容易被忽略。笔者也是在改编LIVE555流媒体时,增加mp4文件类型支持时遇到了该问题。(一)首先,从原理上了解一下这2种格式的区别:AVC1描述:H.264bitstreamwithoutstartcode

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

转自:http://www.mworkbox.com/wp/work/314.html

MP4的视频H264封装有2种格式:h264和avc1,对于这个细节,很容易被忽略。笔者也是在改编LIVE555流媒体时,增加mp4文件类型支持时遇到了该问题。

(一)首先,从原理上了解一下这2种格式的区别:
AVC1 描述:H.264 bitstream without start codes.一般通过ffmpeg转码生成的视频,是不带起始码0×00000001的。
H264 描述:H.264 bitstream with start codes.一般对于一下HDVD等电影的压制格式,是带有起始码0×00000001的。
来源文档:http://msdn.microsoft.com/zh-cn/library/dd757808(v=vs.85).aspx
(二)其次,通过VLC播放器,可以查看到具体的格式。打开视频后,通过菜单【工具】/【编解码信息】可以查看到【编解码器】具体格式,举例如下,编解码器信息:
编码: H264 – MPEG-4 AVC (part 10) (avc1)
编码: H264 – MPEG-4 AVC (part 10) (h264)

(三)最后,分享一下ffmpeg demux MP4文件后,转换视频流为live555可直接使用的h264 ES流的经验和方法:
针对(avc1),av_read_frame后,取前四个字节为长度,把前四字节直接替换为0×00,0×00,0×00,0×01即可,但注意每个frame可以有多个NAUL:

  AVPacket pkt
;

    AVPacket
* packet 
= 
&pkt
;

    av_init_packet
(packet
)
;

    av_read_frame
(ctx
, packet
)
;

    

    

    
if
(packet
->stream_index 
== 
0
)

    
{

//is video stream

    

       
const 
char start_code
[
4
] 
= 
{
 
0
, 
0
, 
0
, 
1 
}
;

            
if
(is_avc_ 
|| 
memcmp
(start_code
, packet
->data
, 
4
) 
!= 
0
)

            
{

//is avc1 code, have no start code of H264

                
int len 
= 
0
;

                
uint8_t 
*
= packet
->data
;



                is_avc_ 
= True
;

                
do 

                
{

//add start_code for each NAL, one frame may have multi NALs.

                    len 
= ntohl
(
*
(
(
long
*
)p
)
)
;

                    
memcpy
(p
, start_code
, 
4
)
;



                    p 
+= 
4
;

                    p 
+= len
;

                    
if
(
>= packet
->data 
+ packet
->size
)

                    
{


                        
break
;

                    
}

                
} 
while 
(
1
)
;

            
}

        
}

对于另外一种格式,(h264), 则直接对每个packet调用av_bitstream_filter_filter处理每个packet即可:

  bsfc_ 
= av_bitstream_filter_init
(
“h264_mp4toannexb”
)
;

  

   
if
(pkt
->stream_index 
== 
0
)

   
{

//is video stream

    

      AVBitStreamFilterContext
* bsfc 
= bsfc_
;

        
int a
;

        
while 
(bsfc
) 
{


            AVPacket new_pkt 
= 
*pkt
;

            a 
= av_bitstream_filter_filter
(bsfc
, encode_ctx_
, NULL
,

                
&new_pkt.
data
, 
&new_pkt.
size
,

                pkt
->data
, pkt
->size
,

                pkt
->flags 
& AV_PKT_FLAG_KEY
)
;

            
if
(
== 
0 
&& new_pkt.
data 
!= pkt
->data 
&& new_pkt.
destruct
) 
{


                
uint8_t 
*
= 
(
uint8_t
*
)
(new_pkt.
size 
+ FF_INPUT_BUFFER_PADDING_SIZE
)
; 
//the new should be a subset of the old so cannot overflow

                
if
(t
) 
{


                    
memcpy
(t
, new_pkt.
data
, new_pkt.
size
)
;

                    
memset
(
+ new_pkt.
size
, 
0
, FF_INPUT_BUFFER_PADDING_SIZE
)
;

                    new_pkt.
data 
= t
;

                    a 
= 
1
;

                
} 
else

                    a 
= AVERROR
(ENOMEM
)
;

            
}

            
if 
(
> 
0 
&& pkt
->data 
!= new_pkt.
data
) 
{


                av_free_packet
(pkt
)
;

                new_pkt.
destruct 
= av_destruct_packet
;

            
} 
else 
if 
(
< 
0
) 
{


                envir
(
) 
<< 
“!!!!!!!!!!av_bitstream_filter_filter failed” 
<< 
“,res=” 
<< a 
<< 
\n
;

            
}

            
*pkt 
= new_pkt
;

    

            bsfc 
= bsfc
->next
;

        
}

    
}

分类:技术文章 | 标签: h264码流MP4 demuxmp4 ffmpeg demuxMP4文件两种格式AVC1和H264的区别 | 阅读次数: 2,184

我一直疑问为什么有些视频解码时显示格式是:H264,大部分又是:AVC1
我在搜索编程资料时在微软的msdn上发现的:
原文:http://msdn.microsoft.com/en-us/library/dd757808(v=vs.85).aspx
FOURCC:AVC1   描述:H.264 bitstream without start codes.
FOURCC:H264   描述:H.264 bitstream with start codes.


H.264 Bitstream with Start Codes

H.264 bitstreams that are transmitted over the air, or contained in MPEG-2 program or transport streams, or recorded on HD-DVD, are formatted as described in Annex B of ITU-T Rec. H.264. According to this specification, the bitstream consists of a sequence of network abstraction layer units (NALUs), each of which is prefixed with a start code equal to 0x000001 or 0x00000001.
这段话的大致意思是:带有开始码的H.264视频一般是用于无线发射、有线广播或者HD-DVD中的。这些数据流的开始都有一个开始码:0x000001 或者 0x00000001.


H.264 Bitstream Without Start Codes

The MP4 Container format stores H.264 data without start codes. Instead, each NALU is prefixed by a length field, which gives the length of the NALU in bytes. The size of the length field can vary, but is typically 1, 2, or 4 bytes.
这段话的大致意思是:没有开始码的H.264视频主要是存储在MP4格式的文件中的。它的数据流的开始是1、2或者4个字节表示长度数据。

原文中的”NALU”简单说是H.264格式中的最基本的单元,是一个数据包。

http://www.mysilu.com/archiver/?tid-721741.html


以下转自:https://msdn.microsoft.com/zh-cn/library/dd757808(v=vs.85).aspx

EN
此内容没有您的语言版本,但有英语版本。

H.264 Video Types

The following media subtypes are defined for H.264 video.

Subtype FOURCC Description
MEDIASUBTYPE_AVC1 ‘AVC1’ H.264 bitstream without start codes.
MEDIASUBTYPE_H264 ‘H264’ H.264 bitstream with start codes.
MEDIASUBTYPE_h264 ‘h264’ Equivalent to MEDIASUBTYPE_H264, with a different FOURCC.
MEDIASUBTYPE_X264 ‘X264’ Equivalent to MEDIASUBTYPE_H264, with a different FOURCC.
MEDIASUBTYPE_x264 ‘x264’ Equivalent to MEDIASUBTYPE_H264, with a different FOURCC.

 

These subtype GUIDs are declared in wmcodecdsp.h.

The main difference between these media types is the presence of start codes in the bitstream. If the subtype is MEDIASUBTYPE_AVC1, the bitstream does not contain start codes.

H.264 Bitstream with Start Codes

H.264 bitstreams that are transmitted over the air, or contained in MPEG-2 program or transport streams, or recorded on HD-DVD, are formatted as described in Annex B of ITU-T Rec. H.264. According to this specification, the bitstream consists of a sequence of network abstraction layer units (NALUs), each of which is prefixed with a start code equal to 0x000001 or 0x00000001.

When start codes are present in the bitstream, the following media type is used:

Major type MEDIATYPE_Video
Subtypes MEDIASUBTYPE_H264MEDIASUBTYPE_h264MEDIASUBTYPE_X264, or MEDIASUBTYPE_x264
Format type FORMAT_VideoInfoFORMAT_VideoInfo2FORMAT_MPEG2Video, or GUID_NULL

 

If the format type is GUID_NULL, no format structure is present.

When the bitstream contains start codes, any of the format types listed here is sufficient, because the decoder does not require any additional information to parse the stream. The bitstream already contains all of the information needed by the decoder, and the start codes enable the decoder to locate the start of each NALU.

The following subtypes are equivalent:

H.264 Bitstream Without Start Codes

The MP4 container format stores H.264 data without start codes. Instead, each NALU is prefixed by a length field, which gives the length of the NALU in bytes. The size of the length field can vary, but is typically 1, 2, or 4 bytes.

When start codes are not present in the bitstream, the following media type is used.

Major type MEDIATYPE_Video
Subtype MEDIASUBTYPE_AVC1
Format type FORMAT_MPEG2Video

 

The format block is an MPEG2VIDEOINFO structure. This structure should be filled in as follows:

  • hdr: A VIDEOINFOHEADER2 structure that describes the bitstream. No color table is present after the BITMAPINFOHEADERportion of the structure, and biClrUsed must be zero.
  • dwStartTimeCode: Not used. Set to zero.
  • cbSequenceHeader: The length of the dwSequenceHeader array in bytes.
  • dwProfile: Specifies the H.264 profile.
  • dwLevel: Specifies the H.264 level.
  • dwFlags: The number of bytes used for the length field that appears before each NALU. The length field indicates the size of the following NALU in bytes. For example, if dwFlags is 4, each NALU is preceded by a 4-byte length field. The valid values are 1, 2, and 4.
  • dwSequenceHeader: A byte array that may contain sequence parameter set (SPS) and picture parameter set (PPS) NALUs.

The MP4 container might contain sequence parameter sets (SPS) or picture parameter sets (PPS) as special NAL units in file headers or in a separate stream (distinct from the video stream). When the format is established, the media type can specify SPS and PPS NAL units in the dwSequenceHeader array. If cbSequenceHeader is greater than zero, dwSequenceHeader is the start of a byte array containing SPS and PPS NALUs, delimited by 2-byte length fields, all in network byte order (big-endian). It is possible to have both SPS and PPS, only one of these types, or none. The actual type of each NALU can be determined by examining the nal_unit_type field of the NALU itself.

When this media type is used, each media sample starts at the beginning of a NALU, and NAL units do not span samples. This enables the decoder to recover from data corruption or dropped samples.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/181112.html原文链接:https://javaforall.net

(0)
上一篇 2022年10月17日 上午8:36
下一篇 2022年10月17日 上午8:36


相关推荐

  • Python处理xml文件_文件格式怎么转换

    Python处理xml文件_文件格式怎么转换由于项目组中原来的文件使用的XML格式作为配置,扩展性很好,但是编辑与阅读不是很直观,特别一些规则的二维表,所以为了方便阅读与编辑,花了一些时间写了一个Python脚本,以实现将XML文件转为Excel文件。这里支持XML文件转为一个Sheet或者多个Sheet:如果第二层所有标签都相同则会转为一个Sheet,所有第二层的标签都会作为行数据如果第二层的标签有多种,则会把第二层的不同标签作为…

    2022年8月22日
    7
  • struts2的response的contentType设置

    struts2的response的contentType设置服务器端发送到客户端,提示保存还是打开?       response.setContentType(“text/html;charset=utf-8”)发送到客户端格式不正确。使用ajaxUpload插件的时候,strust2返回application/json格式的数据,但是ajaxUpload要求返回text/html,这样就需要在配置文件中配置contentType项。

    2022年7月19日
    18
  • 使用arpspoof进行ARP欺骗[通俗易懂]

    使用arpspoof进行ARP欺骗[通俗易懂]使用arpspoof进行ARP欺骗使用虚拟机上的kail进行测试基本原理我们将运行实际的ARP中毒攻击,重定向数据包流并使其流经我们的设备基本命令arpspooef-i网卡-t目标ip默认网关测试下面是我作为被攻击的kail,ip为192.168.25.129下面是我作为攻击的kail,网卡对应ip,网卡名字为eth0,ip为192.168.25.128使用上面…

    2022年10月7日
    5
  • pycharm2021.11.2激活[最新免费获取]

    (pycharm2021.11.2激活)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~09LVN3XLKC-eyJsaWNlbnNlSWQiOi…

    2022年3月28日
    47
  • 利用树莓派搭建 web 服务器 (个人认为是网上步骤最全,也是最新的方式了 使用 PHP7)[通俗易懂]

    利用树莓派搭建 web 服务器 (个人认为是网上步骤最全,也是最新的方式了 使用 PHP7)[通俗易懂]#前言在暑假的时候想玩玩树莓派,就买了一块树莓派3B+,结果买回来也没太玩就放在宿舍吃灰,最近突然对网站很感兴趣,于是就在网上查找资料去搭建了这个web服务器,它是使用的nginx+PHP7+typecho组成的服务器。#首先安装raspbian系统引用了树莓派实验室的下载地址,大家可以直接下载。下载链接:http://downloads.raspberrypi.org/raspbian_…

    2022年6月6日
    35
  • 豆包怎么开启吵架模式

    豆包怎么开启吵架模式

    2026年3月12日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号