-1

I am trying to create mpeg header using c++: Header hex value : 0X4700311d so the sync byte is :0x47 tei : 0 pusi : 0 priority : 0 pid : 0x0031 tsc : 0x0 afc : 0x1 cc : 13

How should the structure of the mpeg header should look like?

Is there a way to create a char array of 4 bytes and then assign values according to the header and later on modify the pid if i want to ?

I have tried to use this header:

unsigned char _syncByte;
unsigned char _tei : 1;
unsigned char _payloadStart : 1;
unsigned char _priority : 1;
int16_t _pid;
unsigned char _scramblingCtl : 2;
unsigned char _adaptationField : 2;
unsigned  char _counter;

but in visual studio 2015 memory panel the values are different.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
shasanka
  • 33
  • 6
  • 2
    If you want naked struct to do this then you need to be careful about struct padding which is compiler-dependent. I recommend you do it differently: create a structure with integers and bools for settings and a function that will return to you the according mpeg header. – user1316208 Aug 20 '19 at 10:37
  • If the sync byte is 8 bits, followed by three bits of flags, followed by 16 bits of pid, then a sync byte of `0x47` and a pid of `0x0031` cannot possibly result in a header that starts with `0x470031` - there should be three bits in between. – Max Langhof Aug 20 '19 at 10:40
  • @user1316208 thanks for the reply , i am new to this bit stuff, the header length is 4 bytes, if i create a struct with bool and integers then the struct size will be more than 4 bytes. – shasanka Aug 20 '19 at 10:41
  • 1
    Yes, the struct size will be different. The point is that you would write a function that takes this clearly organized struct and packs its values into e.g. a `std::bitset` or into a `char[4]` or similar. The suggestion was to _give up_ on creating a `struct ` that is exactly 4 bytes large and has all the values in it. – Max Langhof Aug 20 '19 at 10:43
  • @MaxLanghof thanks for the reply , then how do i set the values , sync byte should always 0x47 and pid for example will be 31 , how do i set all the values in header? – shasanka Aug 20 '19 at 10:43
  • I don't know how they should be set, I am just saying your example is wrong according to your own explanation/layout shown. You made a mistake in at least one of these: a) The header hex value, b) the field values that you expect, c) the header layout you show. I don't know where the error is but these three things in your question contradict each other. Maybe the compiler does exactly what you tell it to and you think it's wrong because you didn't gather your example correctly. Also note that there are only 31 bits in your shown header layout, not 32 - that looks suspicious. – Max Langhof Aug 20 '19 at 10:44
  • @MaxLanghof std::bitset<32>(*((uint32_t *)header)) , you mean this ? – shasanka Aug 20 '19 at 10:47
  • No, that won't magically pack more than 4 bytes into 4 bytes. You need to do it by hand. But before you try _any_ further code, please fix the inconsistency explained above. – Max Langhof Aug 20 '19 at 10:48
  • @MaxLanghof i am beginner with this bit stuff , i opened a mpeg file in wireshark and in the header it shows 0x4700311d. now my issue is that i need to create a mpeg header , and edit the the values later – shasanka Aug 20 '19 at 10:49
  • Then `pid` is not actually `0x0031`. But since this is not a forum and this confusion has little to do with the original question I can't really help you any further. Write out the 4 bytes as bits by hand, then group those bits according to the spec, then interpret them. Then you should have a viable example. – Max Langhof Aug 20 '19 at 10:52
  • @MaxLanghof thanks , i will try that – shasanka Aug 20 '19 at 10:54

1 Answers1

1
uint32_t header = 0;
header |= 0x47 << 24;
header |= (TransportErrorIndicator & 1) << 23;
header |= (PayloadUnitStartIndicator  & 1) << 22;
header |= (TransportPriority & 1) << 21;
header |= (PacketIdentifier & 8191) << 8;
header |= (TransportScrambling & 3) << 6;
header |= (AdaptationFieldControl & 3) << 4;
header |= (ContinuityCounter & 15);

Be careful of byte ordering.

szatmary
  • 29,969
  • 8
  • 44
  • 57