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 /*
     19     This PVA_FF_MediaHeaderAtom Class contains all the objects that declare information
     20     about the media data within the stream.
     21 */
     22 
     23 
     24 #define IMPLEMENT_MediaHeaderAtom
     25 
     26 #include "mediaheaderatom.h"
     27 #include "atomutils.h"
     28 #include "a_atomdefs.h"
     29 
     30 // Constructor
     31 PVA_FF_MediaHeaderAtom::PVA_FF_MediaHeaderAtom()
     32         : PVA_FF_FullAtom(MEDIA_HEADER_ATOM, 0, 0)
     33 {
     34     init(); // Initialize all member variables
     35     recomputeSize();
     36 }
     37 
     38 // Destructor
     39 PVA_FF_MediaHeaderAtom::~PVA_FF_MediaHeaderAtom()
     40 {
     41     // Empty
     42 }
     43 
     44 
     45 // Initialize all members
     46 void
     47 PVA_FF_MediaHeaderAtom::init()
     48 {
     49     PVA_FF_AtomUtils::setTime(_creationTime); // Setting creating time (since 1/1/1970) - NEED FIX to 1/1/1904
     50     PVA_FF_AtomUtils::setTime(_modificationTime); // Setting modification time
     51     _timeScale = DEFAULT_PRESENTATION_TIMESCALE;
     52     _duration = 0;
     53     _language = 0; // Until find better default value
     54     _reserved = 0;
     55 
     56     /*
     57      * Variables added to ensure that the total track duration includes the duration of the
     58      * last sample as well, which in our case fp same as the last but one.
     59      */
     60     _prevTS = 0;
     61     _deltaTS = 0;
     62 }
     63 
     64 // Inform header that a new sample fp added to allow it to maintain its
     65 // duration parameter - in terms of the media timescale - i.e. a sample count
     66 void
     67 PVA_FF_MediaHeaderAtom::addSample(uint32 ts)
     68 {
     69     _deltaTS = ts - _prevTS;
     70 
     71     setDuration(ts);
     72 
     73     _prevTS = ts;
     74 }
     75 
     76 
     77 // in movie fragment mode set the actual duration of
     78 // last sample
     79 void
     80 PVA_FF_MediaHeaderAtom::updateLastTSEntry(uint32 ts)
     81 {
     82 
     83     setDuration(ts);
     84     _deltaTS = 0;   // made 0 so that duration can not be updated before rendoring
     85     // in movie fragment mode
     86 }
     87 
     88 
     89 
     90 // Recompute size of atom
     91 void
     92 PVA_FF_MediaHeaderAtom::recomputeSize()
     93 {
     94     int32 size = getDefaultSize(); // Get size of base class members
     95     size += 4; // creationTime
     96     size += 4; // modificationTime
     97     size += 4; // timeScale
     98     size += 4; // duration
     99 
    100     size += 2; // language
    101     size += 2; // reserved
    102 
    103     _size = size;
    104 
    105     // Update the size of the parent atom
    106     if (_pparent != NULL)
    107     {
    108         _pparent->recomputeSize();
    109     }
    110 }
    111 
    112 // Rendering the PVA_FF_Atom in proper format (bitlengths, etc.) to an ostream
    113 bool
    114 PVA_FF_MediaHeaderAtom::renderToFileStream(MP4_AUTHOR_FF_FILE_IO_WRAP *fp)
    115 {
    116     int32 rendered = 0; // Keep track of number of bytes rendered
    117 
    118     // Render PVA_FF_Atom type and size
    119     if (!renderAtomBaseMembers(fp))
    120     {
    121         return false;
    122     }
    123     rendered += getDefaultSize();
    124 
    125     if (!PVA_FF_AtomUtils::render32(fp, getCreationTime()))
    126     {
    127         return false;
    128     }
    129     if (!PVA_FF_AtomUtils::render32(fp, getModificationTime()))
    130     {
    131         return false;
    132     }
    133     if (!PVA_FF_AtomUtils::render32(fp, getTimeScale()))
    134     {
    135         return false;
    136     }
    137 
    138     /*
    139      * To ensure that the total track duration includes the duration of the
    140      * last sample as well, which in our case fp same as the last but one.
    141      */
    142     uint32 totalDuration = getDuration() + _deltaTS;
    143 
    144     if (!PVA_FF_AtomUtils::render32(fp, totalDuration))
    145     {
    146         return false;
    147     }
    148     rendered += 16;
    149 
    150     if (!PVA_FF_AtomUtils::render16(fp, getLanguage()))
    151     {
    152         return false;
    153     }
    154     rendered += 2;
    155 
    156     if (!PVA_FF_AtomUtils::render16(fp, _reserved))
    157     {
    158         return false;
    159     }
    160     rendered += 2;
    161 
    162     return true;
    163 }
    164 
    165 
    166