- Main Page
- Classes
- Files

# Ap4Atom.h

Go to the documentation of this file.

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