1. 程式人生 > >[轉]多媒體封裝格式詳解---MP4

[轉]多媒體封裝格式詳解---MP4

原文 https://www.cnblogs.com/jingzhishen/p/3698570.html

參考 http://www.cnblogs.com/ranson7zop/p/7889272.html

MP4檔案格式詳解——結構概述

http://blog.csdn.net/pirateleo/article/details/7061452

一、基本概念

1、 檔案,由許多Box和FullBox組成。

2、 Box,每個Box由Header和Data組成。

3、 FullBox,是Box的擴充套件,Box結構的基礎上在Header中增加8bits version和24bits flags。

4、 Header,包含了整個Box的長度size和型別type。當size==0時,代表這是檔案中最後一個Box;當size==1時,意 味著Box長度需要更多bits來描述,在後面會定義一個64bits的largesize描述Box的長度;當type是uuid時,代表Box中的數 據是使用者自定義擴充套件型別。

5、 Data,是Box的實際資料,可以是純資料也可以是更多的子Boxes。

6、 當一個Box的Data中是一系列子Box時,這個Box又可成為Container Box。

結構如下圖:

 

                                                                                             檔案基本結構描述圖

 

1、  ftypbox,在檔案的開始位置,描述的檔案的版本、相容協議等;

2、  moovbox,這個box中不包含具體媒體資料,但包含本檔案中所有媒體資料的巨集觀描述資訊,moov box下有mvhd和trak box。

        >>mvhd中記錄了建立時間、修改時間、時間度量標尺、可播放時長等資訊。

        >>trak中的一系列子box描述了每個媒體軌道的具體資訊。

3、  moofbox,這個box是視訊分片的描述資訊。並不是MP4檔案必須的部分,但在我們常見的可線上播放的MP4格式檔案中(例如Silverlight Smooth Streaming中的ismv檔案)確是重中之重

4、  mdatbox,實際媒體資料。我們最終解碼播放的資料都在這裡面。

5、  mfrabox,一般在檔案末尾,媒體的索引檔案,可通過查詢直接定位所需時間點的媒體資料。

附:Smooth Streaming中ismv檔案結構,檔案分為了多個Fragments,每個Fragment中包含moof和mdat。這樣的結構符合漸進式播放需求。(mdat及其描述資訊逐步傳輸,收齊一個Fragment便可播放其中的mdat)。

1

2

3

4

/* Set other implicit flags immediately */

if (mov->mode == MODE_ISM)

    mov->flags |= FF_MOV_FLAG_EMPTY_MOOV | FF_MOV_FLAG_SEPARATE_MOOF |

                  FF_MOV_FLAG_FRAGMENT;

 


 

 

 

static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
    uint32_t minor_ver;
    int comp_brand_size;
    char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */
    char* comp_brands_str;
    uint8_t type[5] = {0};

    avio_read(pb, type, 4);
    if (strcmp(type, "qt  "))
        c->isom = 1;
    av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
    av_dict_set(&c->fc->metadata, "major_brand", type, 0);
    minor_ver = avio_rb32(pb); /* minor version */
    snprintf(minor_ver_str, sizeof(minor_ver_str), "%"PRIu32"", minor_ver);
    av_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0);

    comp_brand_size = atom.size - 8;
    if (comp_brand_size < 0)
        return AVERROR_INVALIDDATA;
    comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
    if (!comp_brands_str)
        return AVERROR(ENOMEM);
    avio_read(pb, comp_brands_str, comp_brand_size);
    comp_brands_str[comp_brand_size] = 0;
    av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
    av_freep(&comp_brands_str);

    return 0;
}

 

 

二、MP4檔案格式(ISO-14496-12/14)

MP4檔案概述

MP4檔案就是由各式各樣的Box組成的,下表中列出了所有必選或可選的Box型別,√代表Box必選。

具體列表:

 

ftyp

 

 

 

 

 

√ 

file type and compatibility

pdin

 

 

 

 

 

 

progressive download information

moov

 

 

 

 

 

√ 

container for all the metadata

 

mvhd

 

 

 

 

√ 

movie header, overall declarations

 

trak

 

 

 

 

√ 

container for an individual track or stream

 

 

tkhd

 

 

 

√ 

track header, overall information about the track

 

 

tref

 

 

 

 

track reference container

 

 

edts

 

 

 

 

edit list container

 

 

 

elst

 

 

 

an edit list

 

 

mdia

 

 

 

√ 

container for the media information in a track

 

 

 

mdhd

 

 

√ 

media header, overall information about the media

 

 

 

hdlr

 

 

√ 

handler, declares the media (handler) type

 

 

 

minf

 

 

√ 

media information container

 

 

 

 

vmhd

 

 

video media header, overall information (video track only)

 

 

 

 

smhd

 

 

sound media header, overall information (sound track only)

 

 

 

 

hmhd

 

 

hint media header, overall information (hint track only)

 

 

 

 

nmhd

 

 

Null media header, overall information (some tracks only)

 

 

 

 

dinf

 

√ 

data information box, container

 

 

 

 

 

dref

√ 

data reference box, declares source(s) of media data in track

 

 

 

 

stbl

 

√ 

sample table box, container for the time/space map

 

 

 

 

 

stsd

√ 

sample descriptions (codec types, initialization etc.)

 

 

 

 

 

stts

√  

(decoding) time-to-sample

 

 

 

 

 

ctts

 

(composition) time to sample

 

 

 

 

 

stsc

√ 

sample-to-chunk, partial data-offset

information

 

 

 

 

 

stsz

 

sample sizes (framing)

 

 

 

 

 

stz2

 

compact sample sizes (framing)

 

 

 

 

 

stco

√ 

chunk offset, partial data-offset information

 

 

 

 

 

co64

 

64-bit chunk offset

 

 

 

 

 

stss

 

sync sample table (random access points)

 

 

 

 

 

stsh

 

shadow sync sample table

 

 

 

 

 

padb

 

sample padding bits

 

 

 

 

 

stdp

 

sample degradation priority

 

 

 

 

 

sdtp

 

independent and disposable samples

 

 

 

 

 

sbgp

 

sample-to-group

 

 

 

 

 

sgpd

 

sample group description

 

 

 

 

 

subs

 

sub-sample information

 

mvex

 

 

 

 

 

movie extends box

 

 

mehd

 

 

 

 

movie extends header box

 

 

trex

 

 

 

√ 

track extends defaults

 

ipmc

 

 

 

 

 

IPMP Control Box

moof

 

 

 

 

 

 

movie fragment

 

mfhd

 

 

 

 

√ 

movie fragment header

 

traf

 

 

 

 

 

track fragment

 

 

tfhd

 

 

 

√ 

track fragment header

 

 

trun

 

 

 

 

track fragment run

 

 

sdtp

 

 

 

 

independent and disposable samples

 

 

sbgp

 

 

 

 

sample-to-group

 

 

subs

 

 

 

 

sub-sample information

mfra

 

 

 

 

 

 

movie fragment random access

 

tfra

 

 

 

 

 

track fragment random access

 

mfro

 

 

 

 

√ 

movie fragment random access offset

mdat

 

 

 

 

 

 

media data container

free

 

 

 

 

 

 

free space

skip

 

 

 

 

 

 

free space

 

udta

 

 

 

 

 

user-data

 

 

cprt

 

 

 

 

copyright etc.

meta

 

 

 

 

 

 

metadata

 

hdlr

 

 

 

 

√ 

handler, declares the metadata (handler) type

 

dinf

 

 

 

 

 

data information box, container

 

 

dref

 

 

 

 

data reference box, declares source(s) of metadata items

 

ipmc

 

 

 

 

 

IPMP Control Box

 

iloc

 

 

 

 

 

item location

 

ipro

 

 

 

 

 

item protection

 

 

sinf

 

 

 

 

protection scheme information box

 

 

 

frma

 

 

 

original format box

 

 

 

imif

 

 

 

IPMP Information box

 

 

 

schm

 

 

 

scheme type box

 

 

 

schi

 

 

 

scheme information box

 

iinf

 

 

 

 

 

item information

 

xml

 

 

 

 

 

XML container

 

bxml

 

 

 

 

 

binary XML container

 

pitm

 

 

 

 

 

primary item reference

 

fiin

 

 

 

 

 

file delivery item information

 

 

paen

 

 

 

 

partition entry

 

 

 

fpar

 

 

 

file partition

 

 

 

fecr

 

 

 

FEC reservoir

 

 

segr

 

 

 

 

file delivery session group

 

 

gitn

 

 

 

 

group id to name

 

 

tsel

 

 

 

 

track selection

meco

 

 

 

 

 

 

additional metadata container

 

mere

 

 

 

 

 

metabox relation

正式開始前先對檔案的幾個重要部分巨集觀介紹一下,以便諸位在後續學習時心中有數:

1、  ftypbox,在檔案的開始位置,描述的檔案的版本、相容協議等;

2、  moovbox,這個box中不包含具體媒體資料,但包含本檔案中所有媒體資料的巨集觀描述資訊,moov box下有mvhd和trak box。

        >>mvhd中記錄了建立時間、修改時間、時間度量標尺、可播放時長等資訊。

        >>trak中的一系列子box描述了每個媒體軌道的具體資訊。

3、  moofbox,這個box是視訊分片的描述資訊。並不是MP4檔案必須的部分,但在我們常見的可線上播放的MP4格式檔案中(例如Silverlight Smooth Streaming中的ismv檔案)確是重中之重。

4、  mdatbox,實際媒體資料。我們最終解碼播放的資料都在這裡面。

5、  mfrabox,一般在檔案末尾,媒體的索引檔案,可通過查詢直接定位所需時間點的媒體資料。

附:Smooth Streaming中ismv檔案結構,檔案分為了多個Fragments,每個Fragment中包含moof和mdat。這樣的結構符合漸進式播放需求。(mdat及其描述資訊逐步傳輸,收齊一個Fragment便可播放其中的mdat)。

 

 

 

 

 

 

http://blog.csdn.net/tx3344/article/details/8476669

MP4(MPEG-4 Part 14)是一種常見的多媒體容器格式,它是在“ISO/IEC 14496-14”標準檔案中定義的。

1.最小組成單元 BOX

像FLV的tag、MKV的EBML、ASF檔案中的 ASF object.mp4 是由一系列的box組成,他的最小組成單元就是box.

 

 

size;指明瞭整個box所佔用的大小,包括header部分.

type;表示這個box的型別。(附表1)

largesize;如果box很大超過了uint32的最大數值,size就被設定為1,並用接下來的 largesize來存放大小。

 

2.mp4檔案整體結構

mp4檔案說白了就是一系列box組成,大box裡面有小box。

接下來會深入到具體的box裡面,來具體分析mp4格式

未完待續.....

 

 

附表1

 

Code Abstract Defined in/by
ainf Asset information to identify, license and play DECE
albm Album title and track number (user-data) 3GPP
auth Media author name (user-data) 3GPP
avcn AVC NAL Unit Storage Box DECE
bloc Base location and purchase location for license acquisition DECE
bpcc Bits per component JP2
buff Buffering information AVC
bxml binary XML container ISO
ccid OMA DRM Content ID OMA DRM 2.1
cdef type and ordering of the components within the codestream JP2
clsf Media classification (user-data) 3GPP
cmap mapping between a palette and codestream components JP2
co64 64-bit chunk offset ISO
colr specifies the colourspace of the image JP2
cprt copyright etc. (user-data) ISO
crhd reserved for ClockReferenceStream header MP4V1
cslg composition to decode timeline mapping ISO
ctts (composition) time to sample ISO
cvru OMA DRM Cover URI OMA DRM 2.1
dcfD Marlin DCF Duration, user-data atom type OMArlin
dinf data information box, container ISO
dref data reference box, declares source(s) of media data in track ISO
dscp Media description (user-data) 3GPP
dsgd DVB Sample Group Description Box DVB
dstg DVB Sample to Group Box DVB
edts edit list container ISO
elst an edit list ISO
feci FEC Informatiom ISO
fecr FEC Reservoir ISO
fiin FD Item Information ISO
fire File Reservoir ISO
fpar File Partition ISO
free free space ISO
frma original format box ISO
ftyp file type and compatibility JP2ISO
gitn Group ID to name ISO
gnre Media genre (user-data) 3GPP
grpi OMA DRM Group ID OMA DRM 2.0
hdlr handler, declares the media (handler) type ISO
hmhd hint media header, overall information (hint track only) ISO
hpix Hipix Rich Picture (user-data or meta-data) HIPIX
icnu OMA DRM Icon URI OMA DRM 2.0
ID32 ID3 version 2 container inline
idat Item data ISO
ihdr Image Header JP2
iinf item information ISO
iloc item location ISO
imif IPMP Information box ISO
infu OMA DRM Info URL OMA DRM 2.0
iods Object Descriptor container box MP4V1
iphd reserved for IPMP Stream header MP4V1
ipmc IPMP Control Box ISO
ipro item protection ISO
iref Item reference ISO
jP$20$20 JPEG 2000 Signature JP2
jp2c JPEG 2000 contiguous codestream JP2
jp2h Header JP2
jp2i intellectual property information JP2
kywd Media keywords (user-data) 3GPP
loci Media location information (user-data) 3GPP
lrcu OMA DRM Lyrics URI OMA DRM 2.1
m7hd reserved for MPEG7Stream header MP4V1
mdat media data container ISO
mdhd media header, overall information about the media ISO
mdia container for the media information in a track ISO
mdri Mutable DRM information OMA DRM 2.0
meco additional metadata container ISO
mehd movie extends header box ISO
mere metabox relation ISO
meta Metadata container ISO
mfhd movie fragment header ISO
mfra Movie fragment random access ISO
mfro Movie fragment random access offset ISO
minf media information container ISO
mjhd reserved for MPEG-J Stream header MP4V1
moof movie fragment ISO
moov container for all the meta-data ISO
mvcg Multiview group AVC
mvci Multiview Information AVC
mvex movie extends box ISO
mvhd movie header, overall declarations ISO
mvra Multiview Relation Attribute AVC
nmhd Null media header, overall information (some tracks only) ISO
ochd reserved for ObjectContentInfoStream header MP4V1
odaf OMA DRM Access Unit Format OMA DRM 2.0
odda OMA DRM Content Object OMA DRM 2.0
odhd reserved for ObjectDescriptorStream header MP4V1
odhe OMA DRM Discrete Media Headers OMA DRM 2.0
odrb OMA DRM Rights Object OMA DRM 2.0
odrm OMA DRM Container OMA DRM 2.0
odtt OMA DRM Transaction Tracking OMA DRM 2.0
ohdr OMA DRM Common headers OMA DRM 2.0
padb sample padding bits ISO
paen Partition Entry ISO
pclr palette which maps a single component in index space to a multiple- component image JP2
pdin Progressive download information ISO