From 9b386a578682ce4c83e98bddbc3bea2cdae2dfbb Mon Sep 17 00:00:00 2001 From: Mark Calderbank Date: Tue, 30 Oct 2007 22:35:07 +0000 Subject: [PATCH] Demuxer changes/fixes/improvements --- demuxer.cc | 38 +++++++++++-------------- demuxer.h | 12 ++++---- demuxeraudio.cc | 1 + demuxeraudio.h | 2 ++ demuxerts.cc | 75 +++++++++++++++++++++++++++++++------------------ demuxerts.h | 1 + demuxervdr.cc | 3 ++ 7 files changed, 76 insertions(+), 56 deletions(-) diff --git a/demuxer.cc b/demuxer.cc index 5054dfb..b9ec4ab 100644 --- a/demuxer.cc +++ b/demuxer.cc @@ -20,17 +20,12 @@ */ #include "demuxer.h" -#ifndef WIN32 -#include -#else -#define __LITTLE_ENDIAN 1234 -#define __BIG_ENDIAN 4321 -#define __BYTE_ORDER __LITTLE_ENDIAN -#endif + +#include "callback.h" +#include "log.h" #define DEMUXER_SEQ_HEAD 0x000001B3 #define DEMUXER_PIC_HEAD 0x00000101 - #define SEEK_THRESHOLD 150000 // About 1.5 seconds // Statics @@ -53,7 +48,6 @@ void PESPacket::init(UCHAR type) data[3] = type; data[4] = data[5] = 0; packetType = type; - pts = PTS_INVALID; seq_header = 1; // Unknown seq_header status } @@ -72,23 +66,25 @@ int PESPacket::write(UCHAR *buf, int len) data[5] = (length & 0xFF); // We have added data - reset seq_header indicator if necessary if (seq_header == 0) seq_header = 1; // Reset to 'unknown' - // Extract PTS if this is the first write to reach 14 bytes - if (size - len < 14 && size >= 14 && data[7] & 0x80 && - ( (packetType >= Demuxer::PESTYPE_AUD0 && + return 1; +} + +ULLONG PESPacket::getPTS() +{ + if ( ( (packetType >= Demuxer::PESTYPE_AUD0 && packetType <= Demuxer::PESTYPE_AUDMAX) - || + || (packetType >= Demuxer::PESTYPE_VID0 && packetType <= Demuxer::PESTYPE_VIDMAX) - )) + ) && size >= 14 && data[7] & 0x80) { - // 14 bytes are now in, and PTS_DTS_flags indicate PTS - pts = ( (ULLONG)(data[ 9] & 0x0E) << 29 ) | - ( (ULLONG)(data[10]) << 22 ) | - ( (ULLONG)(data[11] & 0xFE) << 14 ) | - ( (ULLONG)(data[12]) << 7 ) | - ( (ULLONG)(data[13] & 0xFE) >> 1 ); + return ( (ULLONG)(data[ 9] & 0x0E) << 29) | + ( (ULLONG)(data[10]) << 22 ) | + ( (ULLONG)(data[11] & 0xFE) << 14 ) | + ( (ULLONG)(data[12]) << 7 ) | + ( (ULLONG)(data[13] & 0xFE) >> 1 ); } - return 1; + else return PTS_INVALID; } UCHAR PESPacket::operator[] (UINT index) diff --git a/demuxer.h b/demuxer.h index 10ffee4..a2313dc 100644 --- a/demuxer.h +++ b/demuxer.h @@ -30,13 +30,11 @@ however, no code was copied verbatim. #ifndef DEMUXER_H #define DEMUXER_H -#include -#include #include "stream.h" -#include "log.h" #include "defines.h" -#include "callback.h" -#include "draintarget.h" + +class Callback; +class DrainTarget; class PESPacket { @@ -54,8 +52,8 @@ class PESPacket UCHAR getPacketType() { return packetType; } void setSubstream(UCHAR s) { substream = s; } UCHAR getSubstream() { return substream; } - bool hasPTS() { return (pts != PTS_INVALID); } - ULLONG getPTS() { return pts; } + ULLONG getPTS(); + bool hasPTS() { return (getPTS() != PTS_INVALID); } UINT findPictureHeader(); UINT findSeqHeader(); diff --git a/demuxeraudio.cc b/demuxeraudio.cc index 72cf3fe..54319e6 100644 --- a/demuxeraudio.cc +++ b/demuxeraudio.cc @@ -21,6 +21,7 @@ #include "demuxeraudio.h" #include "audio.h" #include "i18n.h" +#include "log.h" #define HDRBYTE1 0xff #define HDRBYTE2 0xe0 diff --git a/demuxeraudio.h b/demuxeraudio.h index 95dbd11..b8035c6 100644 --- a/demuxeraudio.h +++ b/demuxeraudio.h @@ -25,7 +25,9 @@ #include "defines.h" #include "id3.h" +class Log; class PacketBuffer; + class DemuxerAudio : public Demuxer { public: diff --git a/demuxerts.cc b/demuxerts.cc index f139d56..f06727b 100644 --- a/demuxerts.cc +++ b/demuxerts.cc @@ -19,6 +19,7 @@ */ #include "demuxerts.h" +#include "log.h" DemuxerTS::DemuxerTS(int p_vID, int p_aID) { @@ -31,23 +32,29 @@ void DemuxerTS::flush() partPacket = 0; parsed = false; Demuxer::flush(); - vPacket.init(0xE0); - aPacket.init(0xC0); + vPacket.init(PESTYPE_VID0); + aPacket.init(PESTYPE_AUD0); + vActive = false; + aActive = false; } int DemuxerTS::scan(UCHAR *buf, int len) { - return 0xc0; + return PESTYPE_AUD0; } void DemuxerTS::setVID(int p_vID) { vID = p_vID; + vPacket.init(PESTYPE_VID0); + vActive = false; } void DemuxerTS::setAID(int p_aID) { aID = p_aID; + aPacket.init(PESTYPE_AUD0); + aActive = false; } int DemuxerTS::findPTS(UCHAR* buf, int len, ULLONG* dest) @@ -180,54 +187,66 @@ int DemuxerTS::processTS(UCHAR* buf) if (pid == vID) { - if (!parsed) + if (vActive) { - parsePacketDetails(vPacket); - parsed = true; - } + if (!parsed) + { + parsePacketDetails(vPacket); + parsed = true; + } rc = submitPacket(vPacket); + } + vActive = true; } if (pid == aID) { - if (!parsed) + if (aActive) { - parsePacketDetails(aPacket); - parsed = true; - } + if (!parsed) + { + parsePacketDetails(aPacket); + parsed = true; + } rc = submitPacket(aPacket); + } + aActive = true; } if (rc == 0) return 0; parsed = false; if (pid == vID) { - vPacket.init(0xE0); + vPacket.init(PESTYPE_VID0); buf += 6; datalen -= 6; } if (pid == aID) { - aPacket.init(0xC0); + aPacket.init(PESTYPE_AUD0); buf += 6; datalen -= 6; } } - PESPacket* packet = NULL; - if (pid == vID) packet = &vPacket; - if (pid == aID) packet = &aPacket; - if (packet != NULL) + if ( (pid == vID && vActive) || + (pid == aID && aActive) ) { - if (packet->write(buf, datalen) == 0) - { // Writing to packet failed. It has overflowed. - if (!parsed) - { - parsePacketDetails(*packet); - parsed = true; + PESPacket* packet = NULL; + if (pid == vID) packet = &vPacket; + if (pid == aID) packet = &aPacket; + if (packet != NULL) + { + if (packet->write(buf, datalen) == 0) + { // Writing to packet failed. It has overflowed. + if (!parsed) + { + parsePacketDetails(*packet); + parsed = true; + } + if (submitPacket(*packet) == 0) return 0; + parsed = false; + packet->truncate(); + packet->write((UCHAR*)"\200\000\000", 3); + packet->write(buf, datalen); } - if (submitPacket(*packet) == 0) return 0; - parsed = false; - packet->truncate(); - packet->write((UCHAR*)"\200\000\000", 3); - packet->write(buf, datalen); } } diff --git a/demuxerts.h b/demuxerts.h index 9a6ede0..383094f 100644 --- a/demuxerts.h +++ b/demuxerts.h @@ -47,6 +47,7 @@ class DemuxerTS : public Demuxer PESPacket vPacket; // Video PES packet under construction PESPacket aPacket; // Audio PES packet under construction int vID, aID; // TS IDs for video/audio + bool vActive, aActive; // Whether video/audio is actively being captured }; #endif diff --git a/demuxervdr.cc b/demuxervdr.cc index 715433a..a414788 100644 --- a/demuxervdr.cc +++ b/demuxervdr.cc @@ -19,7 +19,10 @@ */ #include "demuxervdr.h" + #include "video.h" +#include "log.h" + #ifndef WIN32 #include #else -- 2.39.5