示例#1
0
HRESULT CMpegSplitterFile::Init()
{
	HRESULT hr;

    SVP_LogMsg5(L"HRESULT CMpegSplitterFile::Init()");
	// get the type first

	m_type = us;

	Seek(0);

	if(m_type == us)
	{
		if(BitRead(32, true) == 'TFrc') Seek(0x67c);
		int cnt = 0, limit = 4;
		for(trhdr h; cnt < limit && Read(h); cnt++) Seek(h.next);
		if(cnt >= limit) m_type = ts;
	}

	Seek(0);

	if(m_type == us)
	{
		int cnt = 0, limit = 4;
		for(pvahdr h; cnt < limit && Read(h); cnt++) Seek(GetPos() + h.length);
		if(cnt >= limit) m_type = pva;
	}

	Seek(0);

	if(m_type == us)
	{
		BYTE b;
		for(int i = 0; (i < 4 || GetPos() < 65536) && m_type == us && NextMpegStartCode(b); i++)
		{
			if(b == 0xba)
			{
				pshdr h;
				if(Read(h)) 
				{
					m_type = ps;
					m_rate = int(h.bitrate/8);
					break;
				}
			}
			else if((b&0xe0) == 0xc0 // audio, 110xxxxx, mpeg1/2/3
				|| (b&0xf0) == 0xe0 // video, 1110xxxx, mpeg1/2
				// || (b&0xbd) == 0xbd) // private stream 1, 0xbd, ac3/dts/lpcm/subpic
				|| b == 0xbd) // private stream 1, 0xbd, ac3/dts/lpcm/subpic
			{
				peshdr h;
				if(Read(h, b) && BitRead(24, true) == 0x000001)
				{
					m_type = es;
				}
			}
		}
	}

	Seek(0);

	if(m_type == us)
	{
		return E_FAIL;
	}

	// min/max pts & bitrate
	m_rtMin = m_posMin = _I64_MAX;
	m_rtMax = m_posMax = 0;

	if(IsRandomAccess() || IsStreaming())
	{
		if(IsStreaming())
		{
			for(int i = 0; i < 20 || i < 50 && S_OK != HasMoreData(1024*100, 100); i++);
		}

		CAtlList<__int64> fps;
		for(int i = 0, j = 5; i <= j; i++)
			fps.AddTail(i*GetLength()/j);

		for(__int64 pfp = 0; fps.GetCount(); )
		{
			__int64 fp = fps.RemoveHead();
			fp = min(GetLength() - MEGABYTE/8, fp);
			fp = max(pfp, fp);
			__int64 nfp = fp + (pfp == 0 ? 5*MEGABYTE : MEGABYTE/8);
			if(FAILED(hr = SearchStreams(fp, nfp)))
				return hr;
			pfp = nfp;
		}
	}
	else
	{
		if(FAILED(hr = SearchStreams(0, MEGABYTE/8)))
			return hr;
	}

	if(m_posMax - m_posMin <= 0 || m_rtMax - m_rtMin <= 0)
		return E_FAIL;

	int indicated_rate = m_rate;
	int detected_rate = int(10000000i64 * (m_posMax - m_posMin) / (m_rtMax - m_rtMin));
	// normally "detected" should always be less than "indicated", but sometimes it can be a few percent higher (+10% is allowed here)
	// (update: also allowing +/-50k/s)
	if(indicated_rate == 0 || ((float)detected_rate / indicated_rate) < 1.1
	|| abs(detected_rate - indicated_rate) < 50*1024)
		m_rate = detected_rate;
	else ; // TODO: in this case disable seeking, or try doing something less drastical...

//#ifndef DEBUG
	if(m_streams[audio].GetCount() < 1 && m_type == ts){
		SVP_LogMsg5(_T("ts and no audio %d %d"), m_streams[audio].GetCount(), m_streams[unknown].GetCount());
		return E_FAIL;
	}
	if(m_streams[video].GetCount())
	{
		if (!m_bIsHdmv && m_streams[subpic].GetCount())
		{
			stream s;
			s.mt.majortype = MEDIATYPE_Video;
			s.mt.subtype = MEDIASUBTYPE_DVD_SUBPICTURE;
			s.mt.formattype = FORMAT_None;
			m_streams[subpic].Insert(s, this);
		}
		else
		{
			// Add fake stream for "No subtitle"
			AddHdmvPGStream (NO_SUBTITLE_PID, "---");
		}
	}else if(m_type == ts){
		SVP_LogMsg5(_T("ts and no video"));
		return E_FAIL;
	}
//#endif

	Seek(0);

    SVP_LogMsg5(_T("ts %d %d %d"),  m_streams[video].GetCount() , m_streams[audio].GetCount(), m_bIsHdmv);
	return S_OK;
}
示例#2
0
HRESULT CMpegSplitterFile::Init(IAsyncReader* pAsyncReader)
{
    HRESULT hr;

    // get the type first

    m_type = mpeg_us;

    Seek(0);

    if (m_type == mpeg_us) {
        if (BitRead(32, true) == 'TFrc') {
            Seek(0x67c);
        }
        int cnt = 0, limit = 4;
        for (tshdr h; cnt < limit && Read(h); cnt++) {
            Seek(h.next);
        }
        if (cnt >= limit) {
            m_type = mpeg_ts;
        }
    }

    Seek(0);

    if (m_type == mpeg_us) {
        if (BitRead(32, true) == 'TFrc') {
            Seek(0xE80);
        }
        int cnt = 0, limit = 4;
        for (tshdr h; cnt < limit && Read(h); cnt++) {
            Seek(h.next);
        }
        if (cnt >= limit) {
            m_type = mpeg_ts;
        }
    }

    Seek(0);

    if (m_type == mpeg_us) {
        int cnt = 0, limit = 4;
        for (pvahdr h; cnt < limit && Read(h); cnt++) {
            Seek(GetPos() + h.length);
        }
        if (cnt >= limit) {
            m_type = mpeg_pva;
        }
    }

    Seek(0);

    if (m_type == mpeg_us) {
        BYTE b;
        for (int i = 0; (i < 4 || GetPos() < MAX_PROBE_SIZE) && m_type == mpeg_us && NextMpegStartCode(b); i++) {
            if (b == 0xba) {
                pshdr h;
                if (Read(h)) {
                    m_type = mpeg_ps;
                    m_rate = int(h.bitrate / 8);
                    break;
                }
            } else if ((b & 0xe0) == 0xc0 // audio, 110xxxxx, mpeg1/2/3
                       || (b & 0xf0) == 0xe0 // video, 1110xxxx, mpeg1/2
                       // || (b&0xbd) == 0xbd) // private stream 1, 0xbd, ac3/dts/lpcm/subpic
                       || b == 0xbd) { // private stream 1, 0xbd, ac3/dts/lpcm/subpic
                peshdr h;
                if (Read(h, b) && BitRead(24, true) == 0x000001) {
                    m_type = mpeg_es;
                }
            }
        }
    }

    Seek(0);

    if (m_type == mpeg_us) {
        return E_FAIL;
    }

    // min/max pts & bitrate
    m_rtMin = m_posMin = _I64_MAX;
    m_rtMax = m_posMax = 0;
    m_rtPrec = _I64_MIN;
    m_bPTSWrap = false;

    m_init = true;

    if (IsRandomAccess() || IsStreaming()) {
        if (IsStreaming()) {
            for (int i = 0; i < 20 || i < 50 && S_OK != HasMoreData(MEGABYTE, 100); i++) {
                ;
            }
        }

        SearchPrograms(0, min(GetLength(), MEGABYTE * 5)); // max 5Mb for search a valid Program Map Table

        __int64 pfp = 0;
        const int k = 5;
        for (int i = 0; i <= k; i++) {
            __int64 fp = i * GetLength() / k;
            fp = min(GetLength() - MEGABYTE / 8, fp);
            fp = max(pfp, fp);
            __int64 nfp = fp + (pfp == 0 ? 10 * MEGABYTE : MEGABYTE / 8);
            if (FAILED(hr = SearchStreams(fp, nfp, pAsyncReader))) {
                return hr;
            }
            pfp = nfp;
        }
    } else {
        if (FAILED(hr = SearchStreams(0, MEGABYTE / 8, pAsyncReader))) {
            return hr;
        }
    }

    if (m_type == mpeg_ts) {
        if (IsRandomAccess() || IsStreaming()) {
            if (IsStreaming()) {
                for (int i = 0; i < 20 || i < 50 && S_OK != HasMoreData(1024 * 100, 100); i++) {
                    ;
                }
            }

            __int64 pfp = 0;
            const int k = 5;
            for (int i = 0; i <= k; i++) {
                __int64 fp = i * GetLength() / k;
                fp = min(GetLength() - MEGABYTE / 8, fp);
                fp = max(pfp, fp);
                __int64 nfp = fp + (pfp == 0 ? 10 * MEGABYTE : MEGABYTE / 8);
                if (FAILED(hr = SearchStreams(fp, nfp, pAsyncReader, TRUE))) {
                    return hr;
                }
                pfp = nfp;
            }
        } else {
            if (FAILED(hr = SearchStreams(0, MEGABYTE / 8, pAsyncReader, TRUE))) {
                return hr;
            }
        }
    }

    if (m_posMax - m_posMin <= 0 || (m_rtMax - m_rtMin <= 0 && !m_bPTSWrap)) {
        return E_FAIL;
    }

    m_init = false;

    int indicated_rate = m_rate;
    REFERENCE_TIME dur = !m_bPTSWrap ? (m_rtMax - m_rtMin) : (PTS_MAX_BEFORE_WRAP - m_rtMin + m_rtMax);
    int detected_rate = int(10000000i64 * (m_posMax - m_posMin) / dur);

    m_rate = detected_rate ? detected_rate : m_rate;
#if (0)
    // normally "detected" should always be less than "indicated", but sometimes it can be a few percent higher (+10% is allowed here)
    // (update: also allowing +/-50k/s)
    if (indicated_rate == 0 || ((float)detected_rate / indicated_rate) < 1.1 || abs(detected_rate - indicated_rate) < 50 * 1024) {
        m_rate = detected_rate;
    } else {
        ;    // TODO: in this case disable seeking, or try doing something less drastical...
    }
#endif

    // Add fake subtitle stream...
    if (m_streams[video].GetCount() && m_streams[subpic].GetCount()) {
        if (m_type == mpeg_ts && m_bIsHdmv) {
            AddHdmvPGStream(NO_SUBTITLE_PID, "---");
        } else {
            stream s;
            s.pid = NO_SUBTITLE_PID;
            s.mt.majortype = m_streams[subpic].GetHead().mt.majortype;
            s.mt.subtype = m_streams[subpic].GetHead().mt.subtype;
            s.mt.formattype = m_streams[subpic].GetHead().mt.formattype;
            m_streams[subpic].Insert(s, this);
        }
    }

    Seek(0);

    return S_OK;
}