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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - stco 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. /*----------------------------------------------------------------------
  29. |       includes
  30. +---------------------------------------------------------------------*/
  31. #include "Ap4.h"
  32. #include "Ap4StcoAtom.h"
  33. #include "Ap4AtomFactory.h"
  34. #include "Ap4Utils.h"
  35. /*----------------------------------------------------------------------
  36. |       AP4_StcoAtom::AP4_StcoAtom
  37. +---------------------------------------------------------------------*/
  38. AP4_StcoAtom::AP4_StcoAtom(AP4_UI32* entries, AP4_UI32 entry_count) :
  39. AP4_Atom(AP4_ATOM_TYPE_STCO,  
  40.          AP4_FULL_ATOM_HEADER_SIZE+4+entry_count*4, 
  41.          true),
  42.          m_Entries(new AP4_UI32[entry_count]),
  43.          m_EntryCount(entry_count)
  44. {
  45.     memcpy(m_Entries, entries, m_EntryCount*4);
  46. }
  47. /*----------------------------------------------------------------------
  48. |       AP4_StcoAtom::AP4_StcoAtom
  49. +---------------------------------------------------------------------*/
  50. AP4_StcoAtom::AP4_StcoAtom(AP4_Size size, AP4_ByteStream& stream) :
  51.     AP4_Atom(AP4_ATOM_TYPE_STCO, size, true, stream)
  52. {
  53.     stream.ReadUI32(m_EntryCount);
  54.     if (m_EntryCount > (size-AP4_FULL_ATOM_HEADER_SIZE-4)/4) {
  55.         m_EntryCount = (size-AP4_FULL_ATOM_HEADER_SIZE-4)/4;
  56.     }
  57.     m_Entries = new AP4_UI32[m_EntryCount];
  58.     for (AP4_Ordinal i=0; i<m_EntryCount; i++) {
  59.         stream.ReadUI32(m_Entries[i]);
  60.     }
  61. }
  62. /*----------------------------------------------------------------------
  63. |       AP4_StcoAtom::~AP4_StcoAtom
  64. +---------------------------------------------------------------------*/
  65. AP4_StcoAtom::~AP4_StcoAtom()
  66. {
  67.     delete[] m_Entries;
  68. }
  69. /*----------------------------------------------------------------------
  70. |       AP4_StcoAtom::GetChunkOffset
  71. +---------------------------------------------------------------------*/
  72. AP4_Result
  73. AP4_StcoAtom::GetChunkOffset(AP4_Ordinal chunk, AP4_Offset& chunk_offset)
  74. {
  75.     // check the bounds
  76.     if (chunk > m_EntryCount || chunk == 0) {
  77.         return AP4_ERROR_OUT_OF_RANGE;
  78.     }
  79.     // get the chunk offset
  80.     chunk_offset = m_Entries[chunk - 1]; // m_Entries is zero index based
  81.     return AP4_SUCCESS;
  82. }
  83. /*----------------------------------------------------------------------
  84. |       AP4_StcoAtom::SetChunkOffset
  85. +---------------------------------------------------------------------*/
  86. AP4_Result
  87. AP4_StcoAtom::SetChunkOffset(AP4_Ordinal chunk, AP4_Offset chunk_offset)
  88. {
  89.     // check the bounds
  90.     if (chunk > m_EntryCount || chunk == 0) {
  91.         return AP4_ERROR_OUT_OF_RANGE;
  92.     }
  93.     // get the chunk offset
  94.     m_Entries[chunk - 1] = chunk_offset; // m_Entries is zero index based
  95.     return AP4_SUCCESS;
  96. }
  97. /*----------------------------------------------------------------------
  98. |       AP4_StcoAtom::AdjustChunkOffsets
  99. +---------------------------------------------------------------------*/
  100. AP4_Result
  101. AP4_StcoAtom::AdjustChunkOffsets(AP4_Offset offset)
  102. {
  103.     for (AP4_Ordinal i=0; i<m_EntryCount; i++) {
  104.         m_Entries[i] += offset;
  105.     }
  106.     return AP4_SUCCESS;
  107. }
  108. /*----------------------------------------------------------------------
  109. |       AP4_StcoAtom::WriteFields
  110. +---------------------------------------------------------------------*/
  111. AP4_Result
  112. AP4_StcoAtom::WriteFields(AP4_ByteStream& stream)
  113. {
  114.     AP4_Result result;
  115.     // entry count
  116.     result = stream.WriteUI32(m_EntryCount);
  117.     if (AP4_FAILED(result)) return result;
  118.     // entries
  119.     for (AP4_Ordinal i=0; i<m_EntryCount; i++) {
  120.         result = stream.WriteUI32(m_Entries[i]);
  121.         if (AP4_FAILED(result)) return result;
  122.     }
  123.     return result;
  124. }
  125. /*----------------------------------------------------------------------
  126. |       AP4_StcoAtom::InspectFields
  127. +---------------------------------------------------------------------*/
  128. AP4_Result
  129. AP4_StcoAtom::InspectFields(AP4_AtomInspector& inspector)
  130. {
  131.     inspector.AddField("entry_count", m_EntryCount);
  132.     return AP4_SUCCESS;
  133. }