Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Easy pointer question - passing

Posted on 2006-04-09
3
Medium Priority
?
283 Views
Last Modified: 2010-04-01
I have some simple code to read in some information from an mp3 and spit it out.  Problem is, it doesn't work.  I haven't worked with C or C++ for well over a year, and I'm *quite* rusty on pointers.  I know exactly what's wrong with the code and why it's bombing (it always spits out mpeg 2.5 and default in printLayer cause *header is 255), I just don't know how to read the data I'm looking for.  You'll see what I'm trying to do when you see the code.  If you test this and it says it can't find a frame header, use another mp3.  I was using 2 bytes for the sake of simplicity, and some mp3s' frame headers are on odd boundries.

Here ya go:

#define HEADERBYTES 2

void printType (unsigned char* header)
{
      switch (*header & 0x1800) // 00000000000110000000000000000000
      {
      case 00:
            cout << "MPEG Version 2.5";
            break;
      case 10:
            cout << "MPEG Version 2";
            break;
      case 11:
            cout << "MPEG Version 1";
            break;
      default:
            cout << "Something weird happened...";
      }
}

void printLayer (unsigned char* header)
{
      switch (*header & 0x6000) // 00000000000001100000000000000000
      {
      case 01:
            cout << "MPEG Layer 3";
            break;
      case 10:
            cout << "MPEG Layer 2";
            break;
      case 11:
            cout << "MPEG Layer 1";
            break;
      default:
            cout << "More weirdness...";
      }
}

int getFileSize (string &sFilename)
{
      struct _stat statBuffer;

      if (_stat(sFilename.c_str(), &statBuffer) != 0)
      {
            cout << "can't get file size" << endl;
            return 0;
      }

      return statBuffer.st_size;
}

int findFrame (ifstream &in)
{
      int iBytesRead = 0;
      unsigned char *mp3Header = new unsigned char[HEADERBYTES];
      int iFileSize = getFileSize(string("test.mp3"));
      
      for (; (iBytesRead + HEADERBYTES) < iFileSize; iBytesRead += HEADERBYTES)
      {
            in.read((char *)mp3Header, HEADERBYTES);

            if ((mp3Header[0] == 0xFF) && ((mp3Header[1] & 0xE0) == 0xE0))
            {
                  cout << "found header at " << iBytesRead << endl;
                  return iBytesRead;
            }
            in.seekg(iBytesRead + HEADERBYTES);
      }
      return -1;
}

int main(int argc, char *argv[])
{
      ifstream in;
      int iFirstFramePos = 0;

      in.open("test.mp3");

      if (!in)
      {
            cout << "Can't open test.mp3" << endl;
            return 0;
      }
      
      int iPos = findFrame(in);

      if (iPos == -1)
      {
            cout << "Couldn't find first frame header!" << endl;
            return 0;
      }

      unsigned char *pFrameHeader = new unsigned char[4];
      in.seekg(iPos);
      in.read((char *)pFrameHeader, 4);

      printType(pFrameHeader);
      printLayer(pFrameHeader);
      return 0;
}

If you're not sure what I'm trying to get at, http://pipin.tmd.ns.ac.yu/extra/fileformat/modules/mp3/mpeghdr.htm will clear it all up.  How can I pass my frame as a contiguous stream of bits?  If the bits were on even numbered boundaries I wouldn't be having this problem, but all solutions I've come up with so far work, but are total hacks.

Thanks for any help!
0
Comment
Question by:dbunder
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
jhshukla earned 450 total points
ID: 16414703
found at least one error. below is correct version:
void printType (unsigned char* header)
{
     switch (header[1] & 0x18) // 00000000 00011000 00000000 00000000
     {
     case 0x00:
          cout << "MPEG Version 2.5";
          break;
     case 0x10:
          cout << "MPEG Version 2";
          break;
     case 0x18:
          cout << "MPEG Version 1";
          break;
     default:
          cout << "Something weird happened...";
     }
}
==================================
other thing to keep in mind is that there might be multiple possible occurences of FrameSync signature although all of them are not really framesyncs. a good way to tell whether the signature you found is an actual sync is to go just past the end of the frame and find a sync again. else, go back to the place where you stopped reading (i.e. EEEEFFGH byte) and start reading from there (EEEEFFGH included in new reading).
btw i am wondering why are you reading 2 bytes at a time? shouldn't you be reading one byte at a time? what if the second byte of mp3Header is actually the first byte and you discarded it because the preceding byte was not 0xFF?
0
 
LVL 1

Author Comment

by:dbunder
ID: 16414728
After I posted the message I went in and wrote almost the same code.  This is just test code, so I'm not worrying about too much.  Was using 2 bytes for the sake of simplicity (most frame headers are on even numbered bytes in my experience) but that will change.  And I'll be doing more robust frame header checking in the future, yes.

Let's just consider this closed, since I figured it out and wrote more or less the same code as you :)
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 16414732
no prob
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question