reading bits from file
-
Hi Guys, I've been searching extensively over the past couple of days but still have trouble reading bits from a file. I'm trying to read the header of an MPEG ts file. Here is what I have:
// header structure struct TS_PACKET_HEADER { unsigned int sync_byte:8; unsigned int payload_unit_start_indicator:1; unsigned int transport_error_indicator:1; unsigned int transport_priority:1; unsigned int PID:13; unsigned int scrambling_control:2; unsigned int adaptive_field_exists:2; unsigned int payload_data_exists:1; unsigned int continuity_counter:4; }; ... // the code to read from file into structure int size; unsigned char* buffer; std::ifstream inputFile("mpeg.ts", std::ios::in | std::ios::binary); if (inputFile.is_open()) { size = 4; buffer = new unsigned char[size]; inputFile.seekg(0); inputFile.read( reinterpret_cast(buffer), size); TS_PACKET_HEADER header = * (TS_PACKET_HEADER*) buffer; }
The first byte reads fine but the next two bytes (payload_unit_start_indicator, transport_error_indicator, transport_priority and PID) read incorrectly. Where am i going wrong and is there a better way to do this? Packet header information: http://en.wikipedia.org/wiki/Transport_stream#Important_elements_of_a_transport_stream[^] -
Hi Guys, I've been searching extensively over the past couple of days but still have trouble reading bits from a file. I'm trying to read the header of an MPEG ts file. Here is what I have:
// header structure struct TS_PACKET_HEADER { unsigned int sync_byte:8; unsigned int payload_unit_start_indicator:1; unsigned int transport_error_indicator:1; unsigned int transport_priority:1; unsigned int PID:13; unsigned int scrambling_control:2; unsigned int adaptive_field_exists:2; unsigned int payload_data_exists:1; unsigned int continuity_counter:4; }; ... // the code to read from file into structure int size; unsigned char* buffer; std::ifstream inputFile("mpeg.ts", std::ios::in | std::ios::binary); if (inputFile.is_open()) { size = 4; buffer = new unsigned char[size]; inputFile.seekg(0); inputFile.read( reinterpret_cast(buffer), size); TS_PACKET_HEADER header = * (TS_PACKET_HEADER*) buffer; }
The first byte reads fine but the next two bytes (payload_unit_start_indicator, transport_error_indicator, transport_priority and PID) read incorrectly. Where am i going wrong and is there a better way to do this? Packet header information: http://en.wikipedia.org/wiki/Transport_stream#Important_elements_of_a_transport_stream[^]Disable the padding and try.
#pragma pack(1)
// header structure
struct TS_PACKET_HEADER {
unsigned int sync_byte:8;
unsigned int payload_unit_start_indicator:1;
unsigned int transport_error_indicator:1;
unsigned int transport_priority:1;
unsigned int PID:13;
unsigned int scrambling_control:2;
unsigned int adaptive_field_exists:2;
unsigned int payload_data_exists:1;
unsigned int continuity_counter:4;
};Regards, Jijo.
_____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
-
Disable the padding and try.
#pragma pack(1)
// header structure
struct TS_PACKET_HEADER {
unsigned int sync_byte:8;
unsigned int payload_unit_start_indicator:1;
unsigned int transport_error_indicator:1;
unsigned int transport_priority:1;
unsigned int PID:13;
unsigned int scrambling_control:2;
unsigned int adaptive_field_exists:2;
unsigned int payload_data_exists:1;
unsigned int continuity_counter:4;
};Regards, Jijo.
_____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
-
Hi Guys, I've been searching extensively over the past couple of days but still have trouble reading bits from a file. I'm trying to read the header of an MPEG ts file. Here is what I have:
// header structure struct TS_PACKET_HEADER { unsigned int sync_byte:8; unsigned int payload_unit_start_indicator:1; unsigned int transport_error_indicator:1; unsigned int transport_priority:1; unsigned int PID:13; unsigned int scrambling_control:2; unsigned int adaptive_field_exists:2; unsigned int payload_data_exists:1; unsigned int continuity_counter:4; }; ... // the code to read from file into structure int size; unsigned char* buffer; std::ifstream inputFile("mpeg.ts", std::ios::in | std::ios::binary); if (inputFile.is_open()) { size = 4; buffer = new unsigned char[size]; inputFile.seekg(0); inputFile.read( reinterpret_cast(buffer), size); TS_PACKET_HEADER header = * (TS_PACKET_HEADER*) buffer; }
The first byte reads fine but the next two bytes (payload_unit_start_indicator, transport_error_indicator, transport_priority and PID) read incorrectly. Where am i going wrong and is there a better way to do this? Packet header information: http://en.wikipedia.org/wiki/Transport_stream#Important_elements_of_a_transport_stream[^]I have to wonder if this structure is correct:
botnet wrote:
// header structure struct TS_PACKET_HEADER { unsigned int sync_byte:8; unsigned int payload_unit_start_indicator:1; unsigned int transport_error_indicator:1; unsigned int transport_priority:1; unsigned int PID:13; unsigned int scrambling_control:2; unsigned int adaptive_field_exists:2; unsigned int payload_data_exists:1; unsigned int continuity_counter:4; };
The reason I ask is that it contains 33 bits, so it would read 8 bytes from the file. Also, there are many types of MPEG - MP3, MP4, MPEG, etc. You might want to check for the file format at www.wotsit.org Perhaps there are supposed to be some padding bits in there somewhere?
Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
Disable the padding and try.
#pragma pack(1)
// header structure
struct TS_PACKET_HEADER {
unsigned int sync_byte:8;
unsigned int payload_unit_start_indicator:1;
unsigned int transport_error_indicator:1;
unsigned int transport_priority:1;
unsigned int PID:13;
unsigned int scrambling_control:2;
unsigned int adaptive_field_exists:2;
unsigned int payload_data_exists:1;
unsigned int continuity_counter:4;
};Regards, Jijo.
_____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
Thanks for all your replies. I've read up on it a bit and have tried both pragma pack(1) before the struct and pragma pack(push, 1) ... pragma pack(pop) around the struct and both have not worked. This[^] also verifies what you have said. I havent disable padding though. I've look for how to do this but cant find it for either GCC of Visual Studio. I've also had a look at this[^] to no avail. The header was incorrect. I've removed payload_data_exists:1 and made adaptive_field_exists:2 http://www.erg.abdn.ac.uk/research/future-net/digital-video/mpeg2-trans.html[^]