H264 es stream file distinguishes frame boundary by calculating first_mb_in_slice

Source: Internet
Author: User

I recently encountered a problem about how to read a complete frame of data when reading an h264 file. Using the elecard stream analyzer tool, combined with the new generation of video compression coding standard-h264/AVC (second edition), and find and summarize on the Internet as follows:

First, the nal syntax, titles syntax, and nal_unit_type semantics must be known:



The above two figures are taken from the next-generation video compression coding standard-h264/AVC (second edition)

A simple method to differentiate frames is to compare the frame reading time with 0x00 0x00 0x00 0x01, if you read another byte, it will correspond to the forbidden_zero_bit (1bit), nal_ref_idc (2bit), nal_unit_type (5bit) of table 6.1. The nal_unit_type value can determine the frame type according to Table 6.20, for 7, 8, there is no first_mb_in_slice, you can always read that the data between the next 0x00 0x00 0x00 0x01 is parameter set data. First_mb_in_slice is available for and the first_mb_in_slice of the first part of each frame is 0, which is useful for acquiring data of one frame divided into multiple parts.

The following is an analysis of the exponential Columbus code: From http://blog.csdn.net/simongyley/article/details/8517817

When parsing the K-order index Columbus code, first find the first non-zero bit starting from the current location of the bit stream, and record the number of found zero bits as leadingzerobits, and then calculate the codenum according to leadingzerobits. The pseudocode is described as follows:

First_mb_in_slice is obtained by expgolomb Decoding of a string of binary numbers encoded by expgolomb in h264. The calculation method is finally found by searching multiple parties on the Internet and analyzing h264 files, I would like to share with you some comments about any mistakes.

I frame:

As shown in:


88 80 = 1000 1000 1000 assuming the order is 0, then leadingzerobits = 0, codenum (first_mb_in_slice) = 1-1 + 0 = 0

06 62 = 0000 0110 0110 0010 assume that the order is 0, then leadingzerobits = 5, codenum (first_mb_in_slice) = 2exp (5)-2exp (0) + 10011 (2 hexadecimal) = 32-1 + 19 = 50

P frame:

As shown in:


9A 00 = 1001 1010 0000 assuming the order is 0, then leadingzerobits = 0, codenum (first_mb_in_slice) = 1-1 + 0 = 0

03 29 = 0000 0011 0010 1001 assume that the order is 0, then leadingzerobits = 6, codenum (first_mb_in_slice) = 2exp (6)-2exp (0) + 100101 (2 hexadecimal) = 64-1 + 37= 100


I hope it will be helpful to you. For details about how to calculate first_mb_in_slice, there should be more code on the Internet. For details, refer to FFMPEG.


H264 es stream file distinguishes frame boundary by calculating first_mb_in_slice

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.