VideoCodec/Modules/StreamFormat/StreamParser.cpp

#include "StreamParser.h" namespace VideoCodec {    StreamParser::StreamParser() : Module(STREAM_PARSER_MODULE_ID)    {       this->input = NULL;    }        StreamParser::~StreamParser()    {    }        void StreamParser::SetInputModule(StreamInputModule* inputModule)    {       this->input = inputModule;    }        ConfigurationStatus StreamParser::Configure(ConfigurationElement* configuration)    {       return MODULE_CONFIGURATION_OK;    }    void StreamParser::InputFormatHeader(StreamStatus* streamStatus)    {       if (this->input == NULL)       {          Logger::LogMessage("Warning: An input module must be set for the stream parser.");          return;       }              // Assume everything's okay unless something goes wrong.       *streamStatus = STREAM_STATUS_OK;              CodecIdentifier codecIdentifier;       StreamFormatIdentifier streamFormatIdentifier;              if (this->input->Read(&codecIdentifier, sizeof(CodecIdentifier), 1) != 1          || codecIdentifier != STREAM_FORMAT_CODEC_IDENTIFIER)          *streamStatus = STREAM_STATUS_ERROR;              if (this->input->Read(&streamFormatIdentifier, sizeof(StreamFormatIdentifier), 1) != 1          || streamFormatIdentifier != STREAM_FORMAT_VERSION_IDENTIFIER)          *streamStatus = STREAM_STATUS_ERROR;    }        void StreamParser::InputStreamHeader(StreamStatus* streamStatus, StreamHeader* streamHeader)    {       if (this->input == NULL)       {          Logger::LogMessage("Warning: An input module must be set for the stream parser.");          return;       }              // Assume everything's okay unless something goes wrong.       *streamStatus = STREAM_STATUS_OK;              // Read in the frame count and the number of macroblocks in columns and rows.       if (this->input->Read(&streamHeader->frameCount, sizeof(FrameCount), 1) != 1)          *streamStatus = STREAM_STATUS_ERROR;       if (this->input->Read(&streamHeader->blockColumns, sizeof(BlockCount), 1) != 1)          *streamStatus = STREAM_STATUS_ERROR;       if (this->input->Read(&streamHeader->blockRows, sizeof(BlockCount), 1) != 1)          *streamStatus = STREAM_STATUS_ERROR;       streamHeader->mv4 = false;       unsigned char mv4Byte;       if (this->input->Read(&mv4Byte, sizeof(unsigned char), 1) == 1)          streamHeader->mv4 = (mv4Byte == 1);       else          *streamStatus = STREAM_STATUS_ERROR;       streamHeader->halfPixel = false;       unsigned char hpByte;       if (this->input->Read(&hpByte, sizeof(unsigned char), 1) == 1)          streamHeader->halfPixel = (hpByte == 1);       else          *streamStatus = STREAM_STATUS_ERROR;    }        void StreamParser::InputFrameHeader(StreamStatus* streamStatus, FrameHeader* frameHeader, BinaryStreamReader* binaryReader)    {       // We use the binary reader rather than getting bytes from the file, because it may already have       // buffered data.       unsigned char pFrame = (unsigned char)binaryReader->GetBits(8);       binaryReader->AdvanceReader(8);       frameHeader->pFrame = (pFrame == 1);       frameHeader->quantizationMethod = (DCTCoefficientQuantizationMethod)binaryReader->GetBits(8);       binaryReader->AdvanceReader(8);       frameHeader->quantizationScale = (UnsignedByte)binaryReader->GetBits(8);       binaryReader->AdvanceReader(8);    } }