1. 程式人生 > >MP4學習(七)ts-mp4原始碼閱讀(5)mvhd box的解析

MP4學習(七)ts-mp4原始碼閱讀(5)mvhd box的解析

mvhd box的解析

常見的MP4結構圖


mvhd box的定義

// mvhd box  
typedef struct {  
    u_char    size[4]; // 大小  
    u_char    name[4]; // 名字  
    u_char    version[1]; // box版本,0或1,一般為0  
    u_char    flags[3]; // 標識  
    u_char    creation_time[4]; // 建立時間(相對於UTC時間1904-01-01零點的秒數)  
    u_char    modification_time[4]; // 修改時間  
    u_char    timescale[4]; // 檔案媒體在1秒時間內的刻度值,可以理解為1秒長度的時間單元數  
    u_char    duration[4]; // 該track的時間長度,用duration和time scale值可以計算track時長  
    u_char    rate[4]; // 推薦播放速率  
    u_char    volume[2]; // 與rate類似,表示音量  
    u_char    reserved[10]; // 保留位  
    u_char    matrix[36]; // 視訊變換矩陣  
    u_char    preview_time[4]; // 下面四個成員表示pre-defined  
    u_char    preview_duration[4];  
    u_char    poster_time[4];  
    u_char    selection_time[4];  
    u_char    selection_duration[4];  
    u_char    current_time[4];  
    u_char    next_track_id[4]; // 下一個track使用的id號  
} mp4_mvhd_atom;  


mvhd box的解析

// 讀取mvhd box
int
Mp4Meta::mp4_read_mvhd_atom(int64_t atom_header_size, int64_t atom_data_size)
{
    int64_t             atom_size;
    uint32_t            timescale;
    mp4_mvhd_atom       *mvhd;
    mp4_mvhd64_atom     mvhd64;

    if (sizeof(mp4_mvhd_atom) - 8 > (size_t)atom_data_size)
        return -1;

	// 讀取mvhd box
    IOBufferReaderCopy(meta_reader, &mvhd64, sizeof(mp4_mvhd64_atom));
    mvhd = (mp4_mvhd_atom*)&mvhd64;

    if (mvhd->version[0] == 0) {
        timescale = mp4_get_32value(mvhd->timescale);

    } else {        // 64-bit duration
        timescale = mp4_get_32value(mvhd64.timescale);
    }

	// 讀取timescale欄位
    this->timescale = timescale;

    atom_size = atom_header_size + atom_data_size;

    mvhd_atom.buffer = TSIOBufferCreate();
    mvhd_atom.reader = TSIOBufferReaderAlloc(mvhd_atom.buffer);

    TSIOBufferCopy(mvhd_atom.buffer, meta_reader, atom_size, 0);
    mp4_meta_consume(atom_size);

    return 1;
}