|
12 | 12 |
|
13 | 13 | using namespace UTILS; |
14 | 14 |
|
| 15 | +namespace |
| 16 | +{ |
| 17 | +unsigned int ReadGolomb(AP4_BitReader& bits) |
| 18 | +{ |
| 19 | + unsigned int leadingZeros = 0; |
| 20 | + while (bits.ReadBit() == 0) |
| 21 | + { |
| 22 | + leadingZeros++; |
| 23 | + if (leadingZeros >= 32) |
| 24 | + return 0; // safeguard |
| 25 | + } |
| 26 | + if (leadingZeros) |
| 27 | + { |
| 28 | + return (1 << leadingZeros) - 1 + bits.ReadBits(leadingZeros); |
| 29 | + } |
| 30 | + else |
| 31 | + { |
| 32 | + return 0; |
| 33 | + } |
| 34 | +} |
| 35 | +} // unnamed namespace |
| 36 | + |
15 | 37 | AVCCodecHandler::AVCCodecHandler(AP4_SampleDescription* sd) |
16 | 38 | : CodecHandler{sd}, |
17 | 39 | m_countPictureSetIds{0}, |
@@ -87,7 +109,8 @@ void AVCCodecHandler::UpdatePPSId(const AP4_DataBuffer& buffer) |
87 | 109 | if (!m_needSliceInfo) |
88 | 110 | return; |
89 | 111 |
|
90 | | - //Search the Slice header NALU |
| 112 | + // Iterate data to find all NALU units slice headers of type 5 "Coded slice of an IDR picture" |
| 113 | + // to get the pic_parameter_set_id value of last NALU |
91 | 114 | const AP4_Byte* data(buffer.GetData()); |
92 | 115 | AP4_Size dataSize(buffer.GetDataSize()); |
93 | 116 | for (; dataSize;) |
@@ -126,25 +149,20 @@ void AVCCodecHandler::UpdatePPSId(const AP4_DataBuffer& buffer) |
126 | 149 | if (m_countPictureSetIds < 2) |
127 | 150 | m_needSliceInfo = false; |
128 | 151 |
|
129 | | - unsigned int nal_unit_type = *data & 0x1F; |
| 152 | + unsigned int nalUnitType = *data & 0x1F; |
130 | 153 |
|
131 | | - if ( |
132 | | - //nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_NON_IDR_PICTURE || |
133 | | - nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_IDR_PICTURE //|| |
134 | | - //nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_DATA_PARTITION_A || |
135 | | - //nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_DATA_PARTITION_B || |
136 | | - //nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_DATA_PARTITION_C |
137 | | - ) |
| 154 | + // Following code is a simplification of AP4_AvcFrameParser::ParseSliceHeader from AP4_AvcFrameParser::Feed |
| 155 | + if (nalUnitType == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_IDR_PICTURE) |
138 | 156 | { |
139 | 157 | AP4_DataBuffer unescaped(data, dataSize); |
140 | 158 | AP4_NalParser::Unescape(unescaped); |
141 | 159 | AP4_BitReader bits(unescaped.GetData(), unescaped.GetDataSize()); |
142 | 160 |
|
143 | 161 | bits.SkipBits(8); // NAL Unit Type |
144 | 162 |
|
145 | | - AP4_AvcFrameParser::ReadGolomb(bits); // first_mb_in_slice |
146 | | - AP4_AvcFrameParser::ReadGolomb(bits); // slice_type |
147 | | - m_pictureId = AP4_AvcFrameParser::ReadGolomb(bits); //picture_set_id |
| 163 | + ReadGolomb(bits); // first_mb_in_slice |
| 164 | + ReadGolomb(bits); // slice_type |
| 165 | + m_pictureId = ReadGolomb(bits); // pic_parameter_set_id |
148 | 166 | } |
149 | 167 | // move to the next NAL unit |
150 | 168 | data += naluSize; |
|
0 commit comments