Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. reading bits from file

reading bits from file

Scheduled Pinned Locked Moved C / C++ / MFC
iosalgorithmsquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    botnet
    wrote on last edited by
    #1

    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[^]

    J K 2 Replies Last reply
    0
    • B botnet

      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[^]

      J Offline
      J Offline
      Jijo Raj
      wrote on last edited by
      #2

      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.

      A B 2 Replies Last reply
      0
      • J Jijo Raj

        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.

        A Offline
        A Offline
        achainard
        wrote on last edited by
        #3

        A solution more secure : #pragma pack (push, 1) .... #pragma pack (pop) An other things, your structure size is : 33 bits. Align it to 40 bits or 64 bits by adding a fake data at the end. bye... ...

        1 Reply Last reply
        0
        • B botnet

          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[^]

          K Offline
          K Offline
          krmed
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • J Jijo Raj

            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.

            B Offline
            B Offline
            botnet
            wrote on last edited by
            #5

            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[^]

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups