PID Filter - Demultiplexer

From LinuxTVWiki
Revision as of 20:16, 1 November 2004 by Holger (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

A raw MPEG2 Transport Stream consists data of all the services transmitted on a particular transponder. The task on the receiver side is to filter out the interesting packets and schedule them to their target decoders: Audio packets to the audio-decoder, video packets to the video decoder, subtitle packets to the subtitle decoder etc...

MPEG2 TS packets are identified by the Packet ID, the PID. This is a 13-bit number located in the 2nd and 3rd byte of a TS packet.

PID Filter Algorithm

The algorithm is easy: we simply check the 13 PID bits and look whether this PID is enabled in our PID filter list.

PID Filter Sample Code (the easy way)

static inline
unsigned short ts_pid (const unsigned char *ts_packet)
{
        return ((ts_packet[1] & 0x1f) << 8) + ts_packet[2];
}


static
void process_ts_packet (struct demux *d, const unsigned char ts_packet[188])
{
        unsigned short pid = ts_pid(ts_packet);

        if (pid_is_enabled(pid))
               pass_packet_to_decoder(ts_packet);
}

PID Filter Sample Code (with sanity checks)

This slightly extended version of the above code contains additional sanity checks that have been useful in the past:

static inline
unsigned short ts_pid (const unsigned char *ts_packet)
{
        return ((ts_packet[1] & 0x1f) << 8) + ts_packet[2];
}


static
void process_ts_packet (struct demux *d, const unsigned char ts_packet[188])
{
        unsigned short pid = ts_pid(ts_packet);
        unsigned char scrambling_control;

        /**
         *  check TS error indicator bit and discard broken packets...
         */
        if (ts_packet[1] & 0x80) {
                bad_packet_counter++;
                return;
        }

        /**
         *  this makes no sense - however, some transponders like to send
         *  packets with scrambling bits set which contain gaga data (??).
         *
         *  In any case no scrambled packets should arrive in the demux,
         *  CSA descramblers are located before this stage of the pipeline.
         */
        scrambling_control = (ts_packet[3] >> 6) & 0x03;

        if (scrambling_control != 0)
                return 0;

        if (pid_is_enabled(pid))
               pass_packet_to_decoder(ts_packet);
}