MP4 File Format Parsing and MP4 File Segmentation Algorithm

Source: Internet
Author: User
MP4 File Format Parsing and MP4 File Segmentation Algorithm

MP4 is a complicated media format, originated from QuickTime. I spent a lot of time studying it before, especially how to integrate it perfectlyVideoVODIn the application, the main problem is to deal with the huge "Media Header" of MP4 files ". Of course, streaming media VOD can also be made in FLV format, and FLV can also be encapsulated in H. 264 video data, but Adobe does not recommend this. After all, MP4 is H. 264 the best storage format.

In the past few days, I have sorted out and reconstructed the MP4 file parsing program, and integrated the decomposition and merging program. It was previously written in C language and applied to server programs running on Linux, now I want to change it to C ++ so that I can use it in other projects. I don't need to transplant a copy of C #, so I won't be able to use it for the moment. Let's talk about it when necessary. This article first briefly introduces the general structure of MP4 files and its segmentation algorithm, and then writes an article to introduce how to perfectly apply MP4 to VOD projects.

 

I. MP4 format Analysis

MP4(MPEG-4 part 14) is a common multimedia container format that is defined in the ISO/IEC 14496-14 standard file and is part of the MPEG-4, is an implementation of the media format defined in the ISO/IEC 14496-12 (MPEG-4 Part 12 ISO base media file format) standard, the latter defines a general structure standard for media files. MP4 is a fully-described container format. It is considered that any form of data can be embedded in it, and videos and audios of various encoding can all be used together, however, most of our common MP4 files are storedAVC (H.264)OrMPEG-4 (Part 2)Encoded video andAACEncoded audio. The official mp4format file suffix is ".mp4. There are other MP4-based extensions or scaled versions, including:M4v,3GP,F4v.

MP4 is composed of multiple "Boxes". Large boxes store small boxes and are nested at the first level to store media information. The basic structure of the box is:

The size indicates the size occupied by the entire box, including the header part. If the box is large (for example, the mdat box for storing specific video data) and exceeds the maximum value of uint32, the size is set to 1 and the next 8-bit uint64 is used to store the size.

An MP4 file may contain a lot of boxes, which greatly increases the complexity of resolution. On this page, http://mp4ra.org/atoms.htmlrecord a few recently registered box.pdf. If we can see so many boxes, all of them need to be supported and parsed one by one, even if the headers are exploding. Fortunately, most MP4 files do not have so many box types, which is a simplified and common MP4 file structure:

In general, the most important part of parsing a media file is the width and height, length, bit rate, encoding format, frame list, and key frame list of the video file, and the corresponding Timestamp and position in the file. In MP4, the information is stored separately in several boxes under stbl box using specific algorithms, parse all the boxes under stbl to restore media information. The following table describes the storage information of the preceding boxes:

It's not easy to get the list of MP4 File frames. It needs to be parsed layer by layer, and then integrate the information of stts STSC stsz STSS stco and other boxes, to restore the frame list, the timestamp and offset of each frame. Also, you need to take care of the boxes that may or may not appear... As you can see, MP4 groups the frame sample, that is, the chunk, and indirectly uses the chunk to describe the frame. The reason for this is that the storage space can be compressed, reduce the file size occupied by media information. The parsing of STSC box is relatively complicated. It uses a clever method to describe the ing between sample and Chunk.

This is the structure of the STSC box. The meaning of the first few items will not be explained. We can see that each entry structure in the STSC box contains three types of data, which mean:First_chunkThis chunk number starts, and each chunk hasSamples_per_chunkNumber of samples, and each sample can beSample_description_indexThis index finds the description information in the STSD box ". That is to say, each entry struct describes a group of chunks, which share the same characteristics, that is, each chunk containsSamples_per_chunkSample, okay. How many chunks have the same characteristics? Use the next entry struct to calculate the value.First_chunkSubtract thisFirst_chunkThe number of chunks in this group is obtained. The last entry struct indicatesFirst_chunkTo the last chunk, each chunk hasSampls_per_chunkSample. That's what it means. . Because this algorithm cannot know the number of all chunks in the file, you must use stco or co64. The code may be clearer:

1. Analyze the entry directly

2. Then, after knowing the total number of chunks through stco or co64, restore the ing table

After reading STSC, you can combine all the boxes in stbl to calculate the video and audio frame lists, timestamp, offset, and other data. The following shows the list of retrieved key frames:

With the key frame list, we can continue with the question, that isMP4 File Segmentation. Implementing MP4 segmentation is the most critical technical link to apply MP4 to the on-demand video system. If this is not done, the"Drag".

Ii. MP4 File Segmentation Algorithm

The so-called "segmentation" means to cut large files into small files and implement MP4 segmentation,

  • First, you need to obtain the key frame list.
  • Then, select the time range to be split (for example, starting from the key frame)
  • Then, re-generate moov box (note that all related box and box size need to be changed)
  • Finally, copy the corresponding data to generate a new file.

First, as described above, second, you only need to traverse the key frame list to find the key frame closest to the time range you want to split, the fourth point is "copy-paste", and the key lies in the third point. Because this step involves all the boxes in stbl, you must re-generate entrys. Similarly, all other boxes are fine. You only need to keep the sample and Chunk corresponding to the key frame, you can delete the remaining items, but STSC box is a little complicated. Let's look at the Code directly:

After modifying the box, you need to regenerate the moov box. Because the size and length of the moov box have changed, you need to modify the box size accordingly, you must never forget this. Otherwise, the player will encounter parsing errors. After the box is re-generated, we also need to calculate the length of the split data. Because the data length has also changed, while modifying the size of the mdat box, to modify the chunk offset of all boxes in stbl at the same time, remember!

The entire logical process is as follows:

After all these are implemented, we have met the conditions for creating an MP4 on-demand video system. However, there are still some other problems that need to be solved for MP4 On-Demand videos. I will introduce them in the next article.

 

++

Copyright. For more information, see author and source ~

++

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.