mp4property.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. All Rights Reserved.
  17. *
  18. * Contributor(s):
  19. * Dave Mackie [email protected]
  20. */
  21. #ifndef __MP4_PROPERTY_INCLUDED__
  22. #define __MP4_PROPERTY_INCLUDED__
  23. // forward declarations
  24. class MP4Atom;
  25. class MP4File;
  26. class MP4Descriptor;
  27. MP4ARRAY_DECL(MP4Descriptor, MP4Descriptor*);
  28. enum MP4PropertyType {
  29. Integer8Property,
  30. Integer16Property,
  31. Integer24Property,
  32. Integer32Property,
  33. Integer64Property,
  34. Float32Property,
  35. StringProperty,
  36. BytesProperty,
  37. TableProperty,
  38. DescriptorProperty,
  39. };
  40. class MP4Property {
  41. public:
  42. MP4Property(const char *name = NULL);
  43. virtual ~MP4Property() { }
  44. MP4Atom* GetParentAtom() {
  45. return m_pParentAtom;
  46. }
  47. virtual void SetParentAtom(MP4Atom* pParentAtom) {
  48. m_pParentAtom = pParentAtom;
  49. }
  50. const char *GetName() {
  51. return m_name;
  52. }
  53. virtual MP4PropertyType GetType() = 0;
  54. bool IsReadOnly() {
  55. return m_readOnly;
  56. }
  57. void SetReadOnly(bool value = true) {
  58. m_readOnly = value;
  59. }
  60. bool IsImplicit() {
  61. return m_implicit;
  62. }
  63. void SetImplicit(bool value = true) {
  64. m_implicit = value;
  65. }
  66. virtual u_int32_t GetCount() = 0;
  67. virtual void SetCount(u_int32_t count) = 0;
  68. virtual void Generate() { /* default is a no-op */ };
  69. virtual void Read(MP4File* pFile, u_int32_t index = 0) = 0;
  70. virtual void Write(MP4File* pFile, u_int32_t index = 0) = 0;
  71. virtual bool FindProperty(const char* name,
  72. MP4Property** ppProperty, u_int32_t* pIndex = NULL);
  73. protected:
  74. MP4Atom* m_pParentAtom;
  75. const char* m_name;
  76. bool m_readOnly;
  77. bool m_implicit;
  78. };
  79. MP4ARRAY_DECL(MP4Property, MP4Property*);
  80. class MP4IntegerProperty : public MP4Property {
  81. protected:
  82. MP4IntegerProperty(char* name)
  83. : MP4Property(name) { };
  84. public:
  85. u_int64_t GetValue(u_int32_t index = 0);
  86. void SetValue(u_int64_t value, u_int32_t index = 0);
  87. void InsertValue(u_int64_t value, u_int32_t index = 0);
  88. void DeleteValue(u_int32_t index = 0);
  89. void IncrementValue(int32_t increment = 1, u_int32_t index = 0);
  90. };
  91. template <class val_t, u_int8_t size, MP4PropertyType prop_type>
  92. class MP4IntegerPropertyT : public MP4IntegerProperty {
  93. public:
  94. MP4IntegerPropertyT(char* name)
  95. : MP4IntegerProperty(name) {
  96. SetCount(1);
  97. m_values[0] = 0;
  98. }
  99. MP4PropertyType GetType() {
  100. //return Integer##xsize##Property;
  101. return prop_type;
  102. }
  103. u_int32_t GetCount() {
  104. return m_values.Size();
  105. }
  106. void SetCount(u_int32_t count) {
  107. m_values.Resize(count);
  108. }
  109. val_t GetValue(u_int32_t index = 0) {
  110. return m_values[index];
  111. }
  112. void SetValue(val_t value, u_int32_t index = 0)
  113. {
  114. if (m_readOnly) {
  115. throw new MP4Error(EACCES, "property is read-only", m_name); \
  116. }
  117. m_values[index] = value;
  118. }
  119. void AddValue(val_t value) {
  120. m_values.Add(value);
  121. }
  122. void InsertValue(val_t value, u_int32_t index) {
  123. m_values.Insert(value, index);
  124. }
  125. void DeleteValue(u_int32_t index) {
  126. m_values.Delete(index);
  127. }
  128. void IncrementValue(int32_t increment = 1, u_int32_t index = 0) {
  129. m_values[index] += increment;
  130. }
  131. void Read(MP4File* pFile, u_int32_t index = 0) {
  132. if (m_implicit) {
  133. return;
  134. }
  135. m_values[index] = (val_t)pFile->ReadUInt(size/8);
  136. }
  137. void Write(MP4File* pFile, u_int32_t index = 0) {
  138. if (m_implicit) {
  139. return;
  140. }
  141. pFile->WriteUInt((u_int64_t)m_values[index], size/8);
  142. }
  143. protected:
  144. MP4TArray<val_t> m_values;
  145. };
  146. #define MP4INTEGER_PROPERTY_DECL(val_t, xsize) typedef MP4IntegerPropertyT<val_t, xsize, Integer##xsize##Property> MP4Integer##xsize##Property;
  147. MP4INTEGER_PROPERTY_DECL(u_int8_t, 8);
  148. MP4INTEGER_PROPERTY_DECL(u_int16_t, 16);
  149. MP4INTEGER_PROPERTY_DECL(u_int32_t, 24);
  150. MP4INTEGER_PROPERTY_DECL(u_int32_t, 32);
  151. MP4INTEGER_PROPERTY_DECL(u_int64_t, 64);
  152. class MP4BitfieldProperty : public MP4Integer64Property {
  153. public:
  154. MP4BitfieldProperty(char* name, u_int8_t numBits)
  155. : MP4Integer64Property(name) {
  156. ASSERT(numBits != 0);
  157. ASSERT(numBits <= 64);
  158. m_numBits = numBits;
  159. }
  160. u_int8_t GetNumBits() {
  161. return m_numBits;
  162. }
  163. void SetNumBits(u_int8_t numBits) {
  164. m_numBits = numBits;
  165. }
  166. void Read(MP4File* pFile, u_int32_t index = 0);
  167. void Write(MP4File* pFile, u_int32_t index = 0);
  168. protected:
  169. u_int8_t m_numBits;
  170. };
  171. class MP4Float32Property : public MP4Property {
  172. public:
  173. MP4Float32Property(char* name)
  174. : MP4Property(name) {
  175. m_useFixed16Format = false;
  176. m_useFixed32Format = false;
  177. SetCount(1);
  178. m_values[0] = 0.0;
  179. }
  180. MP4PropertyType GetType() {
  181. return Float32Property;
  182. }
  183. u_int32_t GetCount() {
  184. return m_values.Size();
  185. }
  186. void SetCount(u_int32_t count) {
  187. m_values.Resize(count);
  188. }
  189. float GetValue(u_int32_t index = 0) {
  190. return m_values[index];
  191. }
  192. void SetValue(float value, u_int32_t index = 0) {
  193. if (m_readOnly) {
  194. throw new MP4Error(EACCES, "property is read-only", m_name);
  195. }
  196. m_values[index] = value;
  197. }
  198. void AddValue(float value) {
  199. m_values.Add(value);
  200. }
  201. void InsertValue(float value, u_int32_t index) {
  202. m_values.Insert(value, index);
  203. }
  204. bool IsFixed16Format() {
  205. return m_useFixed16Format;
  206. }
  207. void SetFixed16Format(bool useFixed16Format = true) {
  208. m_useFixed16Format = useFixed16Format;
  209. }
  210. bool IsFixed32Format() {
  211. return m_useFixed32Format;
  212. }
  213. void SetFixed32Format(bool useFixed32Format = true) {
  214. m_useFixed32Format = useFixed32Format;
  215. }
  216. void Read(MP4File* pFile, u_int32_t index = 0);
  217. void Write(MP4File* pFile, u_int32_t index = 0);
  218. protected:
  219. bool m_useFixed16Format;
  220. bool m_useFixed32Format;
  221. MP4Float32Array m_values;
  222. };
  223. class MP4StringProperty : public MP4Property {
  224. public:
  225. MP4StringProperty(char* name,
  226. bool useCountedFormat = false, bool useUnicode = false);
  227. ~MP4StringProperty();
  228. MP4PropertyType GetType() {
  229. return StringProperty;
  230. }
  231. u_int32_t GetCount() {
  232. return m_values.Size();
  233. }
  234. void SetCount(u_int32_t count);
  235. const char* GetValue(u_int32_t index = 0) {
  236. return m_values[index];
  237. }
  238. void SetValue(const char* value, u_int32_t index = 0);
  239. void AddValue(const char* value) {
  240. u_int32_t count = GetCount();
  241. SetCount(count + 1);
  242. SetValue(value, count);
  243. }
  244. bool IsCountedFormat() {
  245. return m_useCountedFormat;
  246. }
  247. void SetCountedFormat(bool useCountedFormat) {
  248. m_useCountedFormat = useCountedFormat;
  249. }
  250. bool IsExpandedCountedFormat() {
  251. return m_useExpandedCount;
  252. }
  253. void SetExpandedCountedFormat(bool useExpandedCount) {
  254. m_useExpandedCount = useExpandedCount;
  255. }
  256. bool IsUnicode() {
  257. return m_useUnicode;
  258. }
  259. void SetUnicode(bool useUnicode) {
  260. m_useUnicode = useUnicode;
  261. }
  262. u_int32_t GetFixedLength() {
  263. return m_fixedLength;
  264. }
  265. void SetFixedLength(u_int32_t fixedLength) {
  266. m_fixedLength = fixedLength;
  267. }
  268. void Read(MP4File* pFile, u_int32_t index = 0);
  269. void Write(MP4File* pFile, u_int32_t index = 0);
  270. protected:
  271. bool m_useCountedFormat;
  272. bool m_useExpandedCount;
  273. bool m_useUnicode;
  274. u_int32_t m_fixedLength;
  275. MP4StringArray m_values;
  276. };
  277. class MP4BytesProperty : public MP4Property {
  278. public:
  279. MP4BytesProperty(char* name, u_int32_t valueSize = 0,
  280. u_int32_t defaultValueSize = 0);
  281. ~MP4BytesProperty();
  282. MP4PropertyType GetType() {
  283. return BytesProperty;
  284. }
  285. u_int32_t GetCount() {
  286. return m_values.Size();
  287. }
  288. void SetCount(u_int32_t count);
  289. void GetValue(u_int8_t** ppValue, u_int32_t* pValueSize,
  290. u_int32_t index = 0) {
  291. // N.B. caller must free memory
  292. *ppValue = (u_int8_t*)MP4Malloc(m_valueSizes[index]);
  293. memcpy(*ppValue, m_values[index], m_valueSizes[index]);
  294. *pValueSize = m_valueSizes[index];
  295. }
  296. void CopyValue(u_int8_t* pValue, u_int32_t index = 0) {
  297. // N.B. caller takes responsbility for valid pointer
  298. // and sufficient memory at the destination
  299. memcpy(pValue, m_values[index], m_valueSizes[index]);
  300. }
  301. void SetValue(const u_int8_t* pValue, u_int32_t valueSize,
  302. u_int32_t index = 0);
  303. void AddValue(const u_int8_t* pValue, u_int32_t valueSize) {
  304. u_int32_t count = GetCount();
  305. SetCount(count + 1);
  306. SetValue(pValue, valueSize, count);
  307. }
  308. u_int32_t GetValueSize(u_int32_t valueSize, u_int32_t index = 0) {
  309. return m_valueSizes[index];
  310. }
  311. void SetValueSize(u_int32_t valueSize, u_int32_t index = 0);
  312. u_int32_t GetFixedSize() {
  313. return m_fixedValueSize;
  314. }
  315. void SetFixedSize(u_int32_t fixedSize);
  316. void Read(MP4File* pFile, u_int32_t index = 0);
  317. void Write(MP4File* pFile, u_int32_t index = 0);
  318. protected:
  319. u_int32_t m_fixedValueSize;
  320. u_int32_t m_defaultValueSize;
  321. MP4Integer32Array m_valueSizes;
  322. MP4BytesArray m_values;
  323. };
  324. class MP4TableProperty : public MP4Property {
  325. public:
  326. MP4TableProperty(char* name, MP4IntegerProperty* pCountProperty);
  327. ~MP4TableProperty();
  328. MP4PropertyType GetType() {
  329. return TableProperty;
  330. }
  331. void SetParentAtom(MP4Atom* pParentAtom) {
  332. m_pParentAtom = pParentAtom;
  333. for (u_int32_t i = 0; i < m_pProperties.Size(); i++) {
  334. m_pProperties[i]->SetParentAtom(pParentAtom);
  335. }
  336. }
  337. void AddProperty(MP4Property* pProperty);
  338. MP4Property* GetProperty(u_int32_t index) {
  339. return m_pProperties[index];
  340. }
  341. virtual u_int32_t GetCount() {
  342. return m_pCountProperty->GetValue();
  343. }
  344. virtual void SetCount(u_int32_t count) {
  345. m_pCountProperty->SetValue(count);
  346. }
  347. void Read(MP4File* pFile, u_int32_t index = 0);
  348. void Write(MP4File* pFile, u_int32_t index = 0);
  349. bool FindProperty(const char* name,
  350. MP4Property** ppProperty, u_int32_t* pIndex = NULL);
  351. protected:
  352. virtual void ReadEntry(MP4File* pFile, u_int32_t index);
  353. virtual void WriteEntry(MP4File* pFile, u_int32_t index);
  354. bool FindContainedProperty(const char* name,
  355. MP4Property** ppProperty, u_int32_t* pIndex);
  356. protected:
  357. MP4IntegerProperty* m_pCountProperty;
  358. MP4PropertyArray m_pProperties;
  359. };
  360. class MP4DescriptorProperty : public MP4Property {
  361. public:
  362. MP4DescriptorProperty(char* name = NULL,
  363. u_int8_t tagsStart = 0, u_int8_t tagsEnd = 0,
  364. bool mandatory = false, bool onlyOne = false);
  365. ~MP4DescriptorProperty();
  366. MP4PropertyType GetType() {
  367. return DescriptorProperty;
  368. }
  369. void SetParentAtom(MP4Atom* pParentAtom);
  370. void SetSizeLimit(u_int64_t sizeLimit) {
  371. m_sizeLimit = sizeLimit;
  372. }
  373. u_int32_t GetCount() {
  374. return m_pDescriptors.Size();
  375. }
  376. void SetCount(u_int32_t count) {
  377. m_pDescriptors.Resize(count);
  378. }
  379. void SetTags(u_int8_t tagsStart, u_int8_t tagsEnd = 0) {
  380. m_tagsStart = tagsStart;
  381. m_tagsEnd = tagsEnd ? tagsEnd : tagsStart;
  382. }
  383. MP4Descriptor* AddDescriptor(u_int8_t tag);
  384. void AppendDescriptor(MP4Descriptor* pDescriptor) {
  385. m_pDescriptors.Add(pDescriptor);
  386. }
  387. void DeleteDescriptor(u_int32_t index);
  388. void Generate();
  389. void Read(MP4File* pFile, u_int32_t index = 0);
  390. void Write(MP4File* pFile, u_int32_t index = 0);
  391. bool FindProperty(const char* name,
  392. MP4Property** ppProperty, u_int32_t* pIndex = NULL);
  393. protected:
  394. virtual MP4Descriptor* CreateDescriptor(u_int8_t tag);
  395. bool FindContainedProperty(const char* name,
  396. MP4Property** ppProperty, u_int32_t* pIndex);
  397. protected:
  398. u_int8_t m_tagsStart;
  399. u_int8_t m_tagsEnd;
  400. u_int64_t m_sizeLimit;
  401. bool m_mandatory;
  402. bool m_onlyOne;
  403. MP4DescriptorArray m_pDescriptors;
  404. };
  405. class MP4QosQualifierProperty : public MP4DescriptorProperty {
  406. public:
  407. MP4QosQualifierProperty(char* name = NULL,
  408. u_int8_t tagsStart = 0, u_int8_t tagsEnd = 0,
  409. bool mandatory = false, bool onlyOne = false) :
  410. MP4DescriptorProperty(name, tagsStart, tagsEnd, mandatory, onlyOne) { }
  411. protected:
  412. MP4Descriptor* CreateDescriptor(u_int8_t tag);
  413. };
  414. #endif /* __MP4_PROPERTY_INCLUDED__ */