mp4descriptor.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #include "mp4common.h"
  22. MP4Descriptor::MP4Descriptor(u_int8_t tag) {
  23. m_tag = tag;
  24. m_pParentAtom = NULL;
  25. m_start = 0;
  26. m_size = 0;
  27. m_readMutatePoint = 0;
  28. }
  29. MP4Descriptor::~MP4Descriptor()
  30. {
  31. for (u_int32_t i = 0; i < m_pProperties.Size(); i++) {
  32. delete m_pProperties[i];
  33. }
  34. }
  35. void MP4Descriptor::AddProperty(MP4Property* pProperty)
  36. {
  37. ASSERT(pProperty);
  38. m_pProperties.Add(pProperty);
  39. pProperty->SetParentAtom(m_pParentAtom);
  40. }
  41. bool MP4Descriptor::FindContainedProperty(const char *name,
  42. MP4Property** ppProperty, u_int32_t* pIndex)
  43. {
  44. u_int32_t numProperties = m_pProperties.Size();
  45. for (u_int32_t i = 0; i < numProperties; i++) {
  46. if (m_pProperties[i]->FindProperty(name, ppProperty, pIndex)) {
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. void MP4Descriptor::Generate()
  53. {
  54. // generate properties
  55. for (u_int32_t i = 0; i < m_pProperties.Size(); i++) {
  56. m_pProperties[i]->Generate();
  57. }
  58. }
  59. void MP4Descriptor::Read(MP4File* pFile)
  60. {
  61. ReadHeader(pFile);
  62. ReadProperties(pFile, 0, m_readMutatePoint);
  63. Mutate();
  64. ReadProperties(pFile, m_readMutatePoint);
  65. // flush any leftover read bits
  66. pFile->FlushReadBits();
  67. }
  68. void MP4Descriptor::ReadHeader(MP4File* pFile)
  69. {
  70. VERBOSE_READ(pFile->GetVerbosity(),
  71. printf("ReadDescriptor: pos = 0x"X64"\n",
  72. pFile->GetPosition()));
  73. // read tag and length
  74. u_int8_t tag = pFile->ReadUInt8();
  75. if (m_tag) {
  76. ASSERT(tag == m_tag);
  77. } else {
  78. m_tag = tag;
  79. }
  80. m_size = pFile->ReadMpegLength();
  81. m_start = pFile->GetPosition();
  82. VERBOSE_READ(pFile->GetVerbosity(),
  83. printf("ReadDescriptor: tag 0x%02x data size %u (0x%x)\n",
  84. m_tag, m_size, m_size));
  85. }
  86. void MP4Descriptor::ReadProperties(MP4File* pFile,
  87. u_int32_t propStartIndex, u_int32_t propCount)
  88. {
  89. u_int32_t numProperties = MIN(propCount,
  90. m_pProperties.Size() - propStartIndex);
  91. for (u_int32_t i = propStartIndex;
  92. i < propStartIndex + numProperties; i++) {
  93. MP4Property* pProperty = m_pProperties[i];
  94. int32_t remaining = m_size - (pFile->GetPosition() - m_start);
  95. if (pProperty->GetType() == DescriptorProperty) {
  96. if (remaining > 0) {
  97. // place a limit on how far this sub-descriptor looks
  98. ((MP4DescriptorProperty*)pProperty)->SetSizeLimit(remaining);
  99. pProperty->Read(pFile);
  100. } // else do nothing, empty descriptor
  101. } else {
  102. // non-descriptor property
  103. if (remaining >= 0) {
  104. pProperty->Read(pFile);
  105. } else {
  106. VERBOSE_ERROR(pFile->GetVerbosity(),
  107. printf("Overran descriptor, tag %u data size %u property %u\n",
  108. m_tag, m_size, i));
  109. throw new MP4Error("overran descriptor",
  110. "MP4Descriptor::ReadProperties");
  111. }
  112. }
  113. }
  114. }
  115. void MP4Descriptor::Write(MP4File* pFile)
  116. {
  117. // call virtual function to adapt properties before writing
  118. Mutate();
  119. u_int32_t numProperties = m_pProperties.Size();
  120. if (numProperties == 0) {
  121. WARNING(numProperties == 0);
  122. return;
  123. }
  124. // write tag and length placeholder
  125. pFile->WriteUInt8(m_tag);
  126. u_int64_t lengthPos = pFile->GetPosition();
  127. pFile->WriteMpegLength(0);
  128. u_int64_t startPos = pFile->GetPosition();
  129. for (u_int32_t i = 0; i < numProperties; i++) {
  130. m_pProperties[i]->Write(pFile);
  131. }
  132. // align with byte boundary (rarely necessary)
  133. pFile->PadWriteBits();
  134. // go back and write correct length
  135. u_int64_t endPos = pFile->GetPosition();
  136. pFile->SetPosition(lengthPos);
  137. pFile->WriteMpegLength(endPos - startPos);
  138. pFile->SetPosition(endPos);
  139. }
  140. void MP4Descriptor::WriteToMemory(MP4File* pFile,
  141. u_int8_t** ppBytes, u_int64_t* pNumBytes)
  142. {
  143. // use memory buffer to save descriptor in memory
  144. // instead of going directly to disk
  145. pFile->EnableMemoryBuffer();
  146. Write(pFile);
  147. pFile->DisableMemoryBuffer(ppBytes, pNumBytes);
  148. }
  149. u_int8_t MP4Descriptor::GetDepth()
  150. {
  151. if (m_pParentAtom) {
  152. return m_pParentAtom->GetDepth();
  153. }
  154. return 0;
  155. }