Ap4Atom.h
上传用户:xjjlds
上传日期:2015-12-05
资源大小:22823k
文件大小:14k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - Atoms 
  4. |
  5. |    Copyright 2002 Gilles Boccon-Gibod
  6. |
  7. |
  8. |    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
  9. |
  10. |    Unless you have obtained Bento4 under a difference license,
  11. |    this version of Bento4 is Bento4|GPL.
  12. |    Bento4|GPL is free software; you can redistribute it and/or modify
  13. |    it under the terms of the GNU General Public License as published by
  14. |    the Free Software Foundation; either version 2, or (at your option)
  15. |    any later version.
  16. |
  17. |    Bento4|GPL is distributed in the hope that it will be useful,
  18. |    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. |    GNU General Public License for more details.
  21. |
  22. |    You should have received a copy of the GNU General Public License
  23. |    along with Bento4|GPL; see the file COPYING.  If not, write to the
  24. |    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  25. |    02111-1307, USA.
  26. |
  27.  ****************************************************************/
  28. #ifndef _AP4_ATOM_H_
  29. #define _AP4_ATOM_H_
  30. /*----------------------------------------------------------------------
  31. |       includes
  32. +---------------------------------------------------------------------*/
  33. #include "Ap4Types.h"
  34. #include "Ap4List.h"
  35. #include "Ap4ByteStream.h"
  36. #include "Ap4Debug.h"
  37. /*----------------------------------------------------------------------
  38. |       macros
  39. +---------------------------------------------------------------------*/
  40. #define AP4_ATOM_TYPE(a,b,c,d)  
  41.    ((((unsigned long)a)<<24) |  
  42.     (((unsigned long)b)<<16) |  
  43.     (((unsigned long)c)<< 8) |  
  44.     (((unsigned long)d)    ))
  45. /*----------------------------------------------------------------------
  46. |       constants
  47. +---------------------------------------------------------------------*/
  48. const int AP4_ATOM_HEADER_SIZE      = 8;
  49. const int AP4_FULL_ATOM_HEADER_SIZE = 12;
  50. const int AP4_ATOM_MAX_NAME_SIZE    = 256;
  51. const int AP4_ATOM_MAX_URI_SIZE     = 512;
  52. /*----------------------------------------------------------------------
  53. |       forward references
  54. +---------------------------------------------------------------------*/
  55. class AP4_AtomParent;
  56. /*----------------------------------------------------------------------
  57. |       AP4_AtomInspector
  58. +---------------------------------------------------------------------*/
  59. class AP4_AtomInspector {
  60. public:
  61.     // types
  62.     typedef enum {
  63.         HINT_NONE,
  64.         HINT_HEX,
  65.         HINT_BOOLEAN
  66.     } FormatHint;
  67.     // constructor and destructor
  68.     AP4_AtomInspector() {}
  69.     virtual ~AP4_AtomInspector() {}
  70.     // methods
  71.     virtual void StartElement(const char* name, const char* extra = NULL) {}
  72.     virtual void EndElement() {}
  73.     virtual void AddField(const char* name, AP4_UI32 value, FormatHint hint = HINT_NONE) {}
  74.     virtual void AddField(const char* name, const char* value, FormatHint hint = HINT_NONE) {}
  75. };
  76. /*----------------------------------------------------------------------
  77. |       AP4_Atom
  78. +---------------------------------------------------------------------*/
  79. class AP4_Atom {
  80.  public:
  81.     // types
  82.     typedef AP4_UI32 Type;
  83.     // methods
  84.                        AP4_Atom(Type type, 
  85.                                 bool is_full = false);
  86.                        AP4_Atom(Type     type, 
  87.                                 AP4_Size size, 
  88.                                 bool     is_full = false);
  89.                        AP4_Atom(Type            type, 
  90.                                 AP4_Size        size, 
  91.                                 bool            is_full,
  92.                                 AP4_ByteStream& stream);
  93.     virtual           ~AP4_Atom() {}
  94.     Type               GetType() { return m_Type; }
  95.     void               SetType(Type type) { m_Type = type; }
  96.     AP4_Size           GetHeaderSize();
  97.     virtual AP4_Size   GetSize() { return m_Size; }
  98.     virtual AP4_Result Write(AP4_ByteStream& stream);
  99.     virtual AP4_Result WriteHeader(AP4_ByteStream& stream);
  100.     virtual AP4_Result WriteFields(AP4_ByteStream& stream) = 0;
  101.     virtual AP4_Result Inspect(AP4_AtomInspector& inspector);
  102.     virtual AP4_Result InspectHeader(AP4_AtomInspector& inspector);
  103.     virtual AP4_Result InspectFields(AP4_AtomInspector& inspector) {
  104.         return AP4_SUCCESS; 
  105.     }
  106.     // parent/child realtionship methods
  107.     virtual AP4_Result      SetParent(AP4_AtomParent* parent) {
  108.         m_Parent = parent;
  109.         return AP4_SUCCESS;
  110.     }
  111.     virtual AP4_AtomParent* GetParent() { return m_Parent; }
  112.     virtual AP4_Result Detach();
  113.     // override this if your want to make an atom cloneable
  114.     virtual AP4_Atom*  Clone() { return NULL; }
  115.  protected:
  116.     // members
  117.     Type            m_Type;
  118.     AP4_Size        m_Size;
  119.     bool            m_IsFull;
  120.     AP4_UI32        m_Version;
  121.     AP4_UI32        m_Flags;
  122.     AP4_AtomParent* m_Parent;
  123. };
  124. /*----------------------------------------------------------------------
  125. |       AP4_AtomParent
  126. +---------------------------------------------------------------------*/
  127. class AP4_AtomParent {
  128. public:
  129.     // base methods
  130.     virtual ~AP4_AtomParent();
  131.     AP4_List<AP4_Atom>& GetChildren() { return m_Children; }
  132.     virtual AP4_Result  AddChild(AP4_Atom* child, int position = -1);
  133.     virtual AP4_Result  RemoveChild(AP4_Atom* child);
  134.     virtual AP4_Result  DeleteChild(AP4_Atom::Type type);
  135.     virtual AP4_Atom*   GetChild(AP4_Atom::Type type, AP4_Ordinal index = 0);
  136.     virtual AP4_Atom*   FindChild(const char* path, 
  137.                                   bool        auto_create = false);
  138.     // methods designed to be overridden
  139.     virtual void OnChildChanged(AP4_Atom* child) {}
  140.     virtual void OnChildAdded(AP4_Atom* child)   {}
  141.     virtual void OnChildRemoved(AP4_Atom* child) {}
  142. protected:
  143.     // members
  144.     AP4_List<AP4_Atom> m_Children;
  145. };
  146. /*----------------------------------------------------------------------
  147. |       AP4_UnknownAtom
  148. +---------------------------------------------------------------------*/
  149. class AP4_UnknownAtom : public AP4_Atom {
  150. public:
  151.     // constructor and destructor
  152.     AP4_UnknownAtom(AP4_Atom::Type   type, 
  153.                     AP4_Size         size, 
  154.                     bool             is_full,
  155.                     AP4_ByteStream&  stream);
  156.     ~AP4_UnknownAtom();
  157.     // methods
  158.     virtual AP4_Result WriteFields(AP4_ByteStream& stream);
  159. private:
  160.     // members
  161.     AP4_ByteStream* m_SourceStream;
  162.     AP4_Offset      m_SourceOffset;
  163. };
  164. /*----------------------------------------------------------------------
  165. |       atom types
  166. +---------------------------------------------------------------------*/
  167. const AP4_Atom::Type AP4_ATOM_TYPE_UDTA = AP4_ATOM_TYPE('u','d','t','a');
  168. const AP4_Atom::Type AP4_ATOM_TYPE_URL  = AP4_ATOM_TYPE('u','r','l',' ');
  169. const AP4_Atom::Type AP4_ATOM_TYPE_TRAK = AP4_ATOM_TYPE('t','r','a','k');
  170. const AP4_Atom::Type AP4_ATOM_TYPE_TKHD = AP4_ATOM_TYPE('t','k','h','d');
  171. const AP4_Atom::Type AP4_ATOM_TYPE_STTS = AP4_ATOM_TYPE('s','t','t','s');
  172. const AP4_Atom::Type AP4_ATOM_TYPE_STSZ = AP4_ATOM_TYPE('s','t','s','z');
  173. const AP4_Atom::Type AP4_ATOM_TYPE_STSS = AP4_ATOM_TYPE('s','t','s','s');
  174. const AP4_Atom::Type AP4_ATOM_TYPE_STSD = AP4_ATOM_TYPE('s','t','s','d');
  175. const AP4_Atom::Type AP4_ATOM_TYPE_STSC = AP4_ATOM_TYPE('s','t','s','c');
  176. const AP4_Atom::Type AP4_ATOM_TYPE_STCO = AP4_ATOM_TYPE('s','t','c','o');
  177. const AP4_Atom::Type AP4_ATOM_TYPE_CO64 = AP4_ATOM_TYPE('c','o','6','4');
  178. const AP4_Atom::Type AP4_ATOM_TYPE_STBL = AP4_ATOM_TYPE('s','t','b','l');
  179. const AP4_Atom::Type AP4_ATOM_TYPE_SINF = AP4_ATOM_TYPE('s','i','n','f');
  180. const AP4_Atom::Type AP4_ATOM_TYPE_SCHM = AP4_ATOM_TYPE('s','c','h','m');
  181. const AP4_Atom::Type AP4_ATOM_TYPE_SCHI = AP4_ATOM_TYPE('s','c','h','i');
  182. const AP4_Atom::Type AP4_ATOM_TYPE_MVHD = AP4_ATOM_TYPE('m','v','h','d');
  183. const AP4_Atom::Type AP4_ATOM_TYPE_MP4S = AP4_ATOM_TYPE('m','p','4','s');
  184. const AP4_Atom::Type AP4_ATOM_TYPE_MP4A = AP4_ATOM_TYPE('m','p','4','a');
  185. const AP4_Atom::Type AP4_ATOM_TYPE_MP4V = AP4_ATOM_TYPE('m','p','4','v');
  186. const AP4_Atom::Type AP4_ATOM_TYPE_AVC1 = AP4_ATOM_TYPE('a','v','c','1');
  187. const AP4_Atom::Type AP4_ATOM_TYPE_ENCA = AP4_ATOM_TYPE('e','n','c','a');
  188. const AP4_Atom::Type AP4_ATOM_TYPE_ENCV = AP4_ATOM_TYPE('e','n','c','v');
  189. const AP4_Atom::Type AP4_ATOM_TYPE_MOOV = AP4_ATOM_TYPE('m','o','o','v');
  190. const AP4_Atom::Type AP4_ATOM_TYPE_MINF = AP4_ATOM_TYPE('m','i','n','f');
  191. const AP4_Atom::Type AP4_ATOM_TYPE_META = AP4_ATOM_TYPE('m','e','t','a');
  192. const AP4_Atom::Type AP4_ATOM_TYPE_MDHD = AP4_ATOM_TYPE('m','d','h','d');
  193. const AP4_Atom::Type AP4_ATOM_TYPE_ILST = AP4_ATOM_TYPE('i','l','s','t');
  194. const AP4_Atom::Type AP4_ATOM_TYPE_HDLR = AP4_ATOM_TYPE('h','d','l','r');
  195. const AP4_Atom::Type AP4_ATOM_TYPE_FTYP = AP4_ATOM_TYPE('f','t','y','p');
  196. const AP4_Atom::Type AP4_ATOM_TYPE_ESDS = AP4_ATOM_TYPE('e','s','d','s');
  197. const AP4_Atom::Type AP4_ATOM_TYPE_EDTS = AP4_ATOM_TYPE('e','d','t','s');
  198. const AP4_Atom::Type AP4_ATOM_TYPE_DRMS = AP4_ATOM_TYPE('d','r','m','s');
  199. const AP4_Atom::Type AP4_ATOM_TYPE_DREF = AP4_ATOM_TYPE('d','r','e','f');
  200. const AP4_Atom::Type AP4_ATOM_TYPE_DINF = AP4_ATOM_TYPE('d','i','n','f');
  201. const AP4_Atom::Type AP4_ATOM_TYPE_CTTS = AP4_ATOM_TYPE('c','t','t','s');
  202. const AP4_Atom::Type AP4_ATOM_TYPE_MDIA = AP4_ATOM_TYPE('m','d','i','a');
  203. const AP4_Atom::Type AP4_ATOM_TYPE_VMHD = AP4_ATOM_TYPE('v','m','h','d');
  204. const AP4_Atom::Type AP4_ATOM_TYPE_SMHD = AP4_ATOM_TYPE('s','m','h','d');
  205. const AP4_Atom::Type AP4_ATOM_TYPE_NMHD = AP4_ATOM_TYPE('n','m','h','d');
  206. const AP4_Atom::Type AP4_ATOM_TYPE_HMHD = AP4_ATOM_TYPE('h','m','h','d');
  207. const AP4_Atom::Type AP4_ATOM_TYPE_FRMA = AP4_ATOM_TYPE('f','r','m','a');
  208. const AP4_Atom::Type AP4_ATOM_TYPE_MDAT = AP4_ATOM_TYPE('m','d','a','t');
  209. const AP4_Atom::Type AP4_ATOM_TYPE_FREE = AP4_ATOM_TYPE('f','r','e','e');
  210. const AP4_Atom::Type AP4_ATOM_TYPE_TIMS = AP4_ATOM_TYPE('t','i','m','s');
  211. const AP4_Atom::Type AP4_ATOM_TYPE_RTP  = AP4_ATOM_TYPE('r','t','p',' ');
  212. const AP4_Atom::Type AP4_ATOM_TYPE_HNTI = AP4_ATOM_TYPE('h','n','t','i');
  213. const AP4_Atom::Type AP4_ATOM_TYPE_SDP  = AP4_ATOM_TYPE('s','d','p',' ');
  214. const AP4_Atom::Type AP4_ATOM_TYPE_IKMS = AP4_ATOM_TYPE('i','K','M','S');
  215. const AP4_Atom::Type AP4_ATOM_TYPE_ISFM = AP4_ATOM_TYPE('i','S','F','M');
  216. const AP4_Atom::Type AP4_ATOM_TYPE_HINT = AP4_ATOM_TYPE('h','i','n','t');
  217. const AP4_Atom::Type AP4_ATOM_TYPE_TREF = AP4_ATOM_TYPE('t','r','e','f');
  218. const AP4_Atom::Type AP4_ATOM_TYPE_AVCC = AP4_ATOM_TYPE('a','v','c','C');
  219. const AP4_Atom::Type AP4_ATOM_TYPE_TEXT = AP4_ATOM_TYPE('t','e','x','t');
  220. const AP4_Atom::Type AP4_ATOM_TYPE_TX3G = AP4_ATOM_TYPE('t','x','3','g');
  221. const AP4_Atom::Type AP4_ATOM_TYPE_FTAB = AP4_ATOM_TYPE('f','t','a','b');
  222. const AP4_Atom::Type AP4_ATOM_TYPE_S263 = AP4_ATOM_TYPE('s','2','6','3');
  223. const AP4_Atom::Type AP4_ATOM_TYPE_SAMR = AP4_ATOM_TYPE('s','a','m','r');
  224. const AP4_Atom::Type AP4_ATOM_TYPE_CHPL = AP4_ATOM_TYPE('c','h','p','l');
  225. const AP4_Atom::Type AP4_ATOM_TYPE_NAM  = AP4_ATOM_TYPE(169,'n','a','m');
  226. const AP4_Atom::Type AP4_ATOM_TYPE_ART  = AP4_ATOM_TYPE(169,'A','R','T');
  227. const AP4_Atom::Type AP4_ATOM_TYPE_WRT  = AP4_ATOM_TYPE(169,'w','r','t');
  228. const AP4_Atom::Type AP4_ATOM_TYPE_ALB  = AP4_ATOM_TYPE(169,'a','l','b');
  229. const AP4_Atom::Type AP4_ATOM_TYPE_DAY  = AP4_ATOM_TYPE(169,'d','a','y');
  230. const AP4_Atom::Type AP4_ATOM_TYPE_TOO  = AP4_ATOM_TYPE(169,'t','o','o');
  231. const AP4_Atom::Type AP4_ATOM_TYPE_CMT  = AP4_ATOM_TYPE(169,'c','m','t');
  232. const AP4_Atom::Type AP4_ATOM_TYPE_GEN  = AP4_ATOM_TYPE(169,'g','e','n');
  233. const AP4_Atom::Type AP4_ATOM_TYPE_TRKN = AP4_ATOM_TYPE('t','r','k','n');
  234. // const AP4_Atom::Type AP4_ATOM_TYPE_  = AP4_ATOM_TYPE(169,'','','');
  235. // {"cpil","compilation"},
  236. // {"trkn","track"},
  237. // {"disk","disc"},
  238. // {"gnre","genre"},
  239. // {"covr","cover"},
  240. const AP4_Atom::Type AP4_ATOM_TYPE_DATA = AP4_ATOM_TYPE('d','a','t','a');
  241. /*----------------------------------------------------------------------
  242. |       AP4_AtomListInspector
  243. +---------------------------------------------------------------------*/
  244. class AP4_AtomListInspector : public AP4_List<AP4_Atom>::Item::Operator
  245. {
  246.  public:
  247.     AP4_AtomListInspector(AP4_AtomInspector& inspector) :
  248.         m_Inspector(inspector) {}
  249.     AP4_Result Action(AP4_Atom* atom) const {
  250.         atom->Inspect(m_Inspector);
  251.         return AP4_SUCCESS;
  252.     }
  253.  private:
  254.     AP4_AtomInspector& m_Inspector;
  255. };
  256. /*----------------------------------------------------------------------
  257. |       AP4_AtomListWriter
  258. +---------------------------------------------------------------------*/
  259. class AP4_AtomListWriter : public AP4_List<AP4_Atom>::Item::Operator
  260. {
  261.  public:
  262.     AP4_AtomListWriter(AP4_ByteStream& stream) :
  263.         m_Stream(stream) {}
  264.     AP4_Result Action(AP4_Atom* atom) const {
  265.         atom->Write(m_Stream);
  266.         return AP4_SUCCESS;
  267.     }
  268.  private:
  269.     AP4_ByteStream& m_Stream;
  270. };
  271. /*----------------------------------------------------------------------
  272. |       AP4_AtomFinder
  273. +---------------------------------------------------------------------*/
  274. class AP4_AtomFinder : public AP4_List<AP4_Atom>::Item::Finder
  275. {
  276.  public:
  277.     AP4_AtomFinder(AP4_Atom::Type type, AP4_Ordinal index = 0) : 
  278.        m_Type(type), m_Index(index) {}
  279.     AP4_Result Test(AP4_Atom* atom) const {
  280.         if (atom->GetType() == m_Type) {
  281.             if (m_Index-- == 0) {
  282.                 return AP4_SUCCESS;
  283.             } else {
  284.                 return AP4_FAILURE;
  285.             }
  286.         } else {
  287.             return AP4_FAILURE;
  288.         }
  289.     }
  290.  private:
  291.     AP4_Atom::Type      m_Type;
  292.     mutable AP4_Ordinal m_Index;
  293. };
  294. /*----------------------------------------------------------------------
  295. |       AP4_AtomSizeAdder
  296. +---------------------------------------------------------------------*/
  297. class AP4_AtomSizeAdder : public AP4_List<AP4_Atom>::Item::Operator {
  298. public:
  299.     AP4_AtomSizeAdder(AP4_Size& size) : m_Size(size) {}
  300. private:
  301.     AP4_Result Action(AP4_Atom* atom) const {
  302.         m_Size += atom->GetSize();
  303.         return AP4_SUCCESS;
  304.     }
  305.     AP4_Size& m_Size;
  306. };
  307. #endif // _AP4_ATOM_H_