简体   繁体   English

从Java的传输流中提取信息

[英]Extract information from a Transport Stream in Java

I need to extract some information from a Transport Stream like PID, PAT, PMT, etc. 我需要从传输流中提取一些信息,例如PID,PAT,PMT等。

I found an example code to get the PID: 我找到了获取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] . 但我无法理解得到的原因buf[1]和移位8左侧,因为得到我需要从一开始最后5个比特的PID信息buf[1]从所有8 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. 我只想了解第一部分的意思:等式中的buf[1] << 8 Could someone help me? 有人可以帮我吗?

Lets say your PID is 4660 or 0x1234. 假设您的PID是4660或0x1234。 so buf[1] = 0x12 , and buf[2] = 0x34 所以buf[1] = 0x12 ,而buf[2] = 0x34

Lest do the math int a = 0x12 | 0x32 唯恐数学int a = 0x12 | 0x32 int a = 0x12 | 0x32 what is a set to? int a = 0x12 | 0x32设置为什么? a = 0x36 . a = 0x36

0x36 != 0x1234 . 0x36 != 0x1234 What we need is int a = 0x1200 | 0x34 我们需要的是int a = 0x1200 | 0x34 int a = 0x1200 | 0x34 to get 0x1234 int a = 0x1200 | 0x34得到0x1234

How do we turn 0x12 into 0x1200 ? 我们如何将0x12变成0x1200 We shift it left by 8 bits. 我们将其左移8位。 (buf[1] << 8)

The PID is 13 bits long. PID为13位长。

The buffer buf is a byte array. 缓冲区buf是一个byte数组。 Each element is 8 bits. 每个元素是8位。

The smallest Java integer type to hold a PID is the short (16-bit signed integer). 持有PID的最小Java整数类型是short (16位有符号整数)。 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] << 8促进byteint (4个字节),而移位,以便你现在有足够的空间容纳的结果。

  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. buf[2] & 0xff ,因为在Java中所有字节都是带符号的,并且您需要字节的unsigned int值。 Finally the whole thing is masked with 0x1fff to keep the relevant 13 bits. 最后,整个对象都用0x1fff屏蔽以保留相关的13位。 If you want a short you must cast the resulting int . 如果要short ,则必须转换结果int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM