Ap4Atom.h

Go to the documentation of this file.
00001 /*****************************************************************
00002 |
00003 |    AP4 - Atoms 
00004 |
00005 |    Copyright 2002-2006 Gilles Boccon-Gibod & Julien Boeuf
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  ****************************************************************/
00033 #ifndef _AP4_ATOM_H_
00034 #define _AP4_ATOM_H_
00035 
00036 /*----------------------------------------------------------------------
00037 |   includes
00038 +---------------------------------------------------------------------*/
00039 #include "Ap4Types.h"
00040 #include "Ap4List.h"
00041 #include "Ap4ByteStream.h"
00042 #include "Ap4Debug.h"
00043 
00044 /*----------------------------------------------------------------------
00045 |   macros
00046 +---------------------------------------------------------------------*/
00047 #define AP4_ATOM_TYPE(a,b,c,d)  \
00048    ((((unsigned long)a)<<24) |  \
00049     (((unsigned long)b)<<16) |  \
00050     (((unsigned long)c)<< 8) |  \
00051     (((unsigned long)d)    ))
00052 
00053 /*----------------------------------------------------------------------
00054 |   constants
00055 +---------------------------------------------------------------------*/
00056 const unsigned int AP4_ATOM_HEADER_SIZE      = 8;
00057 const unsigned int AP4_FULL_ATOM_HEADER_SIZE = 12;
00058 const unsigned int AP4_ATOM_MAX_NAME_SIZE    = 256;
00059 const unsigned int AP4_ATOM_MAX_URI_SIZE     = 512;
00060 
00061 /*----------------------------------------------------------------------
00062 |   forward references
00063 +---------------------------------------------------------------------*/
00064 class AP4_AtomParent;
00065 
00066 /*----------------------------------------------------------------------
00067 |   AP4_AtomInspector
00068 +---------------------------------------------------------------------*/
00073 class AP4_AtomInspector {
00074 public:
00075     // types
00076     typedef enum {
00077         HINT_NONE,
00078         HINT_HEX,
00079         HINT_BOOLEAN
00080     } FormatHint;
00081 
00082     // constructor and destructor
00083     AP4_AtomInspector() {}
00084     virtual ~AP4_AtomInspector() {}
00085 
00086     // methods
00087     virtual void StartElement(const char* /* name */, 
00088                               const char* /* extra = NULL */) {}
00089     virtual void EndElement() {}
00090     virtual void AddField(const char* /* name */, 
00091                           AP4_UI32    /* value */, 
00092                           FormatHint  hint = HINT_NONE) {
00093         (void)hint; // gcc warning
00094     }
00095     virtual void AddField(const char* /* name */, 
00096                           const char* /* value */, 
00097                           FormatHint  hint = HINT_NONE) {
00098         (void)hint; // gcc warning 
00099     }
00100     virtual void AddField(const char*          /* name */, 
00101                           const unsigned char* /* bytes */, 
00102                           AP4_Size             /* byte_count */, 
00103                           FormatHint           hint = HINT_NONE) {
00104         (void)hint; // gcc warning 
00105     }
00106 };
00107 
00108 /*----------------------------------------------------------------------
00109 |   AP4_Atom
00110 +---------------------------------------------------------------------*/
00114 class AP4_Atom {
00115  public:
00116     // types
00117     typedef AP4_UI32 Type;
00118 
00119     // class methods
00120     static AP4_Result ReadFullHeader(AP4_ByteStream& stream, 
00121                                      AP4_UI32&       version, 
00122                                      AP4_UI32&       flags);
00123 
00124     // constructors
00128     AP4_Atom(Type type, AP4_UI32 size = AP4_ATOM_HEADER_SIZE);
00129 
00133     AP4_Atom(Type type, AP4_UI64 size);
00134 
00138     AP4_Atom(Type     type, 
00139              AP4_UI32 size,
00140              AP4_UI32 version, 
00141              AP4_UI32 flags);
00142 
00146     AP4_Atom(Type     type, 
00147              AP4_UI64 size,
00148              AP4_UI32 version, 
00149              AP4_UI32 flags);
00150 
00151     // destructor
00152     virtual ~AP4_Atom() {}
00153 
00154     // methods
00155     Type               GetType() const { return m_Type; }
00156     void               SetType(Type type) { m_Type = type; }
00157     AP4_Size           GetHeaderSize() const;
00158     AP4_UI64           GetSize() const { return m_Size32 == 1?m_Size64:m_Size32; }
00159     void               SetSize(AP4_UI64 size, bool force_64 = false);
00160     AP4_UI32           GetSize32() const { return m_Size32; }
00161     void               SetSize32(AP4_UI32 size) { m_Size32 = size; }
00162     AP4_UI64           GetSize64() const { return m_Size64; }
00163     void               SetSize64(AP4_UI64 size) { m_Size64 = size; }
00164     virtual AP4_Result Write(AP4_ByteStream& stream);
00165     virtual AP4_Result WriteHeader(AP4_ByteStream& stream);
00166     virtual AP4_Result WriteFields(AP4_ByteStream& stream) = 0;
00167     virtual AP4_Result Inspect(AP4_AtomInspector& inspector);
00168     virtual AP4_Result InspectHeader(AP4_AtomInspector& inspector);
00169     virtual AP4_Result InspectFields(AP4_AtomInspector& /* inspector */) {
00170         return AP4_SUCCESS; 
00171     }
00172 
00173     // parent/child relationship methods
00174     virtual AP4_Result SetParent(AP4_AtomParent* parent) {
00175         m_Parent = parent;
00176         return AP4_SUCCESS;
00177     }
00178     virtual AP4_AtomParent* GetParent() { return m_Parent; }
00179     virtual AP4_Result      Detach();
00180 
00187     virtual AP4_Atom*  Clone() { return NULL; }
00188 
00189  protected:
00190     // members
00191     Type            m_Type;
00192     AP4_UI32        m_Size32; 
00193     AP4_UI64        m_Size64; // this is 0 if m_Size is not 1 (encoded in 32-bits)
00194                               // and non-zero only if m_Size is 1 (encoded in 64-bits)
00195     bool            m_IsFull;
00196     AP4_UI32        m_Version;
00197     AP4_UI32        m_Flags;
00198     AP4_AtomParent* m_Parent;
00199 };
00200 
00201 /*----------------------------------------------------------------------
00202 |   AP4_AtomParent
00203 +---------------------------------------------------------------------*/
00208 class AP4_AtomParent {
00209 public:
00210     // base methods
00211     virtual ~AP4_AtomParent();
00212     AP4_List<AP4_Atom>& GetChildren() { return m_Children; }
00213     virtual AP4_Result  AddChild(AP4_Atom* child, int position = -1);
00214     virtual AP4_Result  RemoveChild(AP4_Atom* child);
00215     virtual AP4_Result  DeleteChild(AP4_Atom::Type type);
00216     virtual AP4_Atom*   GetChild(AP4_Atom::Type type, AP4_Ordinal index = 0) const;
00217     virtual AP4_Atom*   FindChild(const char* path, 
00218                                   bool        auto_create = false);
00219 
00220     // methods designed to be overridden
00221     virtual void OnChildChanged(AP4_Atom* /* child */) {}
00222     virtual void OnChildAdded(AP4_Atom* /* child */)   {}
00223     virtual void OnChildRemoved(AP4_Atom* /* child */) {}
00224 
00225 protected:
00226     // members
00227     AP4_List<AP4_Atom> m_Children;
00228 };
00229 
00230 /*----------------------------------------------------------------------
00231 |   AP4_UnknownAtom
00232 +---------------------------------------------------------------------*/
00239 class AP4_UnknownAtom : public AP4_Atom {
00240 public:
00241     // constructor and destructor
00242     AP4_UnknownAtom(AP4_Atom::Type   type, 
00243                     AP4_UI64         size, 
00244                     AP4_ByteStream&  stream);
00245     ~AP4_UnknownAtom();
00246 
00247     // methods
00248     virtual AP4_Result WriteFields(AP4_ByteStream& stream);
00249     virtual AP4_Atom*  Clone();
00250 
00251 private:
00252     // members
00253     AP4_ByteStream* m_SourceStream;
00254     AP4_Position    m_SourcePosition;
00255 };
00256 
00257 /*----------------------------------------------------------------------
00258 |   atom types
00259 +---------------------------------------------------------------------*/
00260 const AP4_Atom::Type AP4_ATOM_TYPE_UDTA = AP4_ATOM_TYPE('u','d','t','a');
00261 const AP4_Atom::Type AP4_ATOM_TYPE_URL  = AP4_ATOM_TYPE('u','r','l',' ');
00262 const AP4_Atom::Type AP4_ATOM_TYPE_TRAK = AP4_ATOM_TYPE('t','r','a','k');
00263 const AP4_Atom::Type AP4_ATOM_TYPE_TKHD = AP4_ATOM_TYPE('t','k','h','d');
00264 const AP4_Atom::Type AP4_ATOM_TYPE_STTS = AP4_ATOM_TYPE('s','t','t','s');
00265 const AP4_Atom::Type AP4_ATOM_TYPE_STSZ = AP4_ATOM_TYPE('s','t','s','z');
00266 const AP4_Atom::Type AP4_ATOM_TYPE_STSS = AP4_ATOM_TYPE('s','t','s','s');
00267 const AP4_Atom::Type AP4_ATOM_TYPE_STSD = AP4_ATOM_TYPE('s','t','s','d');
00268 const AP4_Atom::Type AP4_ATOM_TYPE_STSC = AP4_ATOM_TYPE('s','t','s','c');
00269 const AP4_Atom::Type AP4_ATOM_TYPE_STCO = AP4_ATOM_TYPE('s','t','c','o');
00270 const AP4_Atom::Type AP4_ATOM_TYPE_CO64 = AP4_ATOM_TYPE('c','o','6','4');
00271 const AP4_Atom::Type AP4_ATOM_TYPE_STBL = AP4_ATOM_TYPE('s','t','b','l');
00272 const AP4_Atom::Type AP4_ATOM_TYPE_SINF = AP4_ATOM_TYPE('s','i','n','f');
00273 const AP4_Atom::Type AP4_ATOM_TYPE_SCHM = AP4_ATOM_TYPE('s','c','h','m');
00274 const AP4_Atom::Type AP4_ATOM_TYPE_SCHI = AP4_ATOM_TYPE('s','c','h','i');
00275 const AP4_Atom::Type AP4_ATOM_TYPE_MVHD = AP4_ATOM_TYPE('m','v','h','d');
00276 const AP4_Atom::Type AP4_ATOM_TYPE_MP4S = AP4_ATOM_TYPE('m','p','4','s');
00277 const AP4_Atom::Type AP4_ATOM_TYPE_MP4A = AP4_ATOM_TYPE('m','p','4','a');
00278 const AP4_Atom::Type AP4_ATOM_TYPE_MP4V = AP4_ATOM_TYPE('m','p','4','v');
00279 const AP4_Atom::Type AP4_ATOM_TYPE_AVC1 = AP4_ATOM_TYPE('a','v','c','1');
00280 const AP4_Atom::Type AP4_ATOM_TYPE_ENCA = AP4_ATOM_TYPE('e','n','c','a');
00281 const AP4_Atom::Type AP4_ATOM_TYPE_ENCV = AP4_ATOM_TYPE('e','n','c','v');
00282 const AP4_Atom::Type AP4_ATOM_TYPE_MOOV = AP4_ATOM_TYPE('m','o','o','v');
00283 const AP4_Atom::Type AP4_ATOM_TYPE_MINF = AP4_ATOM_TYPE('m','i','n','f');
00284 const AP4_Atom::Type AP4_ATOM_TYPE_META = AP4_ATOM_TYPE('m','e','t','a');
00285 const AP4_Atom::Type AP4_ATOM_TYPE_MDHD = AP4_ATOM_TYPE('m','d','h','d');
00286 const AP4_Atom::Type AP4_ATOM_TYPE_ILST = AP4_ATOM_TYPE('i','l','s','t');
00287 const AP4_Atom::Type AP4_ATOM_TYPE_HDLR = AP4_ATOM_TYPE('h','d','l','r');
00288 const AP4_Atom::Type AP4_ATOM_TYPE_FTYP = AP4_ATOM_TYPE('f','t','y','p');
00289 const AP4_Atom::Type AP4_ATOM_TYPE_ESDS = AP4_ATOM_TYPE('e','s','d','s');
00290 const AP4_Atom::Type AP4_ATOM_TYPE_EDTS = AP4_ATOM_TYPE('e','d','t','s');
00291 const AP4_Atom::Type AP4_ATOM_TYPE_DRMS = AP4_ATOM_TYPE('d','r','m','s');
00292 const AP4_Atom::Type AP4_ATOM_TYPE_DRMI = AP4_ATOM_TYPE('d','r','m','i');
00293 const AP4_Atom::Type AP4_ATOM_TYPE_DREF = AP4_ATOM_TYPE('d','r','e','f');
00294 const AP4_Atom::Type AP4_ATOM_TYPE_DINF = AP4_ATOM_TYPE('d','i','n','f');
00295 const AP4_Atom::Type AP4_ATOM_TYPE_CTTS = AP4_ATOM_TYPE('c','t','t','s');
00296 const AP4_Atom::Type AP4_ATOM_TYPE_MDIA = AP4_ATOM_TYPE('m','d','i','a');
00297 const AP4_Atom::Type AP4_ATOM_TYPE_VMHD = AP4_ATOM_TYPE('v','m','h','d');
00298 const AP4_Atom::Type AP4_ATOM_TYPE_SMHD = AP4_ATOM_TYPE('s','m','h','d');
00299 const AP4_Atom::Type AP4_ATOM_TYPE_NMHD = AP4_ATOM_TYPE('n','m','h','d');
00300 const AP4_Atom::Type AP4_ATOM_TYPE_HMHD = AP4_ATOM_TYPE('h','m','h','d');
00301 const AP4_Atom::Type AP4_ATOM_TYPE_FRMA = AP4_ATOM_TYPE('f','r','m','a');
00302 const AP4_Atom::Type AP4_ATOM_TYPE_MDAT = AP4_ATOM_TYPE('m','d','a','t');
00303 const AP4_Atom::Type AP4_ATOM_TYPE_FREE = AP4_ATOM_TYPE('f','r','e','e');
00304 const AP4_Atom::Type AP4_ATOM_TYPE_TIMS = AP4_ATOM_TYPE('t','i','m','s');
00305 const AP4_Atom::Type AP4_ATOM_TYPE_RTP_ = AP4_ATOM_TYPE('r','t','p',' ');
00306 const AP4_Atom::Type AP4_ATOM_TYPE_HNTI = AP4_ATOM_TYPE('h','n','t','i');
00307 const AP4_Atom::Type AP4_ATOM_TYPE_SDP_ = AP4_ATOM_TYPE('s','d','p',' ');
00308 const AP4_Atom::Type AP4_ATOM_TYPE_IKMS = AP4_ATOM_TYPE('i','K','M','S');
00309 const AP4_Atom::Type AP4_ATOM_TYPE_ISFM = AP4_ATOM_TYPE('i','S','F','M');
00310 const AP4_Atom::Type AP4_ATOM_TYPE_ISLT = AP4_ATOM_TYPE('i','S','L','T');
00311 const AP4_Atom::Type AP4_ATOM_TYPE_HINT = AP4_ATOM_TYPE('h','i','n','t');
00312 const AP4_Atom::Type AP4_ATOM_TYPE_TREF = AP4_ATOM_TYPE('t','r','e','f');
00313 const AP4_Atom::Type AP4_ATOM_TYPE_ODRM = AP4_ATOM_TYPE('o','d','r','m');
00314 const AP4_Atom::Type AP4_ATOM_TYPE_ODKM = AP4_ATOM_TYPE('o','d','k','m');
00315 const AP4_Atom::Type AP4_ATOM_TYPE_OHDR = AP4_ATOM_TYPE('o','h','d','r');
00316 const AP4_Atom::Type AP4_ATOM_TYPE_ODDA = AP4_ATOM_TYPE('o','d','d','a');
00317 const AP4_Atom::Type AP4_ATOM_TYPE_ODHE = AP4_ATOM_TYPE('o','d','h','e');
00318 const AP4_Atom::Type AP4_ATOM_TYPE_ODAF = AP4_ATOM_TYPE('o','d','a','f');
00319 const AP4_Atom::Type AP4_ATOM_TYPE_IPRO = AP4_ATOM_TYPE('i','p','r','o');
00320 const AP4_Atom::Type AP4_ATOM_TYPE_MDRI = AP4_ATOM_TYPE('m','d','r','i');
00321 const AP4_Atom::Type AP4_ATOM_TYPE_AVCC = AP4_ATOM_TYPE('a','v','c','C');
00322 
00323 /*----------------------------------------------------------------------
00324 |   AP4_AtomListInspector
00325 +---------------------------------------------------------------------*/
00326 class AP4_AtomListInspector : public AP4_List<AP4_Atom>::Item::Operator
00327 {
00328  public:
00329     AP4_AtomListInspector(AP4_AtomInspector& inspector) :
00330         m_Inspector(inspector) {}
00331     AP4_Result Action(AP4_Atom* atom) const {
00332         atom->Inspect(m_Inspector);
00333         return AP4_SUCCESS;
00334     }
00335 
00336  private:
00337     AP4_AtomInspector& m_Inspector;
00338 };
00339 
00340 /*----------------------------------------------------------------------
00341 |   AP4_AtomListWriter
00342 +---------------------------------------------------------------------*/
00343 class AP4_AtomListWriter : public AP4_List<AP4_Atom>::Item::Operator
00344 {
00345  public:
00346     AP4_AtomListWriter(AP4_ByteStream& stream) :
00347         m_Stream(stream) {}
00348     AP4_Result Action(AP4_Atom* atom) const;
00349 
00350  private:
00351     AP4_ByteStream& m_Stream;
00352 };
00353 
00354 /*----------------------------------------------------------------------
00355 |   AP4_AtomFinder
00356 +---------------------------------------------------------------------*/
00357 class AP4_AtomFinder : public AP4_List<AP4_Atom>::Item::Finder
00358 {
00359  public:
00360     AP4_AtomFinder(AP4_Atom::Type type, AP4_Ordinal index = 0) : 
00361        m_Type(type), m_Index(index) {}
00362     AP4_Result Test(AP4_Atom* atom) const {
00363         if (atom->GetType() == m_Type) {
00364             if (m_Index-- == 0) {
00365                 return AP4_SUCCESS;
00366             } else {
00367                 return AP4_FAILURE;
00368             }
00369         } else {
00370             return AP4_FAILURE;
00371         }
00372     }
00373  private:
00374     AP4_Atom::Type      m_Type;
00375     mutable AP4_Ordinal m_Index;
00376 };
00377 
00378 /*----------------------------------------------------------------------
00379 |   AP4_AtomSizeAdder
00380 +---------------------------------------------------------------------*/
00381 class AP4_AtomSizeAdder : public AP4_List<AP4_Atom>::Item::Operator {
00382 public:
00383     AP4_AtomSizeAdder(AP4_UI64& size) : m_Size(size) {}
00384 
00385 private:
00386     AP4_Result Action(AP4_Atom* atom) const {
00387         m_Size += atom->GetSize();
00388         return AP4_SUCCESS;
00389     }
00390     AP4_UI64& m_Size;
00391 };
00392 
00393 #endif // _AP4_ATOM_H_

Generated on Thu Mar 15 16:06:09 2007 for Bento4 MP4 SDK by  doxygen 1.5.1-p1