Ap4Atom.h

00001 /*****************************************************************
00002 |
00003 |    AP4 - 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 #ifndef _AP4_ATOM_H_
00030 #define _AP4_ATOM_H_
00031 
00032 /*----------------------------------------------------------------------
00033 |       includes
00034 +---------------------------------------------------------------------*/
00035 #include "Ap4Types.h"
00036 #include "Ap4List.h"
00037 #include "Ap4ByteStream.h"
00038 #include "Ap4Debug.h"
00039 
00040 /*----------------------------------------------------------------------
00041 |       macros
00042 +---------------------------------------------------------------------*/
00043 #define AP4_ATOM_TYPE(a,b,c,d)  \
00044    ((((unsigned long)a)<<24) |  \
00045     (((unsigned long)b)<<16) |  \
00046     (((unsigned long)c)<< 8) |  \
00047     (((unsigned long)d)    ))
00048 
00049 /*----------------------------------------------------------------------
00050 |       constants
00051 +---------------------------------------------------------------------*/
00052 const int AP4_ATOM_HEADER_SIZE      = 8;
00053 const int AP4_FULL_ATOM_HEADER_SIZE = 12;
00054 const int AP4_ATOM_MAX_NAME_SIZE    = 256;
00055 const int AP4_ATOM_MAX_URI_SIZE     = 512;
00056 
00057 /*----------------------------------------------------------------------
00058 |       forward references
00059 +---------------------------------------------------------------------*/
00060 class AP4_AtomParent;
00061 
00062 /*----------------------------------------------------------------------
00063 |       AP4_AtomInspector
00064 +---------------------------------------------------------------------*/
00065 class AP4_AtomInspector {
00066 public:
00067     // types
00068     typedef enum {
00069         HINT_NONE,
00070         HINT_HEX,
00071         HINT_BOOLEAN
00072     } FormatHint;
00073 
00074     // constructor and destructor
00075     AP4_AtomInspector() {}
00076     virtual ~AP4_AtomInspector() {}
00077 
00078     // methods
00079     virtual void StartElement(const char* name, const char* extra = NULL) {}
00080     virtual void EndElement() {}
00081     virtual void AddField(const char* name, AP4_UI32 value, FormatHint hint = HINT_NONE) {}
00082     virtual void AddField(const char* name, const char* value, FormatHint hint = HINT_NONE) {}
00083 };
00084 
00085 /*----------------------------------------------------------------------
00086 |       AP4_Atom
00087 +---------------------------------------------------------------------*/
00088 class AP4_Atom {
00089  public:
00090     // types
00091     typedef AP4_UI32 Type;
00092 
00093     // methods
00094                        AP4_Atom(Type type, 
00095                                 bool is_full = false);
00096                        AP4_Atom(Type     type, 
00097                                 AP4_Size size, 
00098                                 bool     is_full = false);
00099                        AP4_Atom(Type            type, 
00100                                 AP4_Size        size, 
00101                                 bool            is_full,
00102                                 AP4_ByteStream& stream);
00103     virtual           ~AP4_Atom() {}
00104     Type               GetType() { return m_Type; }
00105     void               SetType(Type type) { m_Type = type; }
00106     AP4_Size           GetHeaderSize();
00107     virtual AP4_Size   GetSize() { return m_Size; }
00108     virtual AP4_Result Write(AP4_ByteStream& stream);
00109     virtual AP4_Result WriteHeader(AP4_ByteStream& stream);
00110     virtual AP4_Result WriteFields(AP4_ByteStream& stream) = 0;
00111     virtual AP4_Result Inspect(AP4_AtomInspector& inspector);
00112     virtual AP4_Result InspectHeader(AP4_AtomInspector& inspector);
00113     virtual AP4_Result InspectFields(AP4_AtomInspector& inspector) {
00114         return AP4_SUCCESS; 
00115     }
00116 
00117     // parent/child realtionship methods
00118     virtual AP4_Result      SetParent(AP4_AtomParent* parent) {
00119         m_Parent = parent;
00120         return AP4_SUCCESS;
00121     }
00122     virtual AP4_AtomParent* GetParent() { return m_Parent; }
00123     virtual AP4_Result Detach();
00124 
00125     // override this if your want to make an atom cloneable
00126     virtual AP4_Atom*  Clone() { return NULL; }
00127 
00128  protected:
00129     // members
00130     Type            m_Type;
00131     AP4_Size        m_Size;
00132     bool            m_IsFull;
00133     AP4_UI32        m_Version;
00134     AP4_UI32        m_Flags;
00135     AP4_AtomParent* m_Parent;
00136 };
00137 
00138 /*----------------------------------------------------------------------
00139 |       AP4_AtomParent
00140 +---------------------------------------------------------------------*/
00141 class AP4_AtomParent {
00142 public:
00143     // base methods
00144     virtual ~AP4_AtomParent();
00145     AP4_List<AP4_Atom>& GetChildren() { return m_Children; }
00146     virtual AP4_Result  AddChild(AP4_Atom* child, int position = -1);
00147     virtual AP4_Result  RemoveChild(AP4_Atom* child);
00148     virtual AP4_Result  DeleteChild(AP4_Atom::Type type);
00149     virtual AP4_Atom*   GetChild(AP4_Atom::Type type, AP4_Ordinal index = 0);
00150     virtual AP4_Atom*   FindChild(const char* path, 
00151                                   bool        auto_create = false);
00152 
00153     // methods designed to be overridden
00154     virtual void OnChildChanged(AP4_Atom* child) {}
00155     virtual void OnChildAdded(AP4_Atom* child)   {}
00156     virtual void OnChildRemoved(AP4_Atom* child) {}
00157 
00158 protected:
00159     // members
00160     AP4_List<AP4_Atom> m_Children;
00161 };
00162 
00163 /*----------------------------------------------------------------------
00164 |       AP4_UnknownAtom
00165 +---------------------------------------------------------------------*/
00166 class AP4_UnknownAtom : public AP4_Atom {
00167 public:
00168     // constructor and destructor
00169     AP4_UnknownAtom(AP4_Atom::Type   type, 
00170                     AP4_Size         size, 
00171                     bool             is_full,
00172                     AP4_ByteStream&  stream);
00173     ~AP4_UnknownAtom();
00174 
00175     // methods
00176     virtual AP4_Result WriteFields(AP4_ByteStream& stream);
00177 
00178 private:
00179     // members
00180     AP4_ByteStream* m_SourceStream;
00181     AP4_Offset      m_SourceOffset;
00182 };
00183 
00184 /*----------------------------------------------------------------------
00185 |       atom types
00186 +---------------------------------------------------------------------*/
00187 const AP4_Atom::Type AP4_ATOM_TYPE_UDTA = AP4_ATOM_TYPE('u','d','t','a');
00188 const AP4_Atom::Type AP4_ATOM_TYPE_URL  = AP4_ATOM_TYPE('u','r','l',' ');
00189 const AP4_Atom::Type AP4_ATOM_TYPE_TRAK = AP4_ATOM_TYPE('t','r','a','k');
00190 const AP4_Atom::Type AP4_ATOM_TYPE_TKHD = AP4_ATOM_TYPE('t','k','h','d');
00191 const AP4_Atom::Type AP4_ATOM_TYPE_STTS = AP4_ATOM_TYPE('s','t','t','s');
00192 const AP4_Atom::Type AP4_ATOM_TYPE_STSZ = AP4_ATOM_TYPE('s','t','s','z');
00193 const AP4_Atom::Type AP4_ATOM_TYPE_STSS = AP4_ATOM_TYPE('s','t','s','s');
00194 const AP4_Atom::Type AP4_ATOM_TYPE_STSD = AP4_ATOM_TYPE('s','t','s','d');
00195 const AP4_Atom::Type AP4_ATOM_TYPE_STSC = AP4_ATOM_TYPE('s','t','s','c');
00196 const AP4_Atom::Type AP4_ATOM_TYPE_STCO = AP4_ATOM_TYPE('s','t','c','o');
00197 const AP4_Atom::Type AP4_ATOM_TYPE_CO64 = AP4_ATOM_TYPE('c','o','6','4');
00198 const AP4_Atom::Type AP4_ATOM_TYPE_STBL = AP4_ATOM_TYPE('s','t','b','l');
00199 const AP4_Atom::Type AP4_ATOM_TYPE_SINF = AP4_ATOM_TYPE('s','i','n','f');
00200 const AP4_Atom::Type AP4_ATOM_TYPE_SCHM = AP4_ATOM_TYPE('s','c','h','m');
00201 const AP4_Atom::Type AP4_ATOM_TYPE_SCHI = AP4_ATOM_TYPE('s','c','h','i');
00202 const AP4_Atom::Type AP4_ATOM_TYPE_MVHD = AP4_ATOM_TYPE('m','v','h','d');
00203 const AP4_Atom::Type AP4_ATOM_TYPE_MP4S = AP4_ATOM_TYPE('m','p','4','s');
00204 const AP4_Atom::Type AP4_ATOM_TYPE_MP4A = AP4_ATOM_TYPE('m','p','4','a');
00205 const AP4_Atom::Type AP4_ATOM_TYPE_MP4V = AP4_ATOM_TYPE('m','p','4','v');
00206 const AP4_Atom::Type AP4_ATOM_TYPE_AVC1 = AP4_ATOM_TYPE('a','v','c','1');
00207 const AP4_Atom::Type AP4_ATOM_TYPE_ENCA = AP4_ATOM_TYPE('e','n','c','a');
00208 const AP4_Atom::Type AP4_ATOM_TYPE_ENCV = AP4_ATOM_TYPE('e','n','c','v');
00209 const AP4_Atom::Type AP4_ATOM_TYPE_MOOV = AP4_ATOM_TYPE('m','o','o','v');
00210 const AP4_Atom::Type AP4_ATOM_TYPE_MINF = AP4_ATOM_TYPE('m','i','n','f');
00211 const AP4_Atom::Type AP4_ATOM_TYPE_META = AP4_ATOM_TYPE('m','e','t','a');
00212 const AP4_Atom::Type AP4_ATOM_TYPE_MDHD = AP4_ATOM_TYPE('m','d','h','d');
00213 const AP4_Atom::Type AP4_ATOM_TYPE_ILST = AP4_ATOM_TYPE('i','l','s','t');
00214 const AP4_Atom::Type AP4_ATOM_TYPE_HDLR = AP4_ATOM_TYPE('h','d','l','r');
00215 const AP4_Atom::Type AP4_ATOM_TYPE_FTYP = AP4_ATOM_TYPE('f','t','y','p');
00216 const AP4_Atom::Type AP4_ATOM_TYPE_ESDS = AP4_ATOM_TYPE('e','s','d','s');
00217 const AP4_Atom::Type AP4_ATOM_TYPE_EDTS = AP4_ATOM_TYPE('e','d','t','s');
00218 const AP4_Atom::Type AP4_ATOM_TYPE_DRMS = AP4_ATOM_TYPE('d','r','m','s');
00219 const AP4_Atom::Type AP4_ATOM_TYPE_DREF = AP4_ATOM_TYPE('d','r','e','f');
00220 const AP4_Atom::Type AP4_ATOM_TYPE_DINF = AP4_ATOM_TYPE('d','i','n','f');
00221 const AP4_Atom::Type AP4_ATOM_TYPE_CTTS = AP4_ATOM_TYPE('c','t','t','s');
00222 const AP4_Atom::Type AP4_ATOM_TYPE_MDIA = AP4_ATOM_TYPE('m','d','i','a');
00223 const AP4_Atom::Type AP4_ATOM_TYPE_VMHD = AP4_ATOM_TYPE('v','m','h','d');
00224 const AP4_Atom::Type AP4_ATOM_TYPE_SMHD = AP4_ATOM_TYPE('s','m','h','d');
00225 const AP4_Atom::Type AP4_ATOM_TYPE_NMHD = AP4_ATOM_TYPE('n','m','h','d');
00226 const AP4_Atom::Type AP4_ATOM_TYPE_HMHD = AP4_ATOM_TYPE('h','m','h','d');
00227 const AP4_Atom::Type AP4_ATOM_TYPE_FRMA = AP4_ATOM_TYPE('f','r','m','a');
00228 const AP4_Atom::Type AP4_ATOM_TYPE_MDAT = AP4_ATOM_TYPE('m','d','a','t');
00229 const AP4_Atom::Type AP4_ATOM_TYPE_FREE = AP4_ATOM_TYPE('f','r','e','e');
00230 const AP4_Atom::Type AP4_ATOM_TYPE_TIMS = AP4_ATOM_TYPE('t','i','m','s');
00231 const AP4_Atom::Type AP4_ATOM_TYPE_RTP  = AP4_ATOM_TYPE('r','t','p',' ');
00232 const AP4_Atom::Type AP4_ATOM_TYPE_HNTI = AP4_ATOM_TYPE('h','n','t','i');
00233 const AP4_Atom::Type AP4_ATOM_TYPE_SDP  = AP4_ATOM_TYPE('s','d','p',' ');
00234 const AP4_Atom::Type AP4_ATOM_TYPE_IKMS = AP4_ATOM_TYPE('i','K','M','S');
00235 const AP4_Atom::Type AP4_ATOM_TYPE_ISFM = AP4_ATOM_TYPE('i','S','F','M');
00236 const AP4_Atom::Type AP4_ATOM_TYPE_HINT = AP4_ATOM_TYPE('h','i','n','t');
00237 const AP4_Atom::Type AP4_ATOM_TYPE_TREF = AP4_ATOM_TYPE('t','r','e','f');
00238 
00239 const AP4_Atom::Type AP4_ATOM_TYPE_AVCC = AP4_ATOM_TYPE('a','v','c','C');
00240 const AP4_Atom::Type AP4_ATOM_TYPE_TEXT = AP4_ATOM_TYPE('t','e','x','t');
00241 const AP4_Atom::Type AP4_ATOM_TYPE_TX3G = AP4_ATOM_TYPE('t','x','3','g');
00242 const AP4_Atom::Type AP4_ATOM_TYPE_FTAB = AP4_ATOM_TYPE('f','t','a','b');
00243 const AP4_Atom::Type AP4_ATOM_TYPE_S263 = AP4_ATOM_TYPE('s','2','6','3');
00244 const AP4_Atom::Type AP4_ATOM_TYPE_SAMR = AP4_ATOM_TYPE('s','a','m','r');
00245 const AP4_Atom::Type AP4_ATOM_TYPE_CHPL = AP4_ATOM_TYPE('c','h','p','l');
00246 const AP4_Atom::Type AP4_ATOM_TYPE_NAM  = AP4_ATOM_TYPE(169,'n','a','m');
00247 const AP4_Atom::Type AP4_ATOM_TYPE_ART  = AP4_ATOM_TYPE(169,'A','R','T');
00248 const AP4_Atom::Type AP4_ATOM_TYPE_WRT  = AP4_ATOM_TYPE(169,'w','r','t');
00249 const AP4_Atom::Type AP4_ATOM_TYPE_ALB  = AP4_ATOM_TYPE(169,'a','l','b');
00250 const AP4_Atom::Type AP4_ATOM_TYPE_DAY  = AP4_ATOM_TYPE(169,'d','a','y');
00251 const AP4_Atom::Type AP4_ATOM_TYPE_TOO  = AP4_ATOM_TYPE(169,'t','o','o');
00252 const AP4_Atom::Type AP4_ATOM_TYPE_CMT  = AP4_ATOM_TYPE(169,'c','m','t');
00253 const AP4_Atom::Type AP4_ATOM_TYPE_GEN  = AP4_ATOM_TYPE(169,'g','e','n');
00254 const AP4_Atom::Type AP4_ATOM_TYPE_TRKN = AP4_ATOM_TYPE('t','r','k','n');
00255 // const AP4_Atom::Type AP4_ATOM_TYPE_  = AP4_ATOM_TYPE(169,'','','');
00256 //      {"cpil","compilation"},
00257 //      {"trkn","track"},
00258 //      {"disk","disc"},
00259 //      {"gnre","genre"},
00260 //      {"covr","cover"},
00261 const AP4_Atom::Type AP4_ATOM_TYPE_DATA = AP4_ATOM_TYPE('d','a','t','a');
00262 
00263 /*----------------------------------------------------------------------
00264 |       AP4_AtomListInspector
00265 +---------------------------------------------------------------------*/
00266 class AP4_AtomListInspector : public AP4_List<AP4_Atom>::Item::Operator
00267 {
00268  public:
00269     AP4_AtomListInspector(AP4_AtomInspector& inspector) :
00270         m_Inspector(inspector) {}
00271     AP4_Result Action(AP4_Atom* atom) const {
00272         atom->Inspect(m_Inspector);
00273         return AP4_SUCCESS;
00274     }
00275 
00276  private:
00277     AP4_AtomInspector& m_Inspector;
00278 };
00279 
00280 /*----------------------------------------------------------------------
00281 |       AP4_AtomListWriter
00282 +---------------------------------------------------------------------*/
00283 class AP4_AtomListWriter : public AP4_List<AP4_Atom>::Item::Operator
00284 {
00285  public:
00286     AP4_AtomListWriter(AP4_ByteStream& stream) :
00287         m_Stream(stream) {}
00288     AP4_Result Action(AP4_Atom* atom) const {
00289         atom->Write(m_Stream);
00290         return AP4_SUCCESS;
00291     }
00292 
00293  private:
00294     AP4_ByteStream& m_Stream;
00295 };
00296 
00297 /*----------------------------------------------------------------------
00298 |       AP4_AtomFinder
00299 +---------------------------------------------------------------------*/
00300 class AP4_AtomFinder : public AP4_List<AP4_Atom>::Item::Finder
00301 {
00302  public:
00303     AP4_AtomFinder(AP4_Atom::Type type, AP4_Ordinal index = 0) : 
00304        m_Type(type), m_Index(index) {}
00305     AP4_Result Test(AP4_Atom* atom) const {
00306         if (atom->GetType() == m_Type) {
00307             if (m_Index-- == 0) {
00308                 return AP4_SUCCESS;
00309             } else {
00310                 return AP4_FAILURE;
00311             }
00312         } else {
00313             return AP4_FAILURE;
00314         }
00315     }
00316  private:
00317     AP4_Atom::Type      m_Type;
00318     mutable AP4_Ordinal m_Index;
00319 };
00320 
00321 /*----------------------------------------------------------------------
00322 |       AP4_AtomSizeAdder
00323 +---------------------------------------------------------------------*/
00324 class AP4_AtomSizeAdder : public AP4_List<AP4_Atom>::Item::Operator {
00325 public:
00326     AP4_AtomSizeAdder(AP4_Size& size) : m_Size(size) {}
00327 
00328 private:
00329     AP4_Result Action(AP4_Atom* atom) const {
00330         m_Size += atom->GetSize();
00331         return AP4_SUCCESS;
00332     }
00333     AP4_Size& m_Size;
00334 };
00335 
00336 #endif // _AP4_ATOM_H_

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