Increase ParseScript cache from 30 to 90 seconds
[xy_vsfilter.git] / src / dsutil / H264Nalu.cpp
blob7cd5455676fd424937554928f5f90c0406b7294e
1 /*
2 * (C) 2006-2012 see Authors.txt
4 * This file is part of MPC-HC.
6 * MPC-HC is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * MPC-HC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "stdafx.h"
22 #include "H264Nalu.h"
24 void CH264Nalu::SetBuffer(BYTE* pBuffer, int nSize, int nNALSize)
26 m_pBuffer = pBuffer;
27 m_nSize = nSize;
28 m_nNALSize = nNALSize;
29 m_nCurPos = 0;
30 m_nNextRTP = 0;
32 m_nNALStartPos = 0;
33 m_nNALDataPos = 0;
35 if (!nNALSize && nSize) {
36 MoveToNextAnnexBStartcode();
40 bool CH264Nalu::MoveToNextAnnexBStartcode()
42 int nBuffEnd = m_nSize - 4;
44 for (int i = m_nCurPos; i < nBuffEnd; i++) {
45 if ((*((DWORD*)(m_pBuffer + i)) & 0x00FFFFFF) == 0x00010000) {
46 // Find next AnnexB Nal
47 m_nCurPos = i;
48 return true;
52 m_nCurPos = m_nSize;
53 return false;
56 bool CH264Nalu::MoveToNextRTPStartcode()
58 if (m_nNextRTP < m_nSize) {
59 m_nCurPos = m_nNextRTP;
60 return true;
63 m_nCurPos = m_nSize;
64 return false;
67 bool CH264Nalu::ReadNext()
69 if ((m_nCurPos >= m_nSize) || (m_nCurPos < 0)) {
70 return false;
73 if ((m_nNALSize != 0) && (m_nCurPos == m_nNextRTP)) {
74 // RTP Nalu type : (XX XX) XX XX NAL..., with XX XX XX XX or XX XX equal to NAL size
75 m_nNALStartPos = m_nCurPos;
76 m_nNALDataPos = m_nCurPos + m_nNALSize;
77 unsigned nTemp = 0;
78 for (int i = 0; i < m_nNALSize; i++) {
79 nTemp = (nTemp << 8) + m_pBuffer[m_nCurPos++];
81 m_nNextRTP += nTemp + m_nNALSize;
82 MoveToNextRTPStartcode();
83 } else {
84 // Remove trailing bits
85 while (m_pBuffer[m_nCurPos] == 0x00 && ((*((DWORD*)(m_pBuffer + m_nCurPos)) & 0x00FFFFFF) != 0x00010000)) {
86 m_nCurPos++;
89 // AnnexB Nalu : 00 00 01 NAL...
90 m_nNALStartPos = m_nCurPos;
91 m_nCurPos += 3;
92 m_nNALDataPos = m_nCurPos;
93 MoveToNextAnnexBStartcode();
96 forbidden_bit = (m_pBuffer[m_nNALDataPos] >> 7) & 1;
97 nal_reference_idc = (m_pBuffer[m_nNALDataPos] >> 5) & 3;
98 nal_unit_type = (NALU_TYPE)(m_pBuffer[m_nNALDataPos] & 0x1f);
100 return true;