/* ------------------------------------------------------------------
 * Copyright (C) 1998-2010 PacketVideo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 * -------------------------------------------------------------------
 */
/*********************************************************************************/
/*     -------------------------------------------------------------------       */
/*                         MPEG-4 SampleToChunkAtom Class                        */
/*     -------------------------------------------------------------------       */
/*********************************************************************************/
/*
    This SampleSizeAtom Class contains the sample count and a table giving the
    size of each sample.
*/


#ifndef SAMPLETOCHUNKATOM_H_INCLUDED
#define SAMPLETOCHUNKATOM_H_INCLUDED


#ifndef OSCL_FILE_IO_H_INCLUDED
#include "oscl_file_io.h"
#endif

#ifndef FULLATOM_H_INCLUDED
#include "fullatom.h"
#endif

class SampleToChunkAtom : public FullAtom
{

    public:
        SampleToChunkAtom(MP4_FF_FILE *fp, uint32 size, uint32 type, OSCL_wString& filename, uint32 parsingMode);
        virtual ~SampleToChunkAtom();

        // Member gets and sets
        uint32 getEntryCount() const
        {
            return _entryCount;
        }

        uint32 getNextChunkNumber()
        {
            return _currentChunkNumber++;
        }

        MP4_ERROR_CODE getFirstChunkAt(uint32 index, int32& pos);
        MP4_ERROR_CODE getSamplesPerChunkAt(uint32 index, int32& SamplesPerChunk);

        MP4_ERROR_CODE getSDIndex(uint32& SDIndex) const;
        // Returns the chunk number for the given sample number
        MP4_ERROR_CODE getChunkNumberForSample(uint32 sampleNum, uint32& ChunkNumber);
        MP4_ERROR_CODE getChunkNumberForSampleGet(uint32 sampleNum, uint32& ChunkNumber);

        // Returns the number of the first sample in chunk number 'chunk'
        MP4_ERROR_CODE getFirstSampleNumInChunk(uint32 chunkNum, uint32& SampleNum) ;
        MP4_ERROR_CODE getFirstSampleNumInChunkPeek(uint32& SampleNum) const;

        int32 getMaxNumSamlplesPerChunk()
        {
            return _maxNumSamplesPerChunk;
        }
        void setMaxNumSamlplesPerChunk(int32 max)
        {
            _maxNumSamplesPerChunk = max;
        }
        int32 getMaxChunkDataSize()
        {
            return _maxChunkDataSize;
        }
        void setMaxChunkDataSize(int32 max)
        {
            _maxChunkDataSize = max;
        }

        MP4_ERROR_CODE getNumChunksInRunofChunks(uint32 chunk, uint32& NumChunks);
        MP4_ERROR_CODE getSamplesPerChunkCorrespondingToSample(uint32 sampleNum, uint32& SamplesPerChunk);
        MP4_ERROR_CODE getSamplesPerChunkCorrespondingToSampleGet(uint32& SamplesPerChunk) const;
        int32 resetStateVariables();
        int32 resetStateVariables(uint32 sampleNum);
        MP4_ERROR_CODE getFirstSampleNumInChunkGet(uint32& SampleNum) const;
        MP4_ERROR_CODE getChunkNumberForSamplePeek(uint32 sampleNum, uint32& ChunkNumber);
        MP4_ERROR_CODE getNumChunksInRunofChunksGet(uint32& numChunksInRun) const;
        MP4_ERROR_CODE getSDIndexPeek(uint32& SDIndex) const;
        MP4_ERROR_CODE getSDIndexGet(uint32& SDIndex) const;
        int32 resetPeekwithGet();
        uint32 getCurrPeekSampleCount()
        {
            return _currPeekSampleCount;
        }

    private:

        bool ParseEntryUnit(uint32 sample_cnt);
        void CheckAndParseEntry(uint32 i);
        uint32 _entryCount;
        uint32 *_pfirstChunkVec;
        uint32 *_psamplesPerChunkVec;
        uint32 *_psampleDescriptionIndexVec;

        int32 _mediaType;

        uint32 _currentChunkNumSamples;
        uint32 _currentChunkDataSize; // Ad up all sample sizes for the current chunk
        uint32 _currentChunkNumber;

        int32 _maxNumSamplesPerChunk;
        int32 _maxChunkDataSize;

        uint32 _Index;

        int32 _numChunksInRun;
        int32 _majorGetIndex;
        int32 _currGetChunk;
        int32 _numGetChunksInRun;
        uint32 _currGetSampleCount;
        int32 _firstGetSampleInCurrChunk;
        int32 _numGetSamplesPerChunk;
        int32 _currGetSDI;

        int32 _majorPeekIndex;
        int32 _currPeekChunk;
        int32 _numPeekChunksInRun;
        uint32 _currPeekSampleCount;
        int32 _firstPeekSampleInCurrChunk;
        int32 _numPeekSamplesPerChunk;
        int32 _currPeekSDI;
        PVLogger *iLogger, *iStateVarLogger, *iParsedDataLogger;
        uint32 _parsing_mode;

        MP4_FF_FILE *_fileptr;
        uint32  _parsed_entry_cnt;
        MP4_FF_FILE *_curr_fptr;
        TOsclFileOffset *_stbl_fptr_vec;
        uint32 _stbl_buff_size;
        uint32 _curr_entry_point;
        uint32 _curr_buff_number;
        uint32 _next_buff_number;



};


#endif // SAMPLETOCHUNKATOM_H_INCLUDED

