Getw.bnalu analysis (online data plus personal analysis)

Source: Internet
Author: User
Tags rewind
Getw.bnalu analysis (online data plus personal analysis)

Http://www.chinavideo.org/archiver? Tid-7943.html

[Color = # 38761d]/*!
**************************************** ********************************
* \ Brief
* Returns the size of the NALU (BITs between start codes in case
* Annex B. NALU-> Buf and NALU-> Len are filled. Other field in
* NALU-> remain uninitialized (will be taken care of by nalutorbsp.
*
* \ Return
* 0 if there is nothing any more to read (EOF)
*-1 in case of any error
*
* \ Note side-effect: returns length of Start-code in bytes.
*
* \ Note
* Getaskbnalu expects start codes at byte aligned positions in the file
*
**************************************** ********************************
*/[/Color]
[Color = # 38761d] // bits is the read byte stream, which has been initialized before it. This function is decoded from byte_stream_nal_unit (numbytesinnalunit) to unlock NALU, get an ebsp process [/color]
Int getw.bnalu (nalu_t * NALU)
{
Int info2, info3, Pos = 0;
Int startcodefound, rewind;
Char * Buf;
Int leadingzero8bitscount = 0, trailingzero8bits = 0;

If (BUF = (char *) calloc (NALU-> max_size, sizeof (char) = NULL) no_mem_exit ("getw.bnalu: Buf ");
[Color = # 38761d] // * starts from/* a start */to/* A end */to identify the start code prefix startcodeprefix.
// If the slice corresponding to NALU is the start of a frame, startcodeprefix = ox00000001 and startcodeprefix_len = 4 are expressed in four bytes.
// Otherwise, startcodeprefix = ox000001 and startcodeprefix_len = 3 */
// Count a byte stream NALU unit. A total of POS bytes (including this non-zero byte) before the first non-zero byte [/color]
While (! Feof (BITs) & (BUF [POS ++] = fgetc (BITs) = 0); [color = # 38761d] // read data in bits into Buf, until the first non-zero byte or the end, the tracking result is Pos = 4, that is, the data format before the byte stream is 00 00 01 ...... [/Color]

If (feof (BITs) [color = # 38761d] // feof (BITs): if the end of the file is reached, the function value is true.
[/Color] {
If (Pos = 0)
Return 0;
Else
{
Printf ("getaskbnalu can't read start code \ n ");
Free (BUF );
Return-1;
}
}
If (BUF [pos-1]! = 1)
{
Printf ("getaskbnalu: no start code at the begin of the nalu, return-1 \ n ");
Free (BUF );
Return-1;
}
If (Pos <3)
{
Printf ("getaskbnalu: no start code at the begin of the nalu, return-1 \ n ");
Free (BUF );
Return-1;
}
Else if (Pos = 3)
{
NALU-> startcodeprefix_len = 3; [color = # 38761d] // startcodeprefix = 0x000001
[/Color] leadingzero8bitscount = 0;
}
Else
{
Leadingzero8bitscount = pos-4; [color = # 38761d] // The Slice corresponding to NALU is represented in 4-bit bytes at the beginning of a frame [/color] startcodeprefix = 0x00000001
NALU-> startcodeprefix_len = 4;
}
[Color = # 38761d] // The 1st byte stream nal unit can has leading_zero_8bits, but subsequent ones are not
// Allowed to contain it since these zeros (if any) are considered trailing_zero_8bits
// Of the previous byte stream nal unit. [/color]
If (! Isfirstbytestreamnalu & leadingzero8bitscount> 0)
{
Printf ("getaskbnalu: The leading_zero_8bits syntax can only be present in the first byte stream nal unit, return-1 \ n ");
Free (BUF );
Return-1;
}
Isfirstbytestreamnalu = 0;
Startcodefound = 0;
Info2 = 0;
Info3 = 0;
While (! Startcodefound)
{
If (feof (BITs ))
{
[Color = # 38761d] // count the trailing_zero_8bits [/color]
While (BUF [pos-2-TrailingZero8Bits] = 0)
Trailingzero8bits ++;
NALU-> Len = (pos-1)-NALU-> startcodeprefix_len-LeadingZero8BitsCount-TrailingZero8Bits;
Memcpy (NALU-> Buf, & Buf [leadingzero8bitscount + NALU-> startcodeprefix_len], NALU-> Len );

NALU-> forbidden_bit = (NALU-> Buf [0]> 7) & 1;
NALU-> nal_reference_idc = (NALU-> Buf [0]> 5) & 3;
NALU-> nal_unit_type = (NALU-> Buf [0]) & 0x1f;
[Color = # 38761d] // printf ("getw.bnalu, EOF case: POS % d NALU-> Len % d, NALU-> reference_idc % d, nal_unit_type % d \ n ", POs, NALU-> Len, NALU-> nal_reference_idc, NALU-> nal_unit_type); [/color]
# If trace
Fprintf (p_trace, "\ n \ nlast NALU in file \ n ");
Fprintf (p_trace, "Annex B NALU w/% s startcode, Len % d, forbidden_bit % d, nal_reference_idc % d, nal_unit_type % d \ n ",
NALU-> startcodeprefix_len = 4? "Long": "short", NALU-> Len, NALU-> forbidden_bit, NALU-> nal_reference_idc, NALU-> nal_unit_type );
Fflush (p_trace );
# Endif
Free (BUF );
Return pos-1;
}
[Color = # ff0000] Buf [POS ++] = fgetc (BITs );
Info3 = findstartcode (& Buf [[color = # 38761d] pos-4] [/color], 3 ); [/color] [color = # 38761d] // ++ info3 = 1 indicates that 8 bits are entered before the start code of the next NALU (00000001)
[/Color] [color = # ff0000] If (info3! = 1)
Info2 = findstartcode (& Buf [[color = # 38761d] pos-3 [/color], 2 ); [/color] [color = # 38761d] // ++ info2 = 1 indicates that no 8-bit zero-fill code (000001) is found before the start code of the next NALU)
[/Color] [color = # ff0000] startcodefound = (info2 = 1 | info3 = 1); [/color]
}
[Color = # ff0000] // The Red Section is very important. It is mainly used to find the starting code of the next NALU, because the front POS points to the byte corresponding to the last underline in 00 00 00 01 _, at this time Pos = 5, for info3, [/color] [color = # 38761d] pos-4 [/color] [color = # ff0000] at this time, POS points to 00 00, that is, the second byte data, info3 is looking for the POs followed by 00 00 01, of course, there is no, because at this time the POS is connected to 00 00 01, infro2 is the same, to make info3, info2 is 1, as long as the start code of the next NALU is found,
Use Buf [POS ++] = fgetc [/color] [color = # ff0000] (BITs); constantly copy data in bits to Buf, finally, [/color] [color = # ff0000] POS + rewind is the parameter in nal_unit (numbytesinnalunit. [/Color]
[Color = # ff0000] let's take a look at findstartcode [/color]
[Color = # ff0000] Static int findstartcode (unsigned char * Buf, int zeros_in_startcode)
{
Int Info;
Int I; [/color]
[Color = # ff0000] info = 1;
For (I = 0; I <zeros_in_startcode; I ++) [color = # 38761d] // hainei_10_3_14 _ find three consecutive 0x00 (zeros_in_startcode = 3)
[/Color] If (BUF [I]! = 0)
Info = 0; [/color]
[Color = # ff0000] If (BUF [I]! = 1) [color = # 38761d] // 0x01 After 3 0x00
[/Color] info = 0;
Return Info;
} [/Color]
// Count the trailing_zero_8bits
If (info3 = 1) // If the detected start code is 00 00 01, trailing_zero_8bits is sure not to be present
{[Color = # 38761d] // ++ that is, when info2 = 1, the current NALU must not end with an 8-bit code and therefore do not need to be counted. Otherwise, when info3 = 1, to analyze whether there are eight bits at the end of the current NALU to fill in the zero code, count if there are
[/Color] While (BUF [pos-5-TrailingZero8Bits] = 0)
Trailingzero8bits ++;
}
[Color = # 38761d] // here, we have found another start code (and read length of startcode bytes more than we shold
// Have. Hence, go back in the file [/color]
Rewind = 0;
If (info3 = 1)
Rewind =-4;
Else if (info2 = 1)
Rewind =-3;
Else
Printf ("panic: Error in next start Code Search \ n ");
If (0! = Fseek (bits, rewind, seek_cur) [color = # 38761d] // ++ seek_cur = 1, representing the current position of the file pointer
[/Color] {
Snprintf (errortext, et_size, "get1_bnalu: cannot fseek % d in the bit stream file", rewind );
Free (BUF );
Error (errortext, 600 );
}
[Color = # 38761d] // here the leading zeros (if any), start code, the complete nalu, trailing zeros (if any)
// And the next start code is in the Buf.
// The size of BUF is POs, POS + rewind are the number of bytes excluding the next
// Start code, and (Pos + rewind)-startcodeprefix_len-LeadingZero8BitsCount-TrailingZero8Bits
// Is the size of the NALU. [/color]
NALU-> Len = (Pos + rewind)-NALU-> startcodeprefix_len-LeadingZero8BitsCount-TrailingZero8Bits;
Memcpy (NALU-> Buf, & Buf [leadingzero8bitscount + NALU-> startcodeprefix_len], NALU-> Len );
[Color = # 38761d] // The first line is the first syntax element of the decoder in section 7.3.1.
// The second line is the second bold syntax element of the decoder in section 7.3.1. [/color]
NALU-> forbidden_bit = (NALU-> Buf [0]> 7) & 1; // ++
NALU-> nal_reference_idc = (NALU-> Buf [0]> 5) & 3; // ++ for the definition of the three values, see standard 7.4.1
NALU-> nal_unit_type = (NALU-> Buf [0]) & 0x1f; // ++

// Printf ("getw.bnalu, regular case: POS % d NALU-> Len % d, NALU-> reference_idc % d, nal_unit_type % d \ n", POs, NALU-> Len, NALU-> nal_reference_idc, NALU-> nal_unit_type );
# If trace
Fprintf (p_trace, "\ n \ nannex B NALU w/% s startcode, Len % d, forbidden_bit % d, nal_reference_idc % d, nal_unit_type % d \ n ",
NALU-> startcodeprefix_len = 4? "Long": "short", NALU-> Len, NALU-> forbidden_bit, NALU-> nal_reference_idc, NALU-> nal_unit_type );
Fflush (p_trace );
# Endif

Free (BUF );

Return (Pos + rewind );
}

GuxiongxxxPosted on

It's really good.

Sam_wheatPosted at AM

One is supported, so the code should be studied.

Yanyun231Posted on

I spoke very well. Thanks

YuanzhenhaiPosted on AM

Top one

JstulhwPosted on PM

Good

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.