Exemplo n.º 1
0
REFERENCE_TIME CMpegSplitterFile::NextPTS(DWORD TrackNum)
{
	REFERENCE_TIME rt = -1;
	__int64 rtpos = -1;

	BYTE b;

	while(GetRemaining())
	{
		if(m_type == ps || m_type == es)
		{
			if(!NextMpegStartCode(b)) // continue;
				{ASSERT(0); break;}

			rtpos = GetPos()-4;

#if (EVO_SUPPORT == 0)
			if(b >= 0xbd && b < 0xf0)
#else
			if((b >= 0xbd && b < 0xf0) || (b == 0xfd))
#endif
			{
				peshdr h;
				if(!Read(h, b) || !h.len) continue;

				__int64 pos = GetPos();

				if(h.fpts && AddStream(0, b, h.len) == TrackNum)
				{
					ASSERT(h.pts >= m_rtMin && h.pts <= m_rtMax);
					rt = h.pts;
					break;
				}

				Seek(pos + h.len);
			}
		}
		else if(m_type == ts)
		{
			trhdr h;
			if(!Read(h)) continue;

			rtpos = GetPos()-4;

			if(h.payload && h.payloadstart && ISVALIDPID(h.pid))
			{
				peshdr h2;
				if(NextMpegStartCode(b, 4) && Read(h2, b)) // pes packet
				{
					if(h2.fpts && AddStream(h.pid, b, DWORD(h.bytes - (GetPos() - rtpos)) == TrackNum))
					{
						ASSERT(h2.pts >= m_rtMin && h2.pts <= m_rtMax);
						rt = h2.pts;
						break;
					}
				}
			}

			Seek(h.next);
		}
		else if(m_type == pva)
		{
			pvahdr h;
			if(!Read(h)) continue;

			if(h.fpts)
			{
				rt = h.pts;
				break;
			}
		}
	}

	if(rtpos >= 0) Seek(rtpos);
	if(rt >= 0) rt -= m_rtMin;

	return rt;
}
Exemplo n.º 2
0
HRESULT CMpegSplitterFile::SearchStreams(__int64 start, __int64 stop)
{
	Seek(start);
	stop = min(stop, GetLength());

	while(GetPos() < stop)
	{
		BYTE b;

		if(m_type == ps || m_type == es)
		{
			if(!NextMpegStartCode(b)) continue;

			if(b == 0xba) // program stream header
			{
				pshdr h;
				if(!Read(h)) continue;
			}
			else if(b == 0xbb) // program stream system header
			{
				pssyshdr h;
				if(!Read(h)) continue;
			}
#if (EVO_SUPPORT == 0)
			else if(b >= 0xbd && b < 0xf0) // pes packet
#else
			else if((b >= 0xbd && b < 0xf0) || (b == 0xfd)) // pes packet
#endif
			{
				peshdr h;
				if(!Read(h, b)) continue;

				if(h.type == mpeg2 && h.scrambling) {ASSERT(0); return E_FAIL;}

				if(h.fpts)
				{
					if(m_rtMin == _I64_MAX) {m_rtMin = h.pts; m_posMin = GetPos();}
					if(m_rtMin < h.pts && m_rtMax < h.pts) {m_rtMax = h.pts; m_posMax = GetPos();}
/*
int rate = 10000000i64 * (m_posMax - m_posMin) / (m_rtMax - m_rtMin); 
if(m_rate == 0) m_rate = rate;
TRACE(_T("rate = %d (%d), (h.pts = %I64d)\n"), rate, rate - m_rate, h.pts);
m_rate = rate;
*/
				}

				__int64 pos = GetPos();
				AddStream(0, b, h.len);
				if(h.len) Seek(pos + h.len);
			}
		}
		else if(m_type == ts)
		{
			trhdr h;
			if(!Read(h)) continue;

			// if(h.scrambling) {ASSERT(0); return E_FAIL;}

			__int64 pos = GetPos();

			if(h.payload && h.payloadstart)
			{
				UpdatePrograms(h);
			}
				
			if(h.payload && ISVALIDPID(h.pid))
			{
				peshdr h2;
				if(h.payloadstart && NextMpegStartCode(b, 4) && Read(h2, b)) // pes packet
				{
					if(h2.type == mpeg2 && h2.scrambling) {
						ASSERT(0); 
						return E_FAIL;
					}

					if(h2.fpts)
					{
						if(m_rtMin == _I64_MAX) {m_rtMin = h2.pts; m_posMin = GetPos();}
						if(m_rtMin < h2.pts && m_rtMax < h2.pts) {m_rtMax = h2.pts; m_posMax = GetPos();}
					}
				}
				else
				{
					b = 0;
				}

				AddStream(h.pid, b, DWORD(h.bytes - (GetPos() - pos)));
			}

			Seek(h.next);
		}
		else if(m_type == pva)
		{
			pvahdr h;
			if(!Read(h)) continue;

			if(h.fpts)
			{
				if(m_rtMin == _I64_MAX) {m_rtMin = h.pts; m_posMin = GetPos();}
				if(m_rtMin < h.pts && m_rtMax < h.pts) {m_rtMax = h.pts; m_posMax = GetPos();}
			}

			__int64 pos = GetPos();
			if(h.streamid == 1) AddStream(h.streamid, 0xe0, h.length);
			else if(h.streamid == 2) AddStream(h.streamid, 0xc0, h.length);
			if(h.length) Seek(pos + h.length);
		}
	}
Exemplo n.º 3
0
HRESULT CMpegSplitterFile::SearchStreams(__int64 start, __int64 stop, IAsyncReader* pAsyncReader, BOOL CalcDuration)
{
    Seek(start);
    stop = min(stop, GetLength());

    while (GetPos() < stop) {
        BYTE b;

        if (m_type == mpeg_ps || m_type == mpeg_es) {
            if (!NextMpegStartCode(b)) {
                continue;
            }

            if (b == 0xba) { // program stream header
                pshdr h;
                if (!Read(h)) {
                    continue;
                }
                m_rate = int(h.bitrate / 8);
            } else if (b == 0xbb) { // program stream system header
                pssyshdr h;
                if (!Read(h)) {
                    continue;
                }
            } else if ((b >= 0xbd && b < 0xf0) || (b == 0xfd)) { // pes packet
                peshdr h;
                if (!Read(h, b) || !h.len) {
                    continue;
                }

                if (h.type == mpeg2 && h.scrambling) {
                    ASSERT(0);
                    return E_FAIL;
                }

                if (h.fpts) {
                    if (m_rtMin == _I64_MAX) {
                        m_rtMin = h.pts;
                        m_posMin = GetPos();
                        TRACE(_T("m_rtMin(SearchStreams)=%s\n"), ReftimeToString(m_rtMin));
                    }
                    if (m_rtMin < h.pts && m_rtMax < h.pts) {
                        m_rtMax = h.pts;
                        m_posMax = GetPos();
                        TRACE(_T("m_rtMax(SearchStreams)=%s\n"), ReftimeToString(m_rtMax));
                    }
                }

                __int64 pos = GetPos();
                AddStream(0, b, h.id_ext, h.len);
                if (h.len) {
                    Seek(pos + h.len);
                }
            }
        } else if (m_type == mpeg_ts) {
            tshdr h;
            if (!Read(h)) {
                continue;
            }

            __int64 pos = GetPos();

            //UpdatePrograms(h);

            if (h.payload && ISVALIDPID(h.pid)) {
                peshdr h2;
                if (h.payloadstart && NextMpegStartCode(b, 4) && Read(h2, b)) { // pes packet
                    if (h2.type == mpeg2 && h2.scrambling) {
                        ASSERT(0);
                        return E_FAIL;
                    }

                    if (h2.fpts && CalcDuration && (m_AlternativeDuration || (GetMasterStream() && GetMasterStream()->GetHead() == h.pid))) {
                        if (m_rtPrec != _I64_MIN && abs(h2.pts - m_rtPrec) >= PTS_MAX_BEFORE_WRAP / 2) {
                            m_bPTSWrap = true;
                            m_rtMax = h2.pts;
                            m_posMax = GetPos();
                            TRACE(_T("PTS Wrap detected --> m_rtMax(SearchStreams)=%s\n"), ReftimeToString(m_rtMax));
                        }
                        m_rtPrec = h2.pts;

                        if ((m_rtMin == _I64_MAX) || (m_rtMin > h2.pts && m_rtMax > h2.pts && !m_bPTSWrap)) {
                            m_rtMin = h2.pts;
                            m_posMin = GetPos();
                            TRACE(_T("m_rtMin(SearchStreams)=%s, PID=%d\n"), ReftimeToString(m_rtMin), h.pid);
                        }

                        if (m_rtMax < h2.pts && (m_rtMin < h2.pts || m_bPTSWrap)) {
                            m_rtMax = h2.pts;
                            m_posMax = GetPos();
                            TRACE(_T("m_rtMax(SearchStreams)=%s, PID=%d\n"), ReftimeToString(m_rtMax), h.pid);
                            // Ugly code : to support BRD H264 seamless playback, CMultiFiles need to update m_rtPTSOffset variable
                            // each time a new part is open...
                            // use this code only if Blu-ray is detected
                            if (m_ClipInfo.IsHdmv()) {
                                for (size_t i = 0; i < m_ClipInfo.GetStreamNumber(); i++) {
                                    CHdmvClipInfo::Stream* stream = m_ClipInfo.GetStreamByIndex(i);
                                    if (stream->m_Type == VIDEO_STREAM_H264 && m_rtMin == 116506666) {
                                        CComQIPtr<ISyncReader>  pReader = pAsyncReader;
                                        if (pReader) {
                                            pReader->SetPTSOffset(&m_rtPTSOffset);
                                        }
                                        //TRACE(_T("UPDATE m_rtPTSOffset(SearchStreams)=%s\n"), ReftimeToString(m_rtPTSOffset));
                                        //TRACE(_T("m_rtMin(Boucle)=%s\n"), ReftimeToString(m_rtMin));
                                        //TRACE(_T("stream=%d\n"), stream->m_Type);
                                        //TRACE(_T("m_rtMax(Boucle)=%s\n"), ReftimeToString(m_rtMax));
                                        //TRACE(_T("m_rtMax - m_rtMin(Boucle)=%s\n"), ReftimeToString(m_rtMax - m_rtMin));
                                    }
                                }
                            }
                        }
                    }
                } else {
                    b = 0;
                }

                if (!CalcDuration) {
                    AddStream(h.pid, b, 0, DWORD(h.bytes - (GetPos() - pos)));
                }
            }

            Seek(h.next);
        } else if (m_type == mpeg_pva) {
            pvahdr h;
            if (!Read(h)) {
                continue;
            }

            if (h.fpts) {
                if (m_rtMin == _I64_MAX) {
                    m_rtMin = h.pts;
                    m_posMin = GetPos();
                }

                if (m_rtMin < h.pts && m_rtMax < h.pts) {
                    m_rtMax = h.pts;
                    m_posMax = GetPos();
                }
            }

            __int64 pos = GetPos();

            if (h.streamid == 1) {
                AddStream(h.streamid, 0xe0, 0, h.length);
            } else if (h.streamid == 2) {
                AddStream(h.streamid, 0xc0, 0, h.length);
            }

            if (h.length) {
                Seek(pos + h.length);
            }
        }
    }

    return S_OK;
}