【多媒体封装格式详解】---MP4【4】
前面介绍过的几种格式flv、mkv、asf等。他们音视频的数据包一般都是按照文件的顺序交叉安放。你解析完头部信息后。剩下的一般就按照文件顺序一个数据包一个数据包的解析就行了。但是MP4完全不是这种概念。他的媒体信息和数据是分开存放的。就是你想获得数据之前必须要解析出每个帧数据所有的位置。mp4存放这个帧信息的是放在stbl这个box里。而真实的数据放在mdat中。接下来就讲讲stbl与mdat的对应关系。
Sample Table Box(stbl)

stts: Decoding Time to Sample Box 时间戳和Sample映射表
stsd: Sample Description Box
stsz, stz2: Sample Size Boxes 每个Sample大小的表。
stsc: Sample to chunk 的映射表。
‘stco’, ‘co64’: Chunk位置偏移表
stss:关键帧index。
1.解析stsd可获得coding类型、视频宽高、音频samplesize、channelcount这些和解码器有关信息。
- aligned(8) class SampleDescriptionBox (unsigned int(32) handler_type)
- extends FullBox('stsd', 0, 0){
- int i ;
- unsigned int(32) entry_count;
- for (i = 1 ; i u entry_count ; i++){
- switch (handler_type){
- case ‘soun’: // for audio tracks
- AudioSampleEntry();
- break;
- case ‘vide’: // for video tracks
- VisualSampleEntry();
- break;
- case ‘hint’: // Hint track
- HintSampleEntry();
- break;
- }
- }
- }
- aligned(8) abstract class SampleEntry (unsigned int(32) format)
- extends Box(format){
- const unsigned int(8)[6] reserved = 0;
- unsigned int(16) data_reference_index;
- }
- class HintSampleEntry() extends SampleEntry (protocol) {
- unsigned int(8) data [];
- }
- // Visual Sequences
- class VisualSampleEntry(codingname) extends SampleEntry (codingname){
- unsigned int(16) pre_defined = 0;
- const unsigned int(16) reserved = 0;
- unsigned int(32)[3] pre_defined = 0;
- unsigned int(16) width;
- unsigned int(16) height;
- template unsigned int(32) horizresolution = 0x00480000; // 72 dpi
- template unsigned int(32) vertresolution = 0x00480000; // 72 dpi
- const unsigned int(32) reserved = 0;
- template unsigned int(16) frame_count = 1;
- string[32] compressorname;
- template unsigned int(16) depth = 0x0018;
- int(16) pre_defined = -1;
- }
- // Audio Sequences
- class AudioSampleEntry(codingname) extends SampleEntry (codingname){
- const unsigned int(32)[2] reserved = 0;
- template unsigned int(16) channelcount = 2;
- template unsigned int(16) samplesize = 16;
- unsigned int(16) pre_defined = 0;
- const unsigned int(16) reserved = 0 ;
- template unsigned int(32) samplerate = {timescale of media}<<16;
- }
2.解析stsz box 可以获得一个sample size的表
- aligned(8) class SampleSizeBox extends FullBox(‘stsz’, version = 0, 0) {
- unsigned int(32) sample_size;
- unsigned int(32) sample_count;
- if (sample_size==0) {
- for (i=1; i u sample_count; i++) {
- unsigned int(32) entry_size;
- }
- }
- }
3.解析stts
- aligned(8) class TimeToSampleBox
- extends FullBox(’stts’, version = 0, 0) {
- unsigned int(32) entry_count;
- int i;
- for (i=0; i < entry_count; i++) {
- unsigned int(32) sample_count;
- unsigned int(32) sample_delta;
- }
- }
4.解析stsc 还原Sample 与chunk的映射表

Sample 是存储的最基本单元,mp4把Sample 存在chunk中。chunk的长度、chunk的大小、chunk中Sample的数量及大小都是不定的。
通过解析这部分box来还原这个映射表。
- aligned(8) class SampleToChunkBox
- extends FullBox(‘stsc’, version = 0, 0) {
- unsigned int(32) entry_count;
- for (i=1; i u entry_count; i++) {
- unsigned int(32) first_chunk;
- unsigned int(32) samples_per_chunk;
- unsigned int(32) sample_description_index;
- }
- }
每个entry 表示着一组数据,entry_count 表示这数量。这一组其实是相同类型的chunk。
first_chunk 表示 这一组相同类型的chunk中 的第一个chunk数。
这些chunk 中包含的Sample 数量,即samples_per_chunk 是一致的。
每个Sample 可以通过sample_description_index 去stsd box 找到描述信息。
看ffmpeg中mov_read_stsc() 它把这些数据放在一个结构体数组中备用。
- static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
- {
- AVStream *st;
- MOVStreamContext *sc;
- unsigned int i, entries;
- if (c->fc->nb_streams < 1)
- return 0;
- st = c->fc->streams[c->fc->nb_streams-1];
- sc = st->priv_data;
- avio_r8(pb); /* version */
- avio_rb24(pb); /* flags */
- entries = avio_rb32(pb);
- av_dlog(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
- if (!entries)
- return 0;
- if (entries >= UINT_MAX / sizeof(*sc->stsc_data))
- return AVERROR_INVALIDDATA;
- sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
- if (!sc->stsc_data)
- return AVERROR(ENOMEM);
- for (i = 0; i < entries && !pb->eof_reached; i++) {
- sc->stsc_data[i].first = avio_rb32(pb);
- sc->stsc_data[i].count = avio_rb32(pb);
- sc->stsc_data[i].id = avio_rb32(pb);
- }
- sc->stsc_count = i;
- if (pb->eof_reached)
- return AVERROR_EOF;
- return 0;
- }
在获得完整的映射表,我们还需要chunk总个数信息。这些信息放在‘stco’, ‘co64’中。
5.解析‘stco’, ‘co64’
“stco”定义了每个thunk在媒体流中的位置。位置有两种可能,32位的和64位的,后者对非常大的电影很有用。
32位
- aligned(8) class ChunkOffsetBox
- extends FullBox(‘stco’, version = 0, 0) {
- unsigned int(32) entry_count;
- for (i=1; i u entry_count; i++) {
- unsigned int(32) chunk_offset;
- }
- }
64位
- aligned(8) class ChunkLargeOffsetBox
- extends FullBox(‘co64’, version = 0, 0) {
- unsigned int(32) entry_count;
- for (i=1; i u entry_count; i++) {
- unsigned int(64) chunk_offset;
- }
- }
从这个box我们就可以获得 chunk 的总数量,entry_count。
from:http://blog.csdn.net/tx3344/article/details/8506131
1.解析‘stco’, ‘co64’我们有了chunk 表,知道了chunk 的总数及每个chunk所在文件的位置。
2.解析stsc 配合着上面的chunk表,我们就能弄个Sample与chunk的关系表。我们也就能获得每个Sample的位置信息。
3.配合上面的stts 时间表和解码器信息等。搞出ES流已经不成问题了。
4.想获得关键帧的index,需要解析stss’
- aligned(8) class SyncSampleBox
- extends FullBox(‘stss’, version = 0, 0) {
- unsigned int(32) entry_count;
- int i;
- for (i=0; i < entry_count; i++) {
- unsigned int(32) sample_number;
- }
- }
【多媒体封装格式详解】---MP4【4】的更多相关文章
- 多媒体封装格式详解---MP4
MP4文件格式详解——结构概述 http://blog.csdn.net/pirateleo/article/details/7061452 一.基本概念 1. 文件,由许多Box和FullBox组成 ...
- 【多媒体封装格式详解】--- AAC ADTS格式分析
ADTS全称是(Audio Data Transport Stream),是AAC的一种十分常见的传输格式. 记得第一次做demux的时候,把AAC音频的ES流从FLV封装格式中抽出来送给硬件解码器时 ...
- (转)【多媒体封装格式详解】--- AAC ADTS格式分析
出自:http://blog.csdn.net/tx3344/article/details/7414543 http://www.it6655.com/2012/08/aac-adts-html ...
- 【多媒体封装格式详解】---MKV
http://blog.csdn.net/tx3344/article/details/8162656# http://blog.csdn.net/tx3344/article/details/817 ...
- FLV视频封装格式详解
FLV视频封装格式详解 分类: FFMpeg编解码 2012-04-04 21:13 1378人阅读 评论(2) 收藏 举报 flvheaderaudiovideocodecfile 目录(?)[-] ...
- H.264标准(一)mp4封装格式详解
在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...
- 【转】FLV视频封装格式详解
Overview Flash Video(简称FLV),是一种流行的网络格式.目前国内外大部分视频分享网站都是采用的这种格式. File Structure 从整个文件上开看,FLV是由The FLV ...
- 以太网帧格式、IP数据报格式、TCP段格式+UDP段格式 详解
转载:http://www.cnblogs.com/lifan3a/articles/6649970.html 以太网帧格式.IP数据报格式.TCP段格式+UDP段格式 详解 1.ISO开放系统有 ...
- java分享第十五天(log4j 格式详解)
log4j 格式详解 log4j.rootLogger=日志级别,appender1, appender2, -. 日志级别:ALL<DEBUG<INFO<WARN<ERRO ...
随机推荐
- C#枚举注释实例
public enum 枚举名称 { /// <summary> /// 注释描述1 /// </summary> ...
- int a[5]={1,2,3,4,5}; int *p=(int*)(&a+1); printf("%d",*(p-1)); 答案为什么是5?
这个问题的关键是理解 &a a是一个数组名,也就是数组的首地址.对a进行取地址运算符,得到的是一个指向数组的指针!!!!这句话尤为重要!也就相当于int (*p) [5] = &a;p ...
- Ubuntu下非常给力的下载工具--uget+aria2
转自Ubuntu下非常给力的下载工具--uget+aria2 Windows下的下载工具--迅雷,之所以下载速度快,乃是它能搜索资源.为己所用,而不是仅仅从原始地址这单一资源处下载. Ubuntu下也 ...
- 2013 ACM-ICPC长沙赛区全国邀请赛—Special equations
……但是没仔细看,直接跳过了 这题直接枚举就可以过了 ;}
- String及其他
String 以下例开始讲解String public class StringDemo { public static void main(String[] args) { // String s ...
- 李洪强iOS开发之OC[013] -类的创建的练习
// // main.m // 12 - 类的创建练习 // // Created by vic fan on 16/7/9. // Copyright © 2016年 李洪强. All ri ...
- MIT算法导论——第三讲.The Divide-and-Conquer
本栏目(Algorithms)下MIT算法导论专题是个人对网易公开课MIT算法导论的学习心得与笔记.所有内容均来自MIT公开课Introduction to Algorithms中Charles E. ...
- Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- PCB板的价格是怎么算出来的?
Part 1 :影响一块PCB板价格的各种因素 PCB的价格是很多采购者一直很困惑的事情,很多人在线下单时也会疑问这些价格是怎么算出来的,下面我们就一起谈论一下PCB价格的组成因素. 1.PCB所用材 ...
- JCIFS是很不稳定的
我以前也试过这样登录失败,第二天就能登录成功了. JCIFS是很不稳定的. 如果是域登录可以这样 //DOMAIN_IP 域名服务(其实域名和域名服务器IP可以,不过用IP解析速度快很 ...