1
0

atom_root.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. MP4RootAtom::MP4RootAtom()
  23. : MP4Atom(NULL)
  24. {
  25. ExpectChildAtom("moov", Required, OnlyOne);
  26. ExpectChildAtom("ftyp", Optional, OnlyOne);
  27. ExpectChildAtom("mdat", Optional, Many);
  28. ExpectChildAtom("free", Optional, Many);
  29. ExpectChildAtom("skip", Optional, Many);
  30. ExpectChildAtom("udta", Optional, Many);
  31. ExpectChildAtom("moof", Optional, Many);
  32. }
  33. void MP4RootAtom::BeginWrite(bool use64)
  34. {
  35. // only call under MP4Create() control
  36. WriteAtomType("ftyp", OnlyOne);
  37. m_pChildAtoms[GetLastMdatIndex()]->BeginWrite(m_pFile->Use64Bits("mdat"));
  38. }
  39. void MP4RootAtom::Write()
  40. {
  41. // no-op
  42. }
  43. void MP4RootAtom::FinishWrite(bool use64)
  44. {
  45. // finish writing last mdat atom
  46. u_int32_t mdatIndex = GetLastMdatIndex();
  47. m_pChildAtoms[mdatIndex]->FinishWrite(m_pFile->Use64Bits("mdat"));
  48. // write all atoms after last mdat
  49. u_int32_t size = m_pChildAtoms.Size();
  50. for (u_int32_t i = mdatIndex + 1; i < size; i++) {
  51. m_pChildAtoms[i]->Write();
  52. }
  53. }
  54. void MP4RootAtom::BeginOptimalWrite()
  55. {
  56. WriteAtomType("ftyp", OnlyOne);
  57. WriteAtomType("moov", OnlyOne);
  58. WriteAtomType("udta", Many);
  59. m_pChildAtoms[GetLastMdatIndex()]->BeginWrite(m_pFile->Use64Bits("mdat"));
  60. }
  61. void MP4RootAtom::FinishOptimalWrite()
  62. {
  63. // finish writing mdat
  64. m_pChildAtoms[GetLastMdatIndex()]->FinishWrite(m_pFile->Use64Bits("mdat"));
  65. // find moov atom
  66. u_int32_t size = m_pChildAtoms.Size();
  67. MP4Atom* pMoovAtom = NULL;
  68. u_int32_t i;
  69. for (i = 0; i < size; i++) {
  70. if (!strcmp("moov", m_pChildAtoms[i]->GetType())) {
  71. pMoovAtom = m_pChildAtoms[i];
  72. break;
  73. }
  74. }
  75. ASSERT(i < size);
  76. ASSERT(pMoovAtom != NULL);
  77. // rewrite moov so that updated chunkOffsets are written to disk
  78. m_pFile->SetPosition(pMoovAtom->GetStart());
  79. u_int64_t oldSize = pMoovAtom->GetSize();
  80. pMoovAtom->Write();
  81. // sanity check
  82. u_int64_t newSize = pMoovAtom->GetSize();
  83. ASSERT(oldSize == newSize);
  84. }
  85. u_int32_t MP4RootAtom::GetLastMdatIndex()
  86. {
  87. for (int32_t i = m_pChildAtoms.Size() - 1; i >= 0; i--) {
  88. if (!strcmp("mdat", m_pChildAtoms[i]->GetType())) {
  89. return i;
  90. }
  91. }
  92. ASSERT(false);
  93. return (u_int32_t)-1;
  94. }
  95. void MP4RootAtom::WriteAtomType(const char* type, bool onlyOne)
  96. {
  97. u_int32_t size = m_pChildAtoms.Size();
  98. for (u_int32_t i = 0; i < size; i++) {
  99. if (!strcmp(type, m_pChildAtoms[i]->GetType())) {
  100. m_pChildAtoms[i]->Write();
  101. if (onlyOne) {
  102. break;
  103. }
  104. }
  105. }
  106. }