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_MovieFragmentAtom
     19 
     20 #include "moviefragmentatom.h"
     21 
     22 typedef Oscl_Vector < PVA_FF_TrackFragmentAtom*,
     23 OsclMemAllocator > PVA_FF_TrackFragmentAtomVecType;
     24 
     25 
     26 
     27 // constructor
     28 PVA_FF_MovieFragmentAtom::PVA_FF_MovieFragmentAtom(uint32 sequenceNumber,
     29         uint32 movieFragmentDuration,
     30         uint32 interleaveDuration)
     31         : PVA_FF_Atom(MOVIE_FRAGMENT_ATOM)
     32 {
     33     _movieFragmentDuration = movieFragmentDuration;
     34     _interleaveDuration = interleaveDuration;
     35 
     36     // Initialise movie fragment header atom
     37     PV_MP4_FF_NEW(fp->auditCB, PVA_FF_MovieFragmentHeaderAtom, ((sequenceNumber)), _pMfhdAtom);
     38     _pMfhdAtom->setParent(this);
     39 
     40     // initialise track fragment list
     41     PV_MP4_FF_NEW(fp->auditCB, PVA_FF_TrackFragmentAtomVecType, (), _pTrafList);
     42 
     43     recomputeSize();
     44 
     45 }
     46 
     47 
     48 //destructor
     49 PVA_FF_MovieFragmentAtom::~PVA_FF_MovieFragmentAtom()
     50 {
     51     // delete header atom
     52     PV_MP4_FF_DELETE(NULL, PVA_FF_MovieFragmentHeaderAtom, _pMfhdAtom);
     53 
     54     // Delete track fragment atom list
     55     for (uint32 ii = 0; ii < _pTrafList->size(); ii++)
     56     {
     57         PV_MP4_FF_DELETE(NULL, PVA_FF_TrackFragmentAtom, (*_pTrafList)[ii]);
     58     }
     59     PV_MP4_FF_TEMPLATED_DELETE(NULL, PVA_FF_TrackFragmentAtomVecType, Oscl_Vector, _pTrafList);
     60 
     61 }
     62 
     63 
     64 // adds new track fragment to the moof
     65 void
     66 PVA_FF_MovieFragmentAtom::addTrackFragment(uint32 mediaType, uint32 codecType,
     67         uint32 trackId, uint32 timescale)
     68 {
     69 
     70     PVA_FF_TrackFragmentAtom*   pTrafAtom;
     71     PV_MP4_FF_NEW(fp->auditCB, PVA_FF_TrackFragmentAtom, ((mediaType), (codecType),
     72                   (trackId), (_interleaveDuration), (timescale)), pTrafAtom);
     73 
     74     _pTrafList->push_back(pTrafAtom);
     75     pTrafAtom->setParent(this);
     76 
     77     // no need to recompute size. that is called by trun for parent
     78     recomputeSize();
     79 
     80     return;
     81 }
     82 
     83 
     84 // get track fragment atom with given trackId
     85 PVA_FF_TrackFragmentAtom*
     86 PVA_FF_MovieFragmentAtom::getTrackFragment(uint32 trackId)
     87 {
     88     if (_pTrafList->size() != 0)
     89     {
     90         for (uint32 ii = 0; ii < _pTrafList->size(); ii++)
     91         {
     92             if (trackId == ((*_pTrafList)[ii])->getTrackId())
     93                 return ((*_pTrafList)[ii]);
     94         }
     95     }
     96 
     97     return NULL;
     98 }
     99 
    100 
    101 // add new sample to this fragment ( in turn added to trun)
    102 void
    103 PVA_FF_MovieFragmentAtom::addSampleToFragment(uint32 trackId, uint32 size,
    104         uint32 ts, uint8 flags, uint32 baseOffset,
    105         bool otrunStart)
    106 {
    107 
    108     PVA_FF_TrackFragmentAtom*   pTrafAtom = getTrackFragment(trackId);
    109 
    110     pTrafAtom->addSampleToFragment(size, ts, flags, baseOffset, otrunStart);
    111 
    112 }
    113 
    114 
    115 // return the current duration of samples in given track
    116 int32
    117 PVA_FF_MovieFragmentAtom::getTrackFragmentDuration(uint32 trackId)
    118 {
    119     PVA_FF_TrackFragmentAtom*   pTrafAtom = getTrackFragment(trackId);
    120 
    121     return pTrafAtom->getFragmentDuration();
    122 }
    123 
    124 // return duration of longest track
    125 int32
    126 PVA_FF_MovieFragmentAtom::getMaxTrackDuration()
    127 {
    128     uint32 fragmentDuration = 0;
    129     if (_pTrafList->size() != 0)
    130     {
    131         for (uint32 ii = 0; ii < _pTrafList->size(); ii++)
    132         {
    133             if (fragmentDuration < ((*_pTrafList)[ii])->getFragmentDuration())
    134                 fragmentDuration = ((*_pTrafList)[ii])->getFragmentDuration();
    135         }
    136     }
    137 
    138 
    139     return fragmentDuration;
    140 }
    141 
    142 // return sequence number of this MOOF
    143 uint32
    144 PVA_FF_MovieFragmentAtom::getSequenceNumber()
    145 {
    146     return _pMfhdAtom->getSequenceNumber();
    147 }
    148 
    149 
    150 // return TRAF number for track id
    151 uint32
    152 PVA_FF_MovieFragmentAtom::getTrackFragmentNumber(uint32 trackId)
    153 {
    154     if (_pTrafList->size() != 0)
    155     {
    156         for (uint32 ii = 0; ii < _pTrafList->size(); ii++)
    157         {
    158             if (trackId == ((*_pTrafList)[ii])->getTrackId())
    159                 return ii + 1;
    160         }
    161     }
    162     return 0;
    163 
    164 }
    165 
    166 
    167 // set header data for current track ( not in use )
    168 void
    169 PVA_FF_MovieFragmentAtom::setTrackFragmentHeaderData(uint32 trackId, uint32 flags)
    170 {
    171     if (_pTrafList->size() != 0)
    172     {
    173         for (uint32 ii = 0; ii < _pTrafList->size(); ii++)
    174         {
    175             if (trackId == ((*_pTrafList)[ii])->getTrackId())
    176                 (*_pTrafList)[ii]->setTrackFragmentHeaderData(flags);
    177         }
    178     }
    179     recomputeSize();
    180 }
    181 
    182 
    183 // set base data offset for given track fragment
    184 void
    185 PVA_FF_MovieFragmentAtom::setTrackFragmentBaseDataOffset(uint32 trackId, uint32 offset)
    186 {
    187     if (_pTrafList->size() != 0)
    188     {
    189         for (uint32 ii = 0; ii < _pTrafList->size(); ii++)
    190         {
    191             if (trackId == ((*_pTrafList)[ii])->getTrackId())
    192                 (*_pTrafList)[ii]->setTrackFragmentBaseDataOffset(offset);
    193         }
    194     }
    195 }
    196 
    197 
    198 
    199 // recompute size of atom
    200 void
    201 PVA_FF_MovieFragmentAtom::recomputeSize()
    202 {
    203     int32 size = getDefaultSize();
    204 
    205     // add size of mfhd atom
    206     size += _pMfhdAtom->getSize();
    207 
    208     // add size of Track fragments
    209     if (_pTrafList->size() != 0)
    210     {
    211         for (uint32 ii = 0; ii < _pTrafList->size(); ii++)
    212         {
    213             if (((*_pTrafList)[ii])->getSampleCount() > 0)
    214             {
    215                 size += ((*_pTrafList)[ii])->getSize();
    216             }
    217         }
    218     }
    219 
    220     _size = size;
    221 
    222     // Update the parent atom size
    223     if (_pparent != NULL)
    224     {
    225         _pparent->recomputeSize();
    226     }
    227 }
    228 
    229 
    230 // write atom in target file
    231 bool
    232 PVA_FF_MovieFragmentAtom::renderToFileStream(MP4_AUTHOR_FF_FILE_IO_WRAP* fp)
    233 {
    234 
    235     uint32 rendered = 0;
    236 
    237     if (!renderAtomBaseMembers(fp))
    238     {
    239         return false;
    240     }
    241     rendered += getDefaultSize();
    242 
    243     // Render the movie fragment header atom
    244     if (!_pMfhdAtom->renderToFileStream(fp))
    245     {
    246         return false;
    247     }
    248     rendered += _pMfhdAtom->getSize();
    249 
    250 
    251     // render track extend atoms
    252     if (_pTrafList->size() != 0)
    253     {
    254         for (uint32 ii = 0; ii < _pTrafList->size(); ii++)
    255         {
    256             if (((*_pTrafList)[ii])->getSampleCount() > 0)
    257             {
    258                 if (!((*_pTrafList)[ii])->renderToFileStream(fp))
    259                 {
    260                     return false;
    261                 }
    262                 rendered += ((*_pTrafList)[ii])->getSize();
    263             }
    264         }
    265     }
    266 
    267     return true;
    268 }
    269