1
0

mp4atom.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * The contents of this file are subject to the Mozilla Public
  3. * License Version 1.1 (the "License"); you may not use this file
  4. * except in compliance with the License. You may obtain a copy of
  5. * the License at http://www.mozilla.org/MPL/
  6. *
  7. * Software distributed under the License is distributed on an "AS
  8. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9. * implied. See the License for the specific language governing
  10. * rights and limitations under the License.
  11. *
  12. * The Original Code is MPEG4IP.
  13. *
  14. * The Initial Developer of the Original Code is Cisco Systems Inc.
  15. * Portions created by Cisco Systems Inc. are
  16. * Copyright (C) Cisco Systems Inc. 2001 - 2004. All Rights Reserved.
  17. *
  18. * 3GPP features implementation is based on 3GPP's TS26.234-v5.60,
  19. * and was contributed by Ximpo Group Ltd.
  20. *
  21. * Portions created by Ximpo Group Ltd. are
  22. * Copyright (C) Ximpo Group Ltd. 2003, 2004. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. * Dave Mackie [email protected]
  26. * Ximpo Group Ltd. [email protected]
  27. */
  28. #ifndef __MP4_ATOM_INCLUDED__
  29. #define __MP4_ATOM_INCLUDED__
  30. class MP4Atom;
  31. MP4ARRAY_DECL(MP4Atom, MP4Atom*);
  32. #define Required true
  33. #define Optional false
  34. #define OnlyOne true
  35. #define Many false
  36. #define Counted true
  37. /* helper class */
  38. class MP4AtomInfo {
  39. public:
  40. MP4AtomInfo() {
  41. m_name = NULL;
  42. m_mandatory = false;
  43. m_onlyOne = false;
  44. m_count = 0;
  45. }
  46. MP4AtomInfo(const char* name, bool mandatory, bool onlyOne);
  47. const char* m_name;
  48. bool m_mandatory;
  49. bool m_onlyOne;
  50. u_int32_t m_count;
  51. };
  52. MP4ARRAY_DECL(MP4AtomInfo, MP4AtomInfo*);
  53. class MP4Atom {
  54. public:
  55. MP4Atom(const char* type = NULL);
  56. virtual ~MP4Atom();
  57. static MP4Atom* ReadAtom(MP4File* pFile, MP4Atom* pParentAtom);
  58. static MP4Atom* CreateAtom(const char* type);
  59. static bool IsReasonableType(const char* type);
  60. MP4File* GetFile() {
  61. return m_pFile;
  62. };
  63. void SetFile(MP4File* pFile) {
  64. m_pFile = pFile;
  65. };
  66. u_int64_t GetStart() {
  67. return m_start;
  68. };
  69. void SetStart(u_int64_t pos) {
  70. m_start = pos;
  71. };
  72. u_int64_t GetEnd() {
  73. return m_end;
  74. };
  75. void SetEnd(u_int64_t pos) {
  76. m_end = pos;
  77. };
  78. u_int64_t GetSize() {
  79. return m_size;
  80. }
  81. void SetSize(u_int64_t size) {
  82. m_size = size;
  83. }
  84. const char* GetType() {
  85. return m_type;
  86. };
  87. void SetType(const char* type) {
  88. if (type && *type != '\0') {
  89. // not needed ASSERT(strlen(type) == 4);
  90. memcpy(m_type, type, 4);
  91. m_type[4] = '\0';
  92. } else {
  93. memset(m_type, 0, 5);
  94. }
  95. }
  96. void GetExtendedType(u_int8_t* pExtendedType) {
  97. memcpy(pExtendedType, m_extendedType, sizeof(m_extendedType));
  98. };
  99. void SetExtendedType(u_int8_t* pExtendedType) {
  100. memcpy(m_extendedType, pExtendedType, sizeof(m_extendedType));
  101. };
  102. bool IsUnknownType() {
  103. return m_unknownType;
  104. }
  105. void SetUnknownType(bool unknownType = true) {
  106. m_unknownType = unknownType;
  107. }
  108. bool IsRootAtom() {
  109. return m_type[0] == '\0';
  110. }
  111. MP4Atom* GetParentAtom() {
  112. return m_pParentAtom;
  113. }
  114. void SetParentAtom(MP4Atom* pParentAtom) {
  115. m_pParentAtom = pParentAtom;
  116. }
  117. void AddChildAtom(MP4Atom* pChildAtom) {
  118. pChildAtom->SetFile(m_pFile);
  119. pChildAtom->SetParentAtom(this);
  120. m_pChildAtoms.Add(pChildAtom);
  121. }
  122. void InsertChildAtom(MP4Atom* pChildAtom, u_int32_t index) {
  123. pChildAtom->SetFile(m_pFile);
  124. pChildAtom->SetParentAtom(this);
  125. m_pChildAtoms.Insert(pChildAtom, index);
  126. }
  127. void DeleteChildAtom(MP4Atom* pChildAtom) {
  128. for (MP4ArrayIndex i = 0; i < m_pChildAtoms.Size(); i++) {
  129. if (m_pChildAtoms[i] == pChildAtom) {
  130. m_pChildAtoms.Delete(i);
  131. return;
  132. }
  133. }
  134. }
  135. u_int32_t GetNumberOfChildAtoms() {
  136. return m_pChildAtoms.Size();
  137. }
  138. MP4Atom* GetChildAtom(u_int32_t index) {
  139. return m_pChildAtoms[index];
  140. }
  141. MP4Property* GetProperty(u_int32_t index) {
  142. return m_pProperties[index];
  143. }
  144. u_int32_t GetCount() {
  145. return m_pProperties.Size();
  146. }
  147. #if 0
  148. void SetProperty(u_int32_t index, MP4Property *property) {
  149. u_int64_t t;
  150. if (index > m_pProperties.Size())
  151. return;
  152. t = property->Get(index);
  153. m_pProperties[index]->Set(t, index);
  154. }
  155. #endif
  156. MP4Atom* FindAtomMP4(const char* name);
  157. MP4Atom* FindChildAtom(const char* name);
  158. bool FindProperty(const char* name,
  159. MP4Property** ppProperty, u_int32_t* pIndex = NULL);
  160. u_int32_t GetFlags();
  161. void SetFlags(u_int32_t flags);
  162. u_int8_t GetDepth();
  163. void Skip();
  164. virtual void Generate();
  165. virtual void Read();
  166. virtual void BeginWrite(bool use64 = false);
  167. virtual void Write();
  168. virtual void Rewrite();
  169. virtual void FinishWrite(bool use64 = false);
  170. protected:
  171. void AddProperty(MP4Property* pProperty);
  172. void AddVersionAndFlags();
  173. void AddReserved(char* name, u_int32_t size);
  174. void ExpectChildAtom(const char* name,
  175. bool mandatory, bool onlyOne = true);
  176. MP4AtomInfo* FindAtomInfo(const char* name);
  177. bool IsMe(const char* name);
  178. bool FindContainedProperty(const char* name,
  179. MP4Property** ppProperty, u_int32_t* pIndex);
  180. void ReadProperties(
  181. u_int32_t startIndex = 0, u_int32_t count = 0xFFFFFFFF);
  182. void ReadChildAtoms();
  183. void WriteProperties(
  184. u_int32_t startIndex = 0, u_int32_t count = 0xFFFFFFFF);
  185. void WriteChildAtoms();
  186. u_int8_t GetVersion();
  187. void SetVersion(u_int8_t version);
  188. /* debugging aid */
  189. u_int32_t GetVerbosity();
  190. protected:
  191. MP4File* m_pFile;
  192. u_int64_t m_start;
  193. u_int64_t m_end;
  194. u_int64_t m_size;
  195. char m_type[5];
  196. bool m_unknownType;
  197. u_int8_t m_extendedType[16];
  198. MP4Atom* m_pParentAtom;
  199. u_int8_t m_depth;
  200. MP4PropertyArray m_pProperties;
  201. MP4AtomInfoArray m_pChildAtomInfos;
  202. MP4AtomArray m_pChildAtoms;
  203. };
  204. inline u_int32_t ATOMID(const char* type) {
  205. return STRTOINT32(type);
  206. }
  207. // inverse ATOMID - 32 bit id to string
  208. inline void IDATOM(u_int32_t type, char *s) {
  209. INT32TOSTR(type, s);
  210. }
  211. #endif /* __MP4_ATOM_INCLUDED__ */