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_TrackFragmentRunAtom
     19 
     20 #include "trackfragmentrunatom.h"
     21 typedef Oscl_Vector<PVA_FF_TrackRunSample, OsclMemAllocator> PVA_FF_TrackRunSampleVecType;
     22 
     23 
     24 // constructor
     25 PVA_FF_TrackFragmentRunAtom::PVA_FF_TrackFragmentRunAtom()
     26         : PVA_FF_FullAtom(TRACK_RUN_ATOM, (uint8)0, (uint32)TRACK_RUN_ATOM_FLAGS)
     27 {
     28 
     29     _currentTimestamp = 0;
     30     _firstEntry = true;
     31 
     32     _sampleCount = 0;
     33     _dataOffset = 0;
     34     _firstSampleFlags = 0;
     35     _lastTSupdated = false;
     36 
     37     // initialise vectors to store sample parameters
     38     PV_MP4_FF_NEW(fp->auditCB, PVA_FF_TrackRunSampleVecType, (), _psampleEntriesVec);
     39 
     40     recomputeSize();
     41 }
     42 
     43 
     44 // destructor
     45 PVA_FF_TrackFragmentRunAtom::~PVA_FF_TrackFragmentRunAtom()
     46 {
     47     // Delete sample parameter vectors
     48     PV_MP4_FF_TEMPLATED_DELETE(NULL, PVA_FF_TrackRunSampleVecType, Oscl_Vector, _psampleEntriesVec);
     49 
     50 }
     51 
     52 // will be used to set tf_flags and denotes sample parameter present
     53 void
     54 PVA_FF_TrackFragmentRunAtom::setFlags(uint32 flags)
     55 {
     56 
     57     // currently not in use. only flags specified by TRACK_RUN_ATOM_FLAGS macro are present
     58     OSCL_UNUSED_ARG(flags);
     59 }
     60 
     61 
     62 void
     63 PVA_FF_TrackFragmentRunAtom::setDataOffset(uint32 offset)
     64 {
     65     _dataOffset = offset;
     66 }
     67 
     68 
     69 // add new sample entry
     70 void
     71 PVA_FF_TrackFragmentRunAtom::addSample(uint32 size, uint32 ts, uint8 flags)
     72 {
     73     _sampleCount++;
     74 
     75     PVA_FF_TrackRunSample sampleEntry;
     76     sampleEntry.sampleDuration = 0;
     77 
     78     if (_firstEntry)
     79     {
     80         _currentTimestamp = ts;
     81         _firstEntry = false;
     82     }
     83     else
     84     {
     85         // Calculate delta
     86         int32 delta = ts - _currentTimestamp;
     87         _currentTimestamp = ts;
     88 
     89         uint32 size = _psampleEntriesVec->size();
     90 
     91         (*_psampleEntriesVec)[size-1].sampleDuration = delta;   // add sample duration for last sample.. same as second last sample
     92         // last sample duration added while rendering
     93     }
     94     //Store meta data params
     95     sampleEntry.sampleSize = size;
     96 
     97     uint8 codingType = (uint8)((flags >> 2) & 0x03);
     98     if (codingType == CODING_TYPE_I)
     99     {
    100         sampleEntry.sampleFlags = KEY_FRAME_ENTRY;
    101     }
    102     else
    103     {
    104         sampleEntry.sampleFlags = 0;
    105     }
    106 
    107     _psampleEntriesVec->push_back(sampleEntry);
    108 
    109     recomputeSize();
    110 }
    111 
    112 
    113 // recompute size of atom
    114 void
    115 PVA_FF_TrackFragmentRunAtom::recomputeSize()
    116 {
    117     int32 size = getDefaultSize();
    118 
    119     size += 4;  // sample count;
    120     size += 4;  // data offset
    121 
    122     if (_psampleEntriesVec->size() != 0)
    123     {
    124         for (uint32 ii = 0; ii < _psampleEntriesVec->size(); ii++)
    125         {
    126             size += 4; // sample duration
    127             size += 4; // sample vector
    128             size += 4; // sample flags
    129         }
    130     }
    131     _size = size;
    132 
    133     // Update the parent atom size
    134     if (_pparent != NULL)
    135     {
    136         _pparent->recomputeSize();
    137     }
    138 }
    139 
    140 
    141 
    142 // update duration for last entry
    143 void
    144 PVA_FF_TrackFragmentRunAtom::updateLastTSEntry(uint32 ts)
    145 {
    146     uint32 delta = ts - _currentTimestamp;
    147     uint32 size = _psampleEntriesVec->size();
    148     (*_psampleEntriesVec)[size -1].sampleDuration = delta;
    149 
    150     _lastTSupdated = true;
    151 }
    152 
    153 
    154 
    155 // write atom to target file
    156 bool
    157 PVA_FF_TrackFragmentRunAtom::renderToFileStream(MP4_AUTHOR_FF_FILE_IO_WRAP* fp)
    158 {
    159     uint32 rendered = 0;
    160     uint32 temp = 0;
    161 
    162     if (!renderAtomBaseMembers(fp))
    163     {
    164         return false;
    165     }
    166     rendered += getDefaultSize();
    167 
    168     if (!PVA_FF_AtomUtils::render32(fp, _sampleCount))
    169     {
    170         return false;
    171     }
    172     rendered += 4;
    173 
    174     if (!PVA_FF_AtomUtils::render32(fp, _dataOffset))
    175     {
    176         return false;
    177     }
    178     rendered += 4;
    179 
    180     // update last sample duration (for last MOOF only)
    181     if (!_lastTSupdated)
    182     {
    183         temp = (*_psampleEntriesVec)[ _sampleCount - 2].sampleDuration;
    184         (*_psampleEntriesVec)[ _sampleCount - 1].sampleDuration = temp;
    185     }
    186 
    187     if (_psampleEntriesVec->size() < _sampleCount)
    188     {
    189         return false;
    190     }
    191     for (uint32 ii = 0; ii < _sampleCount; ii++)
    192     {
    193 
    194         temp = (*_psampleEntriesVec)[ii].sampleDuration;
    195         if (!PVA_FF_AtomUtils::render32(fp, temp))
    196         {
    197             return false;
    198         }
    199         rendered += 4;
    200 
    201         temp = (*_psampleEntriesVec)[ii].sampleSize;
    202         if (!PVA_FF_AtomUtils::render32(fp, temp))
    203         {
    204             return false;
    205         }
    206         rendered += 4;
    207 
    208         temp = (*_psampleEntriesVec)[ii].sampleFlags;
    209         if (!PVA_FF_AtomUtils::render32(fp, temp))
    210         {
    211             return false;
    212         }
    213         rendered += 4;
    214 
    215     }
    216 
    217     return true;
    218 }
    219