HRESULT H264AVCSampleProvider::GetSPSAndPPSBuffer()

in FFmpegInterop/Source/H264AVCSampleProvider.cpp [55:125]


HRESULT H264AVCSampleProvider::GetSPSAndPPSBuffer(DataWriter^ dataWriter)
{
	HRESULT hr = S_OK;
	int spsLength = 0;
	int ppsLength = 0;

	// Get the position of the SPS
	if (m_pAvCodecCtx->extradata == nullptr && m_pAvCodecCtx->extradata_size < 8)
	{
		// The data isn't present
		hr = E_FAIL;
	}
	if (SUCCEEDED(hr))
	{
		byte* spsPos = m_pAvCodecCtx->extradata + 8;
		spsLength = spsPos[-1];

		if (m_pAvCodecCtx->extradata_size < (8 + spsLength))
		{
			// We don't have a complete SPS
			hr = E_FAIL;
		}
		else
		{
			auto vSPS = ref new Platform::Array<uint8_t>(spsPos, spsLength);

			// Write the NAL unit for the SPS
			dataWriter->WriteByte(0);
			dataWriter->WriteByte(0);
			dataWriter->WriteByte(0);
			dataWriter->WriteByte(1);

			// Write the SPS
			dataWriter->WriteBytes(vSPS);
		}
	}

	if (SUCCEEDED(hr))
	{
		if (m_pAvCodecCtx->extradata_size < (8 + spsLength + 3))
		{
			hr = E_FAIL;
		}

		if (SUCCEEDED(hr))
		{
			byte* ppsPos = m_pAvCodecCtx->extradata + 8 + spsLength + 3;
			ppsLength = ppsPos[-1];

			if (m_pAvCodecCtx->extradata_size < (8 + spsLength + 3 + ppsLength))
			{
				hr = E_FAIL;
			}
			else
			{
				auto vPPS = ref new Platform::Array<uint8_t>(ppsPos, ppsLength);

				// Write the NAL unit for the PPS
				dataWriter->WriteByte(0);
				dataWriter->WriteByte(0);
				dataWriter->WriteByte(0);
				dataWriter->WriteByte(1);

				// Write the PPS
				dataWriter->WriteBytes(vPPS);
			}
		}
	}

	return hr;
}