1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
| int GetAnnexbNALU(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("GetAnnexbNALU: Buf");
//如果start_code前还有数据,丢弃start_code前leading_zero_8bits
while(!feof(bits) && (Buf[pos++]=fgetc(bits))==0);
if(feof(bits))
{
if(pos==0)
return 0;
else
{
printf( "GetAnnexbNALU can't read start code\n");
free(Buf);
return -1;
}
}
if(Buf[pos-1]!=1)
{
printf ("GetAnnexbNALU: no Start Code at the begin of the NALU, return -1\n");
free(Buf);
return -1;
}
if(pos<3)
{
printf ("GetAnnexbNALU: no Start Code at the begin of the NALU, return -1\n");
free(Buf);
return -1;
}
else if(pos==3)
{
nalu->startcodeprefix_len = 3;
LeadingZero8BitsCount = 0;
}
else
{
LeadingZero8BitsCount = pos-4;
nalu->startcodeprefix_len = 4;
}
//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.
//字节流数据的第一个 NAL 单元才会有leading_zero_8bits;后面的 NALU 不会包含 leading_zero_8bits,
//因为这些 leading_zero_8bits 会被看做前一个 NAL 单元后面的 trailing_zero_8bits
if(!IsFirstByteStreamNALU && LeadingZero8BitsCount>0)
{
printf ("GetAnnexbNALU: 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))
{
//Count the trailing_zero_8bits
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;
// printf ("GetAnnexbNALU, 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);
#if TRACE
fprintf (p_trace, "\n\nLast NALU in File\n\n");
fprintf (p_trace, "Annex B NALU w/ %s startcode, len %d, forbidden_bit %d, nal_reference_idc %d, nal_unit_type %d\n\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;
}
//找 start_code,先找0x00000001,后找0x000001
Buf[pos++] = fgetc (bits);
info3 = FindStartCode(&Buf[pos-4], 3);
if(info3 != 1)
info2 = FindStartCode(&Buf[pos-3], 2);
StartCodeFound = (info2 == 1 || info3 == 1);
}
//Count the trailing_zero_8bits
//计算 trailing_zero_8bits,如果start_code为0x000001,trailing_zero_8bits 肯定不存在
if(info3==1) //if the detected start code is 00 00 01, trailing_zero_8bits is sure not to be present
{
while(Buf[pos-5-TrailingZero8Bits]==0)
TrailingZero8Bits++;
}
// 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 = 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))
{
snprintf (errortext, ET_SIZE, "GetAnnexbNALU: Cannot fseek %d in the bit stream file", rewind);
free(Buf);
error(errortext, 600);
}
// 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.
nalu->len = (pos+rewind)-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;
//printf ("GetAnnexbNALU, 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\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);
}
|