mp4 文件中的h264 avc1格式介绍
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*?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?*p?=?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(p?>=?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(a?==?0?&&?new_pkt.data?!=?pkt->data?&&?new_pkt.destruct)?{
? ? ? ? ? ? ? ??uint8_t?*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(t?+?new_pkt.size,?0,?FF_INPUT_BUFFER_PADDING_SIZE);
? ? ? ? ? ? ? ? ? ? new_pkt.data?=?t;
? ? ? ? ? ? ? ? ? ? a?=?1;
? ? ? ? ? ? ? ??}?else
? ? ? ? ? ? ? ? ? ? a?=?AVERROR(ENOMEM);
? ? ? ? ? ??}
? ? ? ? ? ??if?(a?>?0?&&?pkt->data?!=?new_pkt.data)?{
? ? ? ? ? ? ? ? av_free_packet(pkt);
? ? ? ? ? ? ? ? new_pkt.destruct?=?av_destruct_packet;
? ? ? ? ? ??}?else?if?(a?<?0)?{
? ? ? ? ? ? ? ? envir()?<<?"!!!!!!!!!!av_bitstream_filter_filter failed"?<<?",res="?<<?a?<<?"\n";
? ? ? ? ? ??}
? ? ? ? ? ??*pkt?=?new_pkt;
? ??
? ? ? ? ? ? bsfc?=?bsfc->next;
? ? ? ??}
? ??} 分類:技術文章?| 標簽:?h264碼流、MP4 demux、mp4 ffmpeg demux、MP4文件兩種格式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.
| 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:
| MEDIATYPE_Video |
| MEDIASUBTYPE_H264,?MEDIASUBTYPE_h264,?MEDIASUBTYPE_X264, or?MEDIASUBTYPE_x264 |
| FORMAT_VideoInfo,?FORMAT_VideoInfo2,?FORMAT_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.
| MEDIATYPE_Video |
| MEDIASUBTYPE_AVC1 |
| 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.
總結
以上是生活随笔為你收集整理的mp4 文件中的h264 avc1格式介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 地址栏图标
- 下一篇: SQL Server错误代码大全及解释(