Ap4TrakAtom.cpp

00001 /*****************************************************************
00002 |
00003 |    AP4 - trak Atoms 
00004 |
00005 |    Copyright 2002 Gilles Boccon-Gibod
00006 |
00007 |
00008 |    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
00009 |
00010 |    Unless you have obtained Bento4 under a difference license,
00011 |    this version of Bento4 is Bento4|GPL.
00012 |    Bento4|GPL is free software; you can redistribute it and/or modify
00013 |    it under the terms of the GNU General Public License as published by
00014 |    the Free Software Foundation; either version 2, or (at your option)
00015 |    any later version.
00016 |
00017 |    Bento4|GPL is distributed in the hope that it will be useful,
00018 |    but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 |    GNU General Public License for more details.
00021 |
00022 |    You should have received a copy of the GNU General Public License
00023 |    along with Bento4|GPL; see the file COPYING.  If not, write to the
00024 |    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
00025 |    02111-1307, USA.
00026 |
00027  ****************************************************************/
00028 
00029 /*----------------------------------------------------------------------
00030 |       includes
00031 +---------------------------------------------------------------------*/
00032 #include "Ap4.h"
00033 #include "Ap4TrakAtom.h"
00034 #include "Ap4MdhdAtom.h"
00035 #include "Ap4VmhdAtom.h"
00036 #include "Ap4SmhdAtom.h"
00037 #include "Ap4HmhdAtom.h"
00038 #include "Ap4NmhdAtom.h"
00039 #include "Ap4DrefAtom.h"
00040 #include "Ap4UrlAtom.h"
00041 #include "Ap4StcoAtom.h"
00042 #include "Ap4Co64Atom.h"
00043 #include "Ap4AtomFactory.h"
00044 #include "Ap4Utils.h"
00045 
00046 /*----------------------------------------------------------------------
00047 |       AP4_TrakAtom::AP4_TrakAtom
00048 +---------------------------------------------------------------------*/
00049 AP4_TrakAtom::AP4_TrakAtom(AP4_SampleTable* sample_table,
00050                            AP4_Atom::Type   hdlr_type,
00051                            const char*      hdlr_name, 
00052                            AP4_UI32         track_id,
00053                            AP4_UI32         creation_time,
00054                            AP4_UI32         modification_time,
00055                            AP4_UI32         track_duration,
00056                            AP4_UI32         media_time_scale,
00057                            AP4_UI32         media_duration,
00058                            AP4_UI16         volume,
00059                            const char*      language,
00060                            AP4_UI32         width,
00061                            AP4_UI32         height) :
00062     AP4_ContainerAtom(AP4_ATOM_TYPE_TRAK)
00063 {
00064     AP4_Result result;
00065 
00066     // create a tkhd atom
00067     m_TkhdAtom = new AP4_TkhdAtom(creation_time, 
00068                                   modification_time, 
00069                                   track_id,
00070                                   track_duration, 
00071                                   volume, 
00072                                   width, 
00073                                   height);
00074 
00075     // create an edts
00076 
00077     // create a mdia atom
00078     AP4_ContainerAtom* mdia = new AP4_ContainerAtom(AP4_ATOM_TYPE_MDIA);
00079 
00080     // create a hdlr atom for the mdia atom
00081     m_HdlrAtom = new AP4_HdlrAtom(hdlr_type, hdlr_name);
00082 
00083     // create a minf atom 
00084     AP4_ContainerAtom* minf = new AP4_ContainerAtom(AP4_ATOM_TYPE_MINF);
00085 
00086     // create a media header atom for minf (vmhd, smhd, hmhd or nmhd)
00087     AP4_Atom* minf_header;
00088     switch (hdlr_type) {
00089         case AP4_HANDLER_TYPE_VIDE:
00090             minf_header = new AP4_VmhdAtom(0, 0, 0, 0);
00091             break;
00092 
00093         case AP4_HANDLER_TYPE_SOUN:
00094             minf_header = new AP4_SmhdAtom(0);
00095             break;
00096 
00097         default:
00098             minf_header = new AP4_NmhdAtom();
00099             break;
00100     }
00101 
00102     // create a dinf atom for minf
00103     AP4_ContainerAtom* dinf = new AP4_ContainerAtom(AP4_ATOM_TYPE_DINF);
00104 
00105     // create a url atom as a ref for dref
00106     AP4_Atom* url = new AP4_UrlAtom(); // local ref
00107 
00108     // create a dref atom for dinf
00109     AP4_DrefAtom* dref = new AP4_DrefAtom(&url, 1);
00110 
00111     // create a stbl atom for minf
00112     AP4_ContainerAtom* stbl;
00113     result = sample_table->GenerateStblAtom(stbl);
00114     if (AP4_FAILED(result)) stbl = NULL;
00115     
00116     // populate the dinf atom
00117     dinf->AddChild(dref);
00118 
00119     // populate the minf atom
00120     minf->AddChild(minf_header);
00121     minf->AddChild(dinf);
00122     if (stbl) minf->AddChild(stbl);
00123 
00124     // create a mdhd atom for the mdia atom
00125     AP4_MdhdAtom* mdhd = new AP4_MdhdAtom(creation_time,
00126                                           modification_time,
00127                                           media_time_scale,
00128                                           media_duration,
00129                                           language);
00130 
00131     // populate the mdia atom
00132     mdia->AddChild(mdhd);
00133     mdia->AddChild(m_HdlrAtom);
00134     mdia->AddChild(minf);
00135 
00136     // attach the children
00137     AddChild(m_TkhdAtom);
00138     AddChild(mdia);
00139 }
00140 
00141 /*----------------------------------------------------------------------
00142 |       AP4_TrakAtom::AP4_TrakAtom
00143 +---------------------------------------------------------------------*/
00144 AP4_TrakAtom::AP4_TrakAtom(AP4_Size         size,
00145                            AP4_ByteStream&  stream,
00146                            AP4_AtomFactory& atom_factory) :
00147     AP4_ContainerAtom(AP4_ATOM_TYPE_TRAK, size, false, stream, atom_factory),
00148     m_HdlrAtom(NULL),
00149     m_TkhdAtom(NULL)
00150 {
00151     AP4_Atom* tkhd = FindChild("tkhd");
00152     if (tkhd != NULL) {
00153         m_TkhdAtom = dynamic_cast<AP4_TkhdAtom*>(tkhd);
00154     } else {
00155         m_TkhdAtom  = NULL;
00156     }
00157 }
00158 
00159 /*----------------------------------------------------------------------
00160 |       AP4_TrakAtom::GetDuration
00161 +---------------------------------------------------------------------*/
00162 AP4_UI32
00163 AP4_TrakAtom::GetDuration()
00164 {
00165     if (m_TkhdAtom) {
00166         return m_TkhdAtom->GetDuration();
00167     } else {
00168         return 0;
00169     }
00170 }
00171 
00172 /*----------------------------------------------------------------------
00173 |       AP4_TrakAtom::SetDuration
00174 +---------------------------------------------------------------------*/
00175 AP4_Result
00176 AP4_TrakAtom::SetDuration(AP4_UI32 duration)
00177 {
00178     if (m_TkhdAtom) {
00179         return m_TkhdAtom->SetDuration(duration);
00180     } else {
00181         return AP4_FAILURE;
00182     }
00183 }
00184 
00185 /*----------------------------------------------------------------------
00186 |       AP4_TrakAtom::AdjustChunkOffsets
00187 +---------------------------------------------------------------------*/
00188 AP4_Result    
00189 AP4_TrakAtom::AdjustChunkOffsets(AP4_Offset offset)
00190 {
00191     if (AP4_Atom* atom = FindChild("mdia/minf/stbl/co64")) {
00192         AP4_Co64Atom* co64 = dynamic_cast<AP4_Co64Atom*>(atom);
00193         co64->AdjustChunkOffsets(offset);
00194         }
00195 
00196     AP4_Atom* atom = FindChild("mdia/minf/stbl/stco");
00197     if (atom != NULL) {
00198         AP4_StcoAtom* stco = dynamic_cast<AP4_StcoAtom*>(atom);
00199         stco->AdjustChunkOffsets(offset);
00200         return AP4_SUCCESS;
00201     } else {
00202         return AP4_FAILURE;
00203     }
00204 }

Generated on Tue Dec 13 14:47:20 2005 for guliverkli by  doxygen 1.4.5