解决方案

mp4 文件中的h264 avc1格式介绍

seo靠我 2023-09-23 07:02:22

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

(一)首先,从原理上了解一下这2种格式的SEO靠我区别:

AVC1 描述:H.264 bitstream without start codes.一般通过ffmpeg转码生成的视频,是不带起始码0×00000001的。

H264 描述:H.264 bitSEO靠我stream with start codes.一般对于一下HDVD等电影的压制格式,是带有起始码0×00000001的。

来源文档:http://msdn.microsoft.com/zh-cn/liSEO靠我brary/dd757808(v=vs.85).aspx

(二)其次,通过VLC播放器,可以查看到具体的格式。打开视频后,通过菜单【工具】/【编解码信息】可以查看到【编解码器】具体格式,举例如下,编解码SEO靠我器信息:

编码: H264 – MPEG-4 AVC (part 10) (avc1)

编码: H264 – MPEG-4 AVC (part 10) (h264)

(三)最后,分享一下ffmpeg demSEO靠我ux MP4文件后,转换视频流为live555可直接使用的h264 ES流的经验和方法:

针对(avc1),av_read_frame后,取前四个字节为长度,把前四字节直接替换为0×00,0×00,0×SEO靠我00,0×01即可,但注意每个frame可以有多个NAUL:

AVPacket pkt ;

    AVPacket * packet  =  &pkt ;

    av_init_packet (packet ) ;

avSEO靠我_read_frame (ctx , packet ) ;

     if (packet ->stream_index  ==  0 )

     { //is video stream

const  char start_SEO靠我code [ 4 ]  =  {  0 ,  0 ,  0 ,  1  } ;

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

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

                 int len  =  0 ;

                 uint8_t  *p  = packet ->data ;

is_avSEO靠我c_  = True ;

                 do 

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

len  = ntohl ( * ( ( loSEO靠我ng * )p ) ) ;

                     memcpy (p , start_code ,  4 ) ;

                    p  +=  4 ;

                    p  += len ;

if (p  >= packet ->data  + packet -SEO靠我>size )

                     {

                         break ;

                     }

                 }  while  ( 1 ) ;

             }

         }

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

bsfc_  = av_bitstream_filter_init ( "h264_mp4toannexb" ) ;

    if (pkt ->stream_index  ==  0 )

{ //isSEO靠我 video stream

      AVBitStreamFilterContext * bsfc  = bsfc_ ;

         int a ;

         while  (bsfc )  {

AVPacket new_pkt  =  SEO靠我*pkt ;

            a  = av_bitstream_filter_filter (bsfc , encode_ctx_ , NULL ,

&new_pkt. data ,  &new_pkt. size ,SEO靠我

                pkt ->data , pkt ->size ,

                pkt ->flags  & AV_PKT_FLAG_KEY ) ;

if (a  ==  0  && new_pkt. data  != pkt ->SEO靠我data  && new_pkt. destruct )  {

uint8_t  *t  =  ( uint8_t * ) (new_pkt. size  + FF_INPUT_BUFFER_PADDISEO靠我NG_SIZE ) ;  //the new should be a subset of the old so cannot overflow

                 if (t )  {

memcpy (t , new_pktSEO靠我. data , new_pkt. size ) ;

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

new_pkt.SEO靠我 data  = t ;

                    a  =  1 ;

                 }  else

                    a  = AVERROR (ENOMEM ) ;

             }

if  (a  >  0  && pkt ->data  != new_pkt. data )SEO靠我  {

                av_free_packet (pkt ) ;

                new_pkt. destruct  = av_destruct_packet ;

             }  else  if  (a  <  0 )  {

envir (SEO靠我 )  <<  "!!!!!!!!!!av_bitstream_filter_filter failed"  <<  ",res="  << a  <<  "\n" ;

             }

*pkt  = new_pktSEO靠我 ;

            bsfc  = bsfc ->next ;

         }

} 分类:技术文章 | 标签: h264码流、MP4 demux、mp4 ffmpeg demux、MP4文件两种格式AVC1SEO靠我和H264的区别 | 阅读次数: 2,184

我一直疑问为什么有些视频解码时显示格式是:H264,大部分又是:AVC1

我在搜索编程资料时在微软的msdn上发现的:

原文:http://msdn.microSEO靠我soft.com/en-us/library/dd757808(v=vs.85).aspx

FOURCC:AVC1   描述:H.264 bitstream without start codes.

FOSEO靠我URCC:H264   描述:H.264 bitstream with start codes.

H.264 Bitstream with Start Codes

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

这段话的大致意思是:带有开始码的H.264视频一般是用于无线发射、有线广播或者HD-DSEO靠我VD中的。这些数据流的开始都有一个开始码:0x000001 或者 0x00000001.

H.264 Bitstream Without Start Codes

The MP4 container forSEO靠我mat stores H.264 data without start codes. Instead, each NALU is prefixed by a length field, which gSEO靠我ives the length of the NALU in bytes. The size of the length field can vary, but is typically 1, 2, SEO靠我or 4 bytes.

这段话的大致意思是:没有开始码的H.264视频主要是存储在MP4格式的文件中的。它的数据流的开始是1、2或者4个字节表示长度数据。

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

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

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

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

H.264 Video Types

The following media subtypes are SEO靠我defined for H.264 video.

SubtypeFOURCCDescriptionMEDIASUBTYPE_AVC1AVC1H.264 bitstream without start cSEO靠我odes.MEDIASUBTYPE_H264H264H.264 bitstream with start codes.MEDIASUBTYPE_h264h264Equivalent to MEDIASUSEO靠我BTYPE_H264, with a different FOURCC.MEDIASUBTYPE_X264X264Equivalent to MEDIASUBTYPE_H264, with a diffSEO靠我erent FOURCC.MEDIASUBTYPE_x264x264Equivalent to MEDIASUBTYPE_H264, with a different FOURCC.

These subtSEO靠我ype GUIDs are declared in wmcodecdsp.h.

The main difference between these media types is the presenceSEO靠我 of start codes in the bitstream. If the subtype isMEDIASUBTYPE_AVC1, the bitstream does not containSEO靠我 start codes.

H.264 Bitstream with Start Codes

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

When start codes are present in the bitstream, the following media type is useSEO靠我d:

Major typeSubtypesFormat typeMEDIATYPE_VideoMEDIASUBTYPE_H264MEDIASUBTYPE_h264MEDIASUBTYPE_X264,SEO靠我 orMEDIASUBTYPE_x264FORMAT_VideoInfoFORMAT_VideoInfo2FORMAT_MPEG2Video, or GUID_NULL

If the format tSEO靠我ype isGUID_NULL, no format structure is present.

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

The following subtypes areSEO靠我 equivalent:

H.264 Bitstream Without Start Codes

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

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

Major typeSubtypeFormat typeMEDIATYPESEO靠我_VideoMEDIASUBTYPE_AVC1FORMAT_MPEG2Video

The format block is an MPEG2VIDEOINFOstructure. This structurSEO靠我e should be filled in as follows:

hdr: A VIDEOINFOHEADER2structure that describes the bitstream. No coSEO靠我lor table is present after theBITMAPINFOHEADERportion of the structure, and biClrUsed must be zero.dwSSEO靠我tartTimeCode: Not used. Set to zero.cbSequenceHeader: The length of the dwSequenceHeaderarray in byteSEO靠我s.dwProfile: Specifies the H.264 profile.dwLevel: Specifies the H.264 level.dwFlags: The number of bSEO靠我ytes used for the length field that appears before eachNALU. The length field indicates the size of SEO靠我the following NALU in bytes. For example, ifdwFlagsis 4, each NALU is preceded by a 4-byte length fiSEO靠我eld. The valid values are 1, 2, and 4.dwSequenceHeader: A byte array that may contain sequence paramSEO靠我eter set (SPS) and picture parameter set (PPS) NALUs.

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

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

“SEO靠我”的新闻页面文章、图片、音频、视频等稿件均为自媒体人、第三方机构发布或转载。如稿件涉及版权等问题,请与 我们联系删除或处理,客服邮箱:html5sh@163.com,稿件内容仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同 其观点或证实其内容的真实性。

网站备案号:浙ICP备17034767号-2