vdr-plugin-streamdev/remux/ts2ps.c

222 lines
5.0 KiB
C
Raw Normal View History

#ifdef STREAMDEV_PS
2004-12-30 23:43:55 +01:00
#include "remux/ts2ps.h"
2005-02-11 17:44:14 +01:00
#include "server/streamer.h"
#include <vdr/channels.h>
2005-11-06 17:43:58 +01:00
#include <vdr/device.h>
2004-12-30 23:43:55 +01:00
namespace Streamdev {
2004-12-30 23:43:55 +01:00
class cTS2PS {
friend void PutPES(uint8_t *Buffer, int Size, void *Data);
private:
ipack m_Ipack;
2005-02-11 17:44:14 +01:00
int m_Pid;
cRingBufferLinear *m_ResultBuffer;
2004-12-30 23:43:55 +01:00
public:
2005-02-11 17:44:14 +01:00
cTS2PS(cRingBufferLinear *ResultBuffer, int Pid, uint8_t AudioCid = 0x00);
2004-12-30 23:43:55 +01:00
~cTS2PS();
void PutTSPacket(const uint8_t *Buffer);
2005-02-11 17:44:14 +01:00
int Pid(void) const { return m_Pid; }
2004-12-30 23:43:55 +01:00
};
2005-02-11 17:44:14 +01:00
void PutPES(uint8_t *Buffer, int Size, void *Data)
{
2004-12-30 23:43:55 +01:00
cTS2PS *This = (cTS2PS*)Data;
2005-02-11 17:44:14 +01:00
int n = This->m_ResultBuffer->Put(Buffer, Size);
if (n != Size)
esyslog("ERROR: result buffer overflow, dropped %d out of %d byte", Size - n, Size);
2004-12-30 23:43:55 +01:00
}
} // namespace Streamdev
using namespace Streamdev;
2005-02-11 17:44:14 +01:00
cTS2PS::cTS2PS(cRingBufferLinear *ResultBuffer, int Pid, uint8_t AudioCid)
{
2004-12-30 23:43:55 +01:00
m_ResultBuffer = ResultBuffer;
2005-02-11 17:44:14 +01:00
m_Pid = Pid;
2004-12-30 23:43:55 +01:00
2005-02-11 17:44:14 +01:00
init_ipack(&m_Ipack, IPACKS, PutPES, false);
2004-12-30 23:43:55 +01:00
m_Ipack.cid = AudioCid;
m_Ipack.data = (void*)this;
}
2005-02-11 17:44:14 +01:00
cTS2PS::~cTS2PS()
{
free_ipack(&m_Ipack);
2004-12-30 23:43:55 +01:00
}
2005-02-11 17:44:14 +01:00
void cTS2PS::PutTSPacket(const uint8_t *Buffer)
{
2004-12-30 23:43:55 +01:00
if (!Buffer)
return;
if (Buffer[1] & 0x80) { // ts error
// TODO
}
if (Buffer[1] & 0x40) { // payload start
if (m_Ipack.plength == MMAX_PLENGTH - 6 && m_Ipack.found > 6) {
m_Ipack.plength = m_Ipack.found - 6;
m_Ipack.found = 0;
send_ipack(&m_Ipack);
reset_ipack(&m_Ipack);
}
}
uint8_t off = 0;
if (Buffer[3] & 0x20) { // adaptation field?
off = Buffer[4] + 1;
if (off + 4 > TS_SIZE - 1)
return;
}
instant_repack((uint8_t*)(Buffer + 4 + off), TS_SIZE - 4 - off, &m_Ipack);
}
2005-02-11 17:44:14 +01:00
cTS2PSRemux::cTS2PSRemux(int VPid, const int *APids, const int *DPids, const int *SPids):
m_NumTracks(0),
m_ResultBuffer(new cStreamdevBuffer(WRITERBUFSIZE, IPACKS)),
2005-02-11 17:44:14 +01:00
m_ResultSkipped(0),
m_Skipped(0),
m_Synced(false),
m_IsRadio(VPid == 0 || VPid == 1 || VPid == 0x1FFF)
{
m_ResultBuffer->SetTimeouts(100, 100);
2005-02-11 17:44:14 +01:00
if (VPid)
m_Remux[m_NumTracks++] = new cTS2PS(m_ResultBuffer, VPid);
if (APids) {
int n = 0;
while (*APids && m_NumTracks < MAXTRACKS && n < MAXAPIDS)
m_Remux[m_NumTracks++] = new cTS2PS(m_ResultBuffer, *APids++, 0xC0 + n++);
}
if (DPids) {
int n = 0;
while (*DPids && m_NumTracks < MAXTRACKS && n < MAXDPIDS)
m_Remux[m_NumTracks++] = new cTS2PS(m_ResultBuffer, *DPids++, 0x80 + n++);
}
2004-12-30 23:43:55 +01:00
}
cTS2PSRemux::~cTS2PSRemux() {
2005-02-11 17:44:14 +01:00
for (int i = 0; i < m_NumTracks; ++i)
delete m_Remux[i];
delete m_ResultBuffer;
}
int cTS2PSRemux::Put(const uchar *Data, int Count)
{
int used = 0;
// Make sure we are looking at a TS packet:
while (Count > TS_SIZE) {
if (Data[0] == TS_SYNC_BYTE && Data[TS_SIZE] == TS_SYNC_BYTE)
break;
Data++;
Count--;
used++;
}
if (used)
esyslog("ERROR: m_Skipped %d byte to sync on TS packet", used);
// Convert incoming TS data into multiplexed PS:
for (int i = 0; i < Count; i += TS_SIZE) {
if (Count - i < TS_SIZE)
break;
if (Data[i] != TS_SYNC_BYTE)
break;
if (m_ResultBuffer->Free() < 2 * IPACKS) {
m_ResultBuffer->WaitForPut();
2005-02-11 17:44:14 +01:00
break; // A cTS2PS might write one full packet and also a small rest
}
2005-02-11 17:44:14 +01:00
int pid = GetPid(Data + i + 1);
if (Data[i + 3] & 0x10) { // got payload
for (int t = 0; t < m_NumTracks; t++) {
if (m_Remux[t]->Pid() == pid) {
m_Remux[t]->PutTSPacket(Data + i);
break;
}
}
}
used += TS_SIZE;
}
// Check if we're getting anywhere here:
if (!m_Synced && m_Skipped >= 0)
m_Skipped += used;
return used;
2004-12-30 23:43:55 +01:00
}
2005-02-11 17:44:14 +01:00
uchar *cTS2PSRemux::Get(int &Count)
{
// Remove any previously skipped data from the result buffer:
if (m_ResultSkipped > 0) {
m_ResultBuffer->Del(m_ResultSkipped);
m_ResultSkipped = 0;
}
// Special VPID case to enable recording radio channels:
if (m_IsRadio) {
// Force syncing of radio channels to avoid "no useful data" error
m_Synced = true;
return m_ResultBuffer->Get(Count);
}
// Check for frame borders:
Count = 0;
uchar *resultData = NULL;
int resultCount = 0;
uchar *data = m_ResultBuffer->Get(resultCount);
if (data) {
for (int i = 0; i < resultCount - 3; i++) {
if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 1) {
int l = 0;
uchar StreamType = data[i + 3];
if (VIDEO_STREAM_S <= StreamType && StreamType <= VIDEO_STREAM_E) {
uchar pt = NO_PICTURE;
l = ScanVideoPacket(data, resultCount, i, pt);
if (l < 0)
return resultData;
if (pt != NO_PICTURE) {
if (pt < I_FRAME || B_FRAME < pt) {
esyslog("ERROR: unknown picture type '%d'", pt);
}
else if (!m_Synced) {
if (pt == I_FRAME) {
m_ResultSkipped = i; // will drop everything before this position
SetBrokenLink(data + i, l);
m_Synced = true;
}
}
else if (Count)
return resultData;
}
} else {
l = GetPacketLength(data, resultCount, i);
if (l < 0)
return resultData;
}
if (m_Synced) {
if (!Count)
resultData = data + i;
Count += l;
} else
m_ResultSkipped = i + l;
if (l > 0)
i += l - 1; // the loop increments, too
}
}
}
return resultData;
2004-12-30 23:43:55 +01:00
}
#endif