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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - stsz 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 "Ap4StszAtom.h"
  33. #include "Ap4AtomFactory.h"
  34. #include "Ap4Utils.h"
  35. /*----------------------------------------------------------------------
  36. |       AP4_StszAtom::AP4_StszAtom
  37. +---------------------------------------------------------------------*/
  38. AP4_StszAtom::AP4_StszAtom() :
  39.     AP4_Atom(AP4_ATOM_TYPE_STSZ, AP4_FULL_ATOM_HEADER_SIZE+8, true),
  40.     m_SampleSize(0),
  41.     m_SampleCount(0)
  42. {
  43. }
  44. /*----------------------------------------------------------------------
  45. |       AP4_StszAtom::AP4_StszAtom
  46. +---------------------------------------------------------------------*/
  47. AP4_StszAtom::AP4_StszAtom(AP4_Size size, AP4_ByteStream& stream) :
  48.     AP4_Atom(AP4_ATOM_TYPE_STSZ, size, true, stream)
  49. {
  50.     stream.ReadUI32(m_SampleSize);
  51.     stream.ReadUI32(m_SampleCount);
  52.     unsigned long sample_count = m_SampleCount;
  53.     if (m_SampleSize == 0) { // means that the samples have different sizes
  54.         while (sample_count--) {
  55.             AP4_UI32 entry_size;
  56.             if (stream.ReadUI32(entry_size) == AP4_SUCCESS) {
  57.                 m_Entries.Append(entry_size);
  58.             }
  59.         }
  60.     }
  61. }
  62. /*----------------------------------------------------------------------
  63. |       AP4_StszAtom::WriteFields
  64. +---------------------------------------------------------------------*/
  65. AP4_Result
  66. AP4_StszAtom::WriteFields(AP4_ByteStream& stream)
  67. {
  68.     AP4_Result result;
  69.     // sample size
  70.     result = stream.WriteUI32(m_SampleSize);
  71.     if (AP4_FAILED(result)) return result;
  72.     // sample count
  73.     result = stream.WriteUI32(m_SampleCount);
  74.     if (AP4_FAILED(result)) return result;
  75.     // entries if needed (the samples have different sizes)
  76.     if (m_SampleSize == 0) {
  77.         for (AP4_UI32 i=0; i<m_SampleCount; i++) {
  78.             result = stream.WriteUI32(m_Entries[i]);
  79.             if (AP4_FAILED(result)) return result;
  80.         }
  81.     }
  82.     return result;
  83. }
  84. /*----------------------------------------------------------------------
  85. |       AP4_StszAtom::GetSampleCount
  86. +---------------------------------------------------------------------*/
  87. AP4_UI32
  88. AP4_StszAtom::GetSampleCount()
  89. {
  90.     return m_SampleCount;
  91. }
  92. /*----------------------------------------------------------------------
  93. |       AP4_StszAtom::GetSampleSize
  94. +---------------------------------------------------------------------*/
  95. AP4_Result
  96. AP4_StszAtom::GetSampleSize(AP4_Ordinal sample, AP4_Size& sample_size)
  97. {
  98.     // check the sample index
  99.     if (sample > m_SampleCount) {
  100.         sample_size = 0;
  101.         return AP4_ERROR_OUT_OF_RANGE;
  102.     } else {
  103.         // find the size
  104.         if (m_SampleSize != 0) { // constant size
  105.             sample_size = m_SampleSize;
  106.         } else {
  107.             sample_size = m_Entries[sample - 1];
  108.         }
  109.         return AP4_SUCCESS;
  110.     }
  111. }
  112. /*----------------------------------------------------------------------
  113. |       AP4_StszAtom::SetSampleSize
  114. +---------------------------------------------------------------------*/
  115. AP4_Result
  116. AP4_StszAtom::SetSampleSize(AP4_Ordinal sample, AP4_Size sample_size)
  117. {
  118.     // check the sample index
  119.     if (sample > m_SampleCount) {
  120.         return AP4_ERROR_OUT_OF_RANGE;
  121.     } else {
  122.         m_Entries[sample - 1] = sample_size;
  123.         return AP4_SUCCESS;
  124.     }
  125. }
  126. /*----------------------------------------------------------------------
  127. |       AP4_StszAtom::AddEntry
  128. +---------------------------------------------------------------------*/
  129. AP4_Result 
  130. AP4_StszAtom::AddEntry(AP4_UI32 size)
  131. {
  132.     m_Entries.Append(size);
  133.     m_SampleCount++;
  134.     m_Size += 4;
  135.     return AP4_SUCCESS;
  136. }
  137. /*----------------------------------------------------------------------
  138. |       AP4_StszAtom::InspectFields
  139. +---------------------------------------------------------------------*/
  140. AP4_Result
  141. AP4_StszAtom::InspectFields(AP4_AtomInspector& inspector)
  142. {
  143.     inspector.AddField("sample_size", m_SampleSize);
  144.     inspector.AddField("sample_count", m_Entries.ItemCount());
  145.     return AP4_SUCCESS;
  146. }