📄 annexb.cpp
字号:
/*!
*************************************************************************************
* \file annexb.c
*
* \brief
* Annex B Byte Stream format
*
* \author
* Main contributors (see contributors.h for copyright, address and affiliation details)
* - Stephan Wenger <stewe@cs.tu-berlin.de>
*************************************************************************************
*/
#include <stdlib.h>
#include <string.h>
#include "global.h"
#include "annexb.h"
#include "memalloc.h"
FILE *bits = NULL; //!< the bit stream file
static int FindStartCode2 (unsigned char *Buf);
static int FindStartCode3 (unsigned char *Buf);
static bool flag = TRUE;
static int info2=0, info3=0;
/*!
************************************************************************
* \brief
* Returns the size of the NALU (bits between start codes in case of
* 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
* GetAnnexbNALU expects start codes at byte aligned positions in the file
*
************************************************************************
*/
int GetAnnexbNALU (NALU_t *nalu)
{
int pos = 0;
int StartCodeFound, rewind;
unsigned char *Buf;
if ((Buf = (unsigned char*)calloc (nalu->max_size , sizeof(char))) == NULL) error ("GetAnnexbNALU: Could not allocate Buf memory", 100);
nalu->startcodeprefix_len=3;
if (3 != fread (Buf, 1, 3, bits))
{
free(Buf);
return 0;
}
info2 = FindStartCode2 (Buf);
if(info2 != 1) {
if(1 != fread(Buf+3, 1, 1, bits))
{
free(Buf);
return 0;
}
info3 = FindStartCode3 (Buf);
if (info3 != 1)
{
free(Buf);
return -1;
}
else {
pos = 4;
nalu->startcodeprefix_len = 4;
}
}
else{
nalu->startcodeprefix_len = 3;
pos = 3;
}
StartCodeFound = 0;
info2 = 0;
info3 = 0;
while (!StartCodeFound)
{
if (feof (bits))
{
nalu->len = (pos-1)-nalu->startcodeprefix_len;
memcpy (nalu->buf, &Buf[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;
free(Buf);
return pos-1;
}
Buf[pos++] = fgetc (bits);
info3 = FindStartCode3(&Buf[pos-4]);
if(info3 != 1)
info2 = FindStartCode2(&Buf[pos-3]);
StartCodeFound = (info2 == 1 || info3 == 1);
}
flag = FALSE;
// Here, we have found another start code (and read length of startcode bytes more than we should
// have. Hence, go back in the file
rewind = (info3 == 1)? -4 : -3;
if (0 != fseek (bits, rewind, SEEK_CUR))
{
free(Buf);
error("GetAnnexbNALU: Cannot fseek in the bit stream file", 600);
}
// Here the Start code, the complete NALU, 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 is the size of the NALU
nalu->len = (pos+rewind)-nalu->startcodeprefix_len;
memcpy (nalu->buf, &Buf[nalu->startcodeprefix_len], nalu->len);
nalu->forbidden_bit = nalu->buf[0] & 0x80; //(nalu->buf[0]>>7) & 1;
nalu->nal_reference_idc = nalu->buf[0] & 0x60; //(nalu->buf[0]>>5) & 3;
nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;
free(Buf);
return (pos+rewind);
}
/*!
************************************************************************
* \brief
* returns if new start code is found at byte aligned position buf.
* new-startcode is of form N 0x00 bytes, followed by a 0x01 byte.
*
* \return
* 1 if start-code is found or \n
* 0, indicating that there is no start code
*
* \param Buf
* pointer to byte-stream
* \param zeros_in_startcode
* indicates number of 0x00 bytes in start-code.
************************************************************************
*/
static int FindStartCode2 (unsigned char *Buf)
{
if(Buf[0]!=0 || Buf[1]!=0 || Buf[2] !=1) return 0;
else return 1;
}
static int FindStartCode3 (unsigned char *Buf)
{
if(Buf[0]!=0 || Buf[1]!=0 || Buf[2] !=0 || Buf[3] !=1) return 0;
else return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -