예제 #1
0
파일: srvcgi.cpp 프로젝트: bduck/livecode
bool MCStreamCache::Read(void *p_buffer, uint32_t p_offset, uint32_t p_length, uint32_t &r_read)
{
	bool t_success = true;
	
	uint32_t t_to_read;
	t_to_read = 0;
	
	uint32_t t_read;
	t_read = 0;
	
	if (p_offset < m_cache_length)
	{
		t_to_read = MCMin(m_cache_length - p_offset, p_length);
		t_success = ReadFromCache(p_buffer, p_offset, t_to_read, t_read);
	}
	
	r_read = t_read;
	
	if (t_success && t_read != t_to_read)
		return true;
	
	if (t_success)
	{
		t_to_read = p_length - t_read;
		t_read = 0;
		if (t_to_read > 0)
			t_success = ReadFromStream((uint8_t*)p_buffer + r_read, t_to_read, t_read);
		
		r_read += t_read;
	}
	
	return t_success;
}
예제 #2
0
파일: Midi.cpp 프로젝트: bjj/midiquiz
Midi Midi::ReadFromFile(const wstring &filename)
{
#if defined WIN32
   fstream file(reinterpret_cast<const wchar_t*>((filename).c_str()), ios::in|ios::binary);
#else
   // TODO: This isn't Unicode!
   // MACTODO: Test to see if opening a unicode filename works.  I bet it doesn't.
   std::string narrow(filename.begin(), filename.end());
   fstream file(narrow.c_str(), ios::in | ios::binary);
#endif

   if (!file.good()) throw MidiError(MidiError_BadFilename);

   Midi m;

   try
   {
      m = ReadFromStream(file);
   }
   catch (const MidiError &e)
   {
      // Close our file resource before handing the error up
      file.close();
      throw e;
   }

   return m;
}
예제 #3
0
파일: csNameMapper.cpp 프로젝트: asir6/Colt
EcsCsvStatus TcsNameMapper::ReadFromStream (std::wistream& inStrm)
{
    EcsCsvStatus rtnStatus;
	TcsCsvStatus csvStatus;

    rtnStatus = ReadFromStream (inStrm,csvStatus);
	return rtnStatus;
}
예제 #4
0
 bool XMLNode::ReadFromFile(const std::string& file_name) {
   std::ifstream in(file_name.c_str(), std::ios::in);
   if (!in)
     return false;
   bool r = ReadFromStream(in);
   in.close();
   return r;
 }
예제 #5
0
TreeList::TreeList(string filename)	{

	mParam = 0;
	mSize = 0;
	mTreeArray = 0;
	ifstream is(filename.c_str());
	ReadFromStream(is);
	SetParameters();
}
예제 #6
0
//-----------------------------------------------------------------------------------
void AnimationMotion::ReadFromFile(const char* filename)
{
    BinaryFileReader reader;
    ASSERT_OR_DIE(reader.Open(filename), "File Open failed!");
    {
        ReadFromStream(reader);
    }
    reader.Close();
}
예제 #7
0
void CItemEnhancement::ReadFromStream (SLoadCtx &Ctx)

//	ReadFromStream
//
//	Reads an enhancement

{
    ReadFromStream(Ctx.dwVersion, Ctx.pStream);
}
예제 #8
0
//
// Load
//
// Load all the data from the given stream
STDMETHODIMP CPersistStream::Load(LPSTREAM pStm)
{
    HRESULT hr;
    // Load the version number then the data
    mPS_dwFileVersion = ReadInt(pStm, hr);
    if (FAILED(hr)) {
        return hr;
    }

    return ReadFromStream(pStm);
}  // Load
예제 #9
0
Tree::Tree(string filename)	{

	mParam = 0;
	Name = "T";
	ifstream is(filename.c_str());
	if (! is)	{
		cerr << "error : non existing file : " << filename << '\n';
		exit(1);
	}
	ReadFromStream(is,1);
	FinishCreation();

}
예제 #10
0
ALERROR CG16bitFont::CreateFromResource (HINSTANCE hInst, char *pszRes)

//	CreateFromResource
//
//	Loads the font from a resource
//	Use DirectXFont to save a font to a file

	{
	ALERROR error;
	HRSRC hRes;
	HGLOBAL hGlobalRes;
	void *pImage;
	int iSize;

	//	Load the resource

	hRes = ::FindResource(hInst, pszRes, "DXFN");
	if (hRes == NULL)
		return ERR_FAIL;

	iSize = ::SizeofResource(hInst, hRes);
	if (iSize == 0)
		return ERR_FAIL;

	hGlobalRes = ::LoadResource(hInst, hRes);
	if (hGlobalRes == NULL)
		return ERR_FAIL;

	pImage = ::LockResource(hGlobalRes);
	if (pImage == NULL)
		return ERR_FAIL;

	//	Load

	CMemoryReadStream Stream((char *)pImage, iSize);
	if (error = Stream.Open())
		return error;

	if (error = ReadFromStream(&Stream))
		return error;

	Stream.Close();

	return NOERROR;
	}
예제 #11
0
nsresult nsRawReader::ReadMetadata(nsVideoInfo* aInfo)
{
  NS_ASSERTION(mDecoder->OnDecodeThread(),
               "Should be on decode thread.");

  nsMediaStream* stream = mDecoder->GetCurrentStream();
  NS_ASSERTION(stream, "Decoder has no media stream");

  if (!ReadFromStream(stream, reinterpret_cast<PRUint8*>(&mMetadata),
                      sizeof(mMetadata)))
    return NS_ERROR_FAILURE;

  // Validate the header
  if (!(mMetadata.headerPacketID == 0 /* Packet ID of 0 for the header*/ &&
        mMetadata.codecID == RAW_ID /* "YUV" */ &&
        mMetadata.majorVersion == 0 &&
        mMetadata.minorVersion == 1))
    return NS_ERROR_FAILURE;

  PRUint32 dummy;
  if (!MulOverflow32(mMetadata.frameWidth, mMetadata.frameHeight, dummy))
    return NS_ERROR_FAILURE;


  if (mMetadata.aspectDenominator == 0 ||
      mMetadata.framerateDenominator == 0)
    return NS_ERROR_FAILURE; // Invalid data

  // Determine and verify frame display size.
  float pixelAspectRatio = static_cast<float>(mMetadata.aspectNumerator) / 
                            mMetadata.aspectDenominator;
  nsIntSize display(mMetadata.frameWidth, mMetadata.frameHeight);
  ScaleDisplayByAspectRatio(display, pixelAspectRatio);
  mPicture = nsIntRect(0, 0, mMetadata.frameWidth, mMetadata.frameHeight);
  nsIntSize frameSize(mMetadata.frameWidth, mMetadata.frameHeight);
  if (!nsVideoInfo::ValidateVideoRegion(frameSize, mPicture, display)) {
    // Video track's frame sizes will overflow. Fail.
    return NS_ERROR_FAILURE;
  }

  mInfo.mHasVideo = true;
  mInfo.mHasAudio = false;
  mInfo.mDisplay = display;

  mFrameRate = static_cast<float>(mMetadata.framerateNumerator) /
               mMetadata.framerateDenominator;

  // Make some sanity checks
  if (mFrameRate > 45 ||
      mFrameRate == 0 ||
      pixelAspectRatio == 0 ||
      mMetadata.frameWidth > 2000 ||
      mMetadata.frameHeight > 2000 ||
      mMetadata.chromaChannelBpp != 4 ||
      mMetadata.lumaChannelBpp != 8 ||
      mMetadata.colorspace != 1 /* 4:2:0 */)
    return NS_ERROR_FAILURE;

  mFrameSize = mMetadata.frameWidth * mMetadata.frameHeight *
    (mMetadata.lumaChannelBpp + mMetadata.chromaChannelBpp) / 8.0 +
    sizeof(nsRawPacketHeader);

  PRInt64 length = stream->GetLength();
  if (length != -1) {
    mozilla::ReentrantMonitorAutoEnter autoMonitor(mDecoder->GetReentrantMonitor());
    mDecoder->GetStateMachine()->SetDuration(USECS_PER_S *
                                           (length - sizeof(nsRawVideoHeader)) /
                                           (mFrameSize * mFrameRate));
  }

  *aInfo = mInfo;

  return NS_OK;
}
예제 #12
0
bool nsRawReader::DecodeVideoFrame(bool &aKeyframeSkip,
                                     PRInt64 aTimeThreshold)
{
  NS_ASSERTION(mDecoder->OnDecodeThread(),
               "Should be on decode thread.");

  // Record number of frames decoded and parsed. Automatically update the
  // stats counters using the AutoNotifyDecoded stack-based class.
  PRUint32 parsed = 0, decoded = 0;
  nsMediaDecoder::AutoNotifyDecoded autoNotify(mDecoder, parsed, decoded);

  if (!mFrameSize)
    return false; // Metadata read failed.  We should refuse to play.

  PRInt64 currentFrameTime = USECS_PER_S * mCurrentFrame / mFrameRate;
  PRUint32 length = mFrameSize - sizeof(nsRawPacketHeader);

  nsAutoArrayPtr<PRUint8> buffer(new PRUint8[length]);
  nsMediaStream* stream = mDecoder->GetCurrentStream();
  NS_ASSERTION(stream, "Decoder has no media stream");

  // We're always decoding one frame when called
  while(true) {
    nsRawPacketHeader header;

    // Read in a packet header and validate
    if (!(ReadFromStream(stream, reinterpret_cast<PRUint8*>(&header),
                         sizeof(header))) ||
        !(header.packetID == 0xFF && header.codecID == RAW_ID /* "YUV" */)) {
      return false;
    }

    if (!ReadFromStream(stream, buffer, length)) {
      return false;
    }

    parsed++;

    if (currentFrameTime >= aTimeThreshold)
      break;

    mCurrentFrame++;
    currentFrameTime += static_cast<double>(USECS_PER_S) / mFrameRate;
  }

  VideoData::YCbCrBuffer b;
  b.mPlanes[0].mData = buffer;
  b.mPlanes[0].mStride = mMetadata.frameWidth * mMetadata.lumaChannelBpp / 8.0;
  b.mPlanes[0].mHeight = mMetadata.frameHeight;
  b.mPlanes[0].mWidth = mMetadata.frameWidth;

  PRUint32 cbcrStride = mMetadata.frameWidth * mMetadata.chromaChannelBpp / 8.0;

  b.mPlanes[1].mData = buffer + mMetadata.frameHeight * b.mPlanes[0].mStride;
  b.mPlanes[1].mStride = cbcrStride;
  b.mPlanes[1].mHeight = mMetadata.frameHeight / 2;
  b.mPlanes[1].mWidth = mMetadata.frameWidth / 2;

  b.mPlanes[2].mData = b.mPlanes[1].mData + mMetadata.frameHeight * cbcrStride / 2;
  b.mPlanes[2].mStride = cbcrStride;
  b.mPlanes[2].mHeight = mMetadata.frameHeight / 2;
  b.mPlanes[2].mWidth = mMetadata.frameWidth / 2;

  VideoData *v = VideoData::Create(mInfo,
                                   mDecoder->GetImageContainer(),
                                   -1,
                                   currentFrameTime,
                                   currentFrameTime + (USECS_PER_S / mFrameRate),
                                   b,
                                   1, // In raw video every frame is a keyframe
                                   -1,
                                   mPicture);
  if (!v)
    return false;

  mVideoQueue.Push(v);
  mCurrentFrame++;
  decoded++;
  currentFrameTime += USECS_PER_S / mFrameRate;

  return true;
}
예제 #13
0
nsresult nsRawReader::ReadMetadata(nsVideoInfo* aInfo)
{
  NS_ASSERTION(mDecoder->OnStateMachineThread(),
               "Should be on state machine thread.");
  mozilla::MonitorAutoEnter autoEnter(mMonitor);

  nsMediaStream* stream = mDecoder->GetCurrentStream();
  NS_ASSERTION(stream, "Decoder has no media stream");

  if (!ReadFromStream(stream, reinterpret_cast<PRUint8*>(&mMetadata),
                      sizeof(mMetadata)))
    return NS_ERROR_FAILURE;

  // Validate the header
  if (!(mMetadata.headerPacketID == 0 /* Packet ID of 0 for the header*/ &&
        mMetadata.codecID == RAW_ID /* "YUV" */ &&
        mMetadata.majorVersion == 0 &&
        mMetadata.minorVersion == 1))
    return NS_ERROR_FAILURE;

  PRUint32 dummy;
  if (!MulOverflow32(mMetadata.frameWidth, mMetadata.frameHeight, dummy))
    return NS_ERROR_FAILURE;

  mInfo.mHasVideo = PR_TRUE;
  mInfo.mPicture.x = 0;
  mInfo.mPicture.y = 0;
  mInfo.mPicture.width = mMetadata.frameWidth;
  mInfo.mPicture.height = mMetadata.frameHeight;
  mInfo.mFrame.width = mMetadata.frameWidth;
  mInfo.mFrame.height = mMetadata.frameHeight;
  if (mMetadata.aspectDenominator == 0 ||
      mMetadata.framerateDenominator == 0)
    return NS_ERROR_FAILURE; // Invalid data
  mInfo.mPixelAspectRatio = static_cast<float>(mMetadata.aspectNumerator) / 
                            mMetadata.aspectDenominator;
  mInfo.mHasAudio = PR_FALSE;

  mFrameRate = static_cast<float>(mMetadata.framerateNumerator) /
               mMetadata.framerateDenominator;

  // Make some sanity checks
  if (mFrameRate > 45 ||
      mFrameRate == 0 ||
      mInfo.mPixelAspectRatio == 0 ||
      mMetadata.frameWidth > 2000 ||
      mMetadata.frameHeight > 2000 ||
      mMetadata.chromaChannelBpp != 4 ||
      mMetadata.lumaChannelBpp != 8 ||
      mMetadata.colorspace != 1 /* 4:2:0 */)
    return NS_ERROR_FAILURE;

  mFrameSize = mMetadata.frameWidth * mMetadata.frameHeight *
    (mMetadata.lumaChannelBpp + mMetadata.chromaChannelBpp) / 8.0 +
    sizeof(nsRawPacketHeader);

  PRInt64 length = stream->GetLength();
  if (length != -1) {
    mozilla::MonitorAutoExit autoExitMonitor(mMonitor);
    mozilla::MonitorAutoEnter autoMonitor(mDecoder->GetMonitor());
    mDecoder->GetStateMachine()->SetDuration(USECS_PER_S *
                                           (length - sizeof(nsRawVideoHeader)) /
                                           (mFrameSize * mFrameRate));
  }

  *aInfo = mInfo;

  return NS_OK;
}
예제 #14
0
VError VValue::ReadRawFromStream( VStream* ioStream, sLONG inParam) //used by db4d server to read data without interpretation (for example : pictures)
{
	return ReadFromStream(ioStream, inParam);
}
예제 #15
0
파일: Midi.cpp 프로젝트: bjj/midiquiz
Midi Midi::ReadFromStream(istream &stream)
{
   Midi m;

   // header_id is always "MThd" by definition
   const static string MidiFileHeader = "MThd";
   const static string RiffFileHeader = "RIFF";

   // I could use (MidiFileHeader.length() + 1), but then this has to be
   // dynamically allocated.  More hassle than it's worth.  MIDI is well
   // defined and will always have a 4-byte header.  We use 5 so we get
   // free null termination.
   char           header_id[5] = { 0, 0, 0, 0, 0 };
   unsigned long  header_length;
   unsigned short format;
   unsigned short track_count;
   unsigned short time_division;

   stream.read(header_id, static_cast<streamsize>(MidiFileHeader.length()));
   string header(header_id);
   if (header != MidiFileHeader)
   {
      if (header != RiffFileHeader) throw MidiError(MidiError_UnknownHeaderType);
      else
      {
         // We know how to support RIFF files
         unsigned long throw_away;
         stream.read(reinterpret_cast<char*>(&throw_away), sizeof(unsigned long)); // RIFF length
         stream.read(reinterpret_cast<char*>(&throw_away), sizeof(unsigned long)); // "RMID"
         stream.read(reinterpret_cast<char*>(&throw_away), sizeof(unsigned long)); // "data"
         stream.read(reinterpret_cast<char*>(&throw_away), sizeof(unsigned long)); // data size

         // Call this recursively, without the RIFF header this time
         return ReadFromStream(stream);
      }
   }

   stream.read(reinterpret_cast<char*>(&header_length), sizeof(unsigned long));
   stream.read(reinterpret_cast<char*>(&format),        sizeof(unsigned short));
   stream.read(reinterpret_cast<char*>(&track_count),   sizeof(unsigned short));
   stream.read(reinterpret_cast<char*>(&time_division), sizeof(unsigned short));

   if (stream.fail()) throw MidiError(MidiError_NoHeader);

   // Chunk Size is always 6 by definition
   const static unsigned int MidiFileHeaderChunkLength = 6;

   header_length = BigToSystem32(header_length);
   if (header_length != MidiFileHeaderChunkLength)
   {
      throw MidiError(MidiError_BadHeaderSize);
   }

   enum MidiFormat { MidiFormat0 = 0, MidiFormat1, MidiFormat2 };

   format = BigToSystem16(format);
   if (format == MidiFormat2)
   {
      // MIDI 0: All information in 1 track
      // MIDI 1: Multiple tracks intended to be played simultaneously
      // MIDI 2: Multiple tracks intended to be played separately
      //
      // We do not support MIDI 2 at this time
      throw MidiError(MidiError_Type2MidiNotSupported);
   }

   track_count = BigToSystem16(track_count);
   if (format == 0 && track_count != 1)
   {
      // MIDI 0 has only 1 track by definition
      throw MidiError(MidiError_BadType0Midi);
   }

   // Time division can be encoded two ways based on a bit-flag:
   // - pulses per quarter note (15-bits)
   // - SMTPE frames per second (7-bits for SMPTE frame count and 8-bits for clock ticks per frame)
   time_division = BigToSystem16(time_division);
   bool in_smpte = ((time_division & 0x8000) != 0);

   if (in_smpte)
   {
      throw MidiError(MidiError_SMTPETimingNotImplemented);
   }

   // We ignore the possibility of SMPTE timing, so we can
   // use the time division value directly as PPQN.
   unsigned short pulses_per_quarter_note = time_division;

   // Read in our tracks
   for (int i = 0; i < track_count; ++i)
   {
      m.m_tracks.push_back(MidiTrack::ReadFromStream(stream));
   }

   m.BuildTempoTrack();

   // Tell our tracks their IDs
   for (int i = 0; i < track_count; ++i)
   {
      m.m_tracks[i].SetTrackId(i);
   }

   // Translate each track's list of notes and list
   // of events into microseconds.
   for (MidiTrackList::iterator i = m.m_tracks.begin(); i != m.m_tracks.end(); ++i)
   {
      i->Reset();
      m.TranslateNotes(i->Notes(), pulses_per_quarter_note);

      MidiEventMicrosecondList event_usecs;
      for (MidiEventPulsesList::const_iterator j = i->EventPulses().begin(); j != i->EventPulses().end(); ++j)
      {
         event_usecs.push_back(m.GetEventPulseInMicroseconds(*j, pulses_per_quarter_note));
      }
      i->SetEventUsecs(event_usecs);
   }

   m.m_initialized = true;

   // Just grab the end of the last note to find out how long the song is
   m.m_microsecond_base_song_length = m.m_translated_notes.rbegin()->end;

   // Eat everything up until *just* before the first note event
   m.m_microsecond_dead_start_air = m.GetEventPulseInMicroseconds(m.FindFirstNotePulse(), pulses_per_quarter_note) - 1;
   
   return m;
}