Home | History | Annotate | Download | only in src
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 1998-2009 PacketVideo
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
     13  * express or implied.
     14  * See the License for the specific language governing permissions
     15  * and limitations under the License.
     16  * -------------------------------------------------------------------
     17  */
     18 #define IMPLEMENT_TrackFragmentRandomAccessAtom
     19 
     20 #include "trackfragmentrandomaccessatom.h"
     21 typedef Oscl_Vector<PVA_FF_RandomAccessEntry, OsclMemAllocator> PVA_FF_RandomAccessEntryVecType;
     22 
     23 // constructor
     24 PVA_FF_TfraAtom::PVA_FF_TfraAtom(uint32 trackId)
     25         : PVA_FF_FullAtom(TRACK_FRAGMENT_RANDOM_ACCESS_ATOM, (uint8)0, (uint32)0)
     26 {
     27     _trackId = trackId;
     28     _reserved = TFRA_LENGTH_SIZE;
     29     _entryCount = 0;
     30 
     31     _moofUpdateSample = 1;
     32 
     33     // initialise vectors
     34     PV_MP4_FF_NEW(fp->auditCB, PVA_FF_RandomAccessEntryVecType, (), _pSampleEntries);
     35 
     36     recomputeSize();
     37 }
     38 
     39 
     40 // destructor
     41 PVA_FF_TfraAtom::~PVA_FF_TfraAtom()
     42 {
     43 
     44     PV_MP4_FF_TEMPLATED_DELETE(NULL, PVA_FF_RandomAccessEntryVecType, Oscl_Vector, _pSampleEntries);
     45 }
     46 
     47 
     48 
     49 // add new entry to TFRA
     50 void
     51 PVA_FF_TfraAtom::addSampleEntry(uint32 ts, uint32 moofOffset, uint32 trafNumber,
     52                                 uint32 trunNumber, uint32 sampleNumber)
     53 {
     54     PVA_FF_RandomAccessEntry sampleEntry;
     55     sampleEntry.time            = ts;
     56     sampleEntry.moofOffset      = moofOffset;
     57     sampleEntry.trafNumber      = trafNumber;
     58     sampleEntry.trunNumber      = trunNumber;
     59     sampleEntry.sampleNumber    = sampleNumber;
     60 
     61     _pSampleEntries->push_back(sampleEntry) ;
     62 
     63     _entryCount++;
     64 
     65     recomputeSize();
     66 }
     67 
     68 
     69 uint32
     70 PVA_FF_TfraAtom::getTrackId()
     71 {
     72     return  _trackId;
     73 }
     74 
     75 
     76 // for last moof all random entries are updated
     77 // by given offset
     78 void
     79 PVA_FF_TfraAtom::updateMoofOffset(uint32 offset)
     80 {
     81     if (_pSampleEntries->size() >= _entryCount)
     82     {
     83         if (_moofUpdateSample <= _entryCount)
     84         {
     85             for (uint32 ii = _moofUpdateSample; ii <= _entryCount; ii++)
     86             {
     87                 ((*_pSampleEntries)[ii-1].moofOffset) += offset;
     88             }
     89             _moofUpdateSample = _entryCount + 1;
     90         }
     91     }
     92 }
     93 
     94 
     95 // recompute size of atom
     96 void
     97 PVA_FF_TfraAtom::recomputeSize()
     98 {
     99     int32 size = getDefaultSize();
    100 
    101     size += 4;  // track id
    102     size += 4;  // reserved
    103     size += 4;  // entry count
    104 
    105     if (_entryCount != 0)
    106     {
    107         for (uint32 ii = 0; ii < _entryCount; ii++)
    108         {
    109             size += 4;  // time
    110             size += 4;  // MoofOffset
    111             size += 2;  // traf number
    112             size += 2;  // trun number
    113             size += 2;  // sample number
    114         }
    115     }
    116 
    117     _size = size;
    118 
    119     // Update the parent atom size
    120     if (_pparent != NULL)
    121     {
    122         _pparent->recomputeSize();
    123     }
    124 }
    125 
    126 
    127 // write atom to target file
    128 bool
    129 PVA_FF_TfraAtom::renderToFileStream(MP4_AUTHOR_FF_FILE_IO_WRAP* fp)
    130 {
    131     uint32 rendered = 0;
    132 
    133     if (!renderAtomBaseMembers(fp))
    134     {
    135         return false;
    136     }
    137     rendered += getDefaultSize();
    138 
    139     if (!PVA_FF_AtomUtils::render32(fp, _trackId))
    140     {
    141         return false;
    142     }
    143     rendered += 4;
    144 
    145     if (!PVA_FF_AtomUtils::render32(fp, _reserved))
    146     {
    147         return false;
    148     }
    149     rendered += 4;
    150 
    151     if (!PVA_FF_AtomUtils::render32(fp, _entryCount))
    152     {
    153         return false;
    154     }
    155     rendered += 4;
    156 
    157     for (uint32 ii = 0; ii < _entryCount; ii++)
    158     {
    159 
    160         if (!PVA_FF_AtomUtils::render32(fp, (*_pSampleEntries)[ii].time))
    161         {
    162             return false;
    163         }
    164         rendered += 4;
    165 
    166         if (!PVA_FF_AtomUtils::render32(fp, (*_pSampleEntries)[ii].moofOffset))
    167         {
    168             return false;
    169         }
    170         rendered += 4;
    171 
    172         if (!PVA_FF_AtomUtils::render16(fp, (*_pSampleEntries)[ii].trafNumber))
    173         {
    174             return false;
    175         }
    176         rendered += 2;
    177 
    178         if (!PVA_FF_AtomUtils::render16(fp, (*_pSampleEntries)[ii].trunNumber))
    179         {
    180             return false;
    181         }
    182         rendered += 2;
    183 
    184         if (!PVA_FF_AtomUtils::render16(fp, (*_pSampleEntries)[ii].sampleNumber))
    185         {
    186             return false;
    187         }
    188         rendered += 2;
    189     }
    190     return true;
    191 }
    192