javampeg2-tstransport-stream

Extract information from a Transport Stream in Java


I need to extract some information from a Transport Stream like PID, PAT, PMT, etc.

I found an example code to get the PID:

pid = ((buf[1] << 8) | (buf[2] & 0xff)) & 0x1fff;

But I could not understand the reason to get the buf[1] and shift 8 to the left side because to get the PID information I need get the 5 last bits from the buf[1] and all 8 from the buf[2]. I tested the code and the result was good. I just want to understand the mean of this first part: buf[1] << 8 in the equation. Could someone help me?


Solution

  • The PID is 13 bits long.

    The buffer buf is a byte array. Each element is 8 bits.

    The smallest Java integer type to hold a PID is the short (16-bit signed integer). buf[1] << 8 promotes the byte to an int (4 bytes) while shifting so you now have enough space to hold the result.

      buf[1] = XXXXXXXX
      buf[2] = YYYYYYYY
    
      buf[1] << 8 
      shifted 8 positions to the left in a 32-bit int
    
      0                   1                   2                   3
        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 X X X X X X X X 0 0 0 0 0 0 0 0| 
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    
    
      (buf[1] << 8) | buf[2]
    
      0                   1                   2                   3
        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 X X X X X X X X Y Y Y Y Y Y Y Y| 
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    
    
      ((buf[1] << 8) | buf[2]) & 0x1fff
    
      0                   1                   2                   3
        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 X X X X X Y Y Y Y Y Y Y Y| 
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    

    buf[2] & 0xff is needed because in Java all bytes are signed and you need the unsigned int value of the byte. Finally the whole thing is masked with 0x1fff to keep the relevant 13 bits. If you want a short you must cast the resulting int.