MP4 stbl box解析

简介:
stbl是MP4文件中包含子Box最多,也是最复杂的一个Box,包含了定位和解码sample的必要信息。

下面是一个MP4文件的box列表:
ftyp ; length: 32
free ; length: 8
mdat ; length: 37524012
moov ; length: 12076
  |__mvhd ; length: 108
  |__trak ; length: 11862
  |  |__tkhd ; length: 92
  |  |__edts ; length: 36
  |  |__mdia ; length: 11726
  |    |__mdhd ; length: 32
  |    |__hdlr ; length: 45
  |    |__minf ; length: 11641
  |      |__vmhd ; length: 20
  |      |__dinf ; length: 36
  |        |__dref ; length: 28
  |      |__stbl ; length: 11577    (sample table)采样表box
  |        |__stsd ; length: 137     (sample descriptions)采样描述box
  |        |__stts ; length: 7224    (time to stamp)采样时戳因映射表box
  |        |__stss ; length: 140     (sync sample table)关键帧列表box
  |        |__stsc ; length: 280     (sample to chunk)sample和chunk映射表box
  |        |__stsz ; length: 3624   (sample  size)每个sample的大小(有可能为stsz2)box
  |        |__stco ; length: 164    (chunk offset)每个chunk相对于文件头的偏移(大于2G的文件为stco64)box
  |__udta ; length: 98
  |  |__meta ; length: 90
end of file!

1.stco  (chunk offset)每个chunk相对于文件头的偏移(大于2G的文件为stco64)box

字段

长度(字节)

描述

尺寸

4

这个atom的字节数

类型

4

stco

版本

1

这个atom的版本

标志

3

这里为0

条目数目(n)

4

chunk offset的数目

chunk偏移

4*n

字节偏移量从文件开始到当前chunk这个表根据chunk number索引,第一项就是第一个trunk,第二项就是第二个trunk



2.stsc  (sample to chunk)sample和chunk映射表box
mp4中每个chunk所包含的sample个数可能不同,具有相同sample个数的chunk被组织起来(chunk entry),stsc中记录了
所有的chunk entry。每个chunk entry有三个变量,(1)chunk entry的第一个chunk序号(First chunk number);(2)chunk entry
中每个chunk拥有的sample个数;(3)chunk entry中每个sample在stsd中的序号。
要知道当前chunk entry包含的chunk个数,必须用下一个chunk entry的First chunk number减去当前的First chunk number。

字段

长度(字节)

描述

尺寸

4

这个atom的字节数

类型

4

stsc

版本

1

这个atom的版本

标志

3

这里为0

条目数目(n)

4

sample-to-chunk的数目

sample-to-chunk

(4*3)*n

sample-to-chunk表的结构

First chunk

4

这个table使用的第一个chunk序号

Samples per chunk

4

当前trunk内的sample数目

Sample description ID

4

与这些sample关联的sample description的序号



3.stsd (sample descriptions)采样描述box

字段

长度(字节)

描述

尺寸

4

这个atom的字节数

类型

4

stsd

版本

1

这个atom的版本

标志

3

这里为0

条目数目

4

sample descriptions的数目

Sample description


不同的媒体类型有不同的sample description,但是每个sample description的前四个字段是相同的,包含以下的数据成员

尺寸

4

这个sample description的字节数

数据格式

4

存储数据的格式。

保留

6


数据引用索引

2

利用这个索引可以检索与当前sample description关联的数据。数据引用存储在data reference atoms


avc1结构:
struct mp4_avc1_box{
VISUAL_SAMPLE_ENTRY_FIELDS{//共86Byte
  MP4_SAMPLE_ENTRY_FIELDS{//共16Byte
    MP4_BASE_BOX{
      U32 type;
      U32 size;
    }
    U8 reserved[6];
    U16 data_reference_index;
  }
  U16 pre_defined;
  U16 reserved;
  U32 pre_defined[3];
  U16 width;
  U16 height;
  U32 horiz_res;
  U32 vert_res;
  U32 reserved; 
  U16 frames_count;
  AVCDecoderConfigurationRecord{
    U8 compressr_name[32];
    U16 bit_depth;
    U16 pre_defined;
  }
}
struct mp4_avcC_box *avcC;
struct mp4_btrt_box *btrt;//可无
struct mp4_m4ds_box *m4ds;//可无
}
avC结构:
例如上图解析为:
00 00 00 19 (avc1 Size)
61 76 63 31 (avc1 box name)
00 00  00 00 00 00 (reserve)
00 01 (data_reference_index)
00 00 (pre_defined)
00 00 (reserved)
00 00 00 00 (pre_defined)
00 00 00 00 (pre_defined)
00 00 00 00 (pre_defined)
07 08 (Width)
04 38  (Height)
00 48 00 00 (horiz_res)
00 48 00 00 (vert_res)
00 00 00 00 (reserved)
00 01 (frames_count)
00 00 ...(32)...00 (compressr_name)
00 18 (bit_depth)
ff ff (pre_defined)
00 00 00 23 (avcC Size)
61 76 63 43 (avcC box name)
01 (version)
64 ( AVCProfileIndication )
00  ( profile_compatibility )
29 ( AVCLevelIndication )
ff  (NALU 长度)
e1 (SPS个数,低五位有效)
00 0c (SPS长度)
67 64     00 29 ac 76    80 78 02 27    e5 40  (SPS内容)
01 (PPS个数)
00 04 (PPS长度)
68 ee 38   b0  (PPS内容)


4.stts  (time to stamp)采样时戳因映射表box

字段

长度(字节)

描述

尺寸

4

这个atom的字节数

类型

4

stts

版本

1

这个atom的版本

标志

3

这里为0

条目数目

4

time-to-sample的数目

time-to-sample


Media中每个sampleduration。包含如下结构

Sample count

4

有相同duration的连续sample的数目

Sample duration

4

每个sampleduration



T(sample持续的时间,单位微秒)计算方式:
T=duration*1e6/timescale (us)

5.stsz  (sample size)每个sample的大小(有可能为stsz2)box

字段

长度(字节)

描述

尺寸

4

这个atom的字节数

类型

4

stsz

版本

1

这个atom的版本

标志

3

这里为0

Sample size

4

全部sample的数目。如果所有的sample有相同的长度,这个字段就是这个值。否则,这个字段的值就是0。那些长度存在sample size表中

条目数目

4

sample size的数目

sample size


sample size表的结构。这个表根据sample number索引,第一项就是第一个sample,第二项就是第二个sample

大小

4

每个sample的大小


6.stss (sync sample table)关键帧列表box

字段

长度(字节)

描述

尺寸

4

这个atom的字节数

类型

4

stss

版本

1

这个atom的版本

标志

3

这里为0

条目数目

4

sync sample的数目

sync sample


sync sample表的结构

Sample序号

4

是关键帧的sample序号


附:
stsz,stsc关系:
CE:ChunkEntry
C:Chunk
S:Sample
       /---CE0--\              /-CE1-\.../-CEn-\
 /--C0--\  /--C1--\  /---C2---\ ... .../--Cn--\
S0 S1 S2 S3 S4 S5 S6 S7 S8 ... ... ... ... ... ... Sn
如果自己写代码实现MP4封装,为了逻辑简单,可另一个ChunkEntry只包含一个Chunk,一个Chunk只包含一个Sample


转载请标明出处: https://blog.csdn.net/u013752202/article/details/80557459


猜你喜欢

转载自blog.csdn.net/u013752202/article/details/80557459