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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - stsd 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 "Ap4StsdAtom.h"
  33. #include "Ap4AtomFactory.h"
  34. #include "Ap4Utils.h"
  35. #include "Ap4SampleEntry.h"
  36. #include "Ap4SampleTable.h"
  37. /*----------------------------------------------------------------------
  38. |       AP4_StsdAtom::AP4_StsdAtom
  39. +---------------------------------------------------------------------*/
  40. AP4_StsdAtom::AP4_StsdAtom(AP4_SampleTable* sample_table) :
  41.     AP4_ContainerAtom(AP4_ATOM_TYPE_STSD, 4+AP4_FULL_ATOM_HEADER_SIZE, true)
  42. {
  43.     AP4_Cardinal sample_description_count = sample_table->GetSampleDescriptionCount();
  44.     m_SampleDescriptions.EnsureCapacity(sample_description_count);
  45.     for (AP4_Ordinal i=0; i<sample_description_count; i++) {
  46.         // clear the cache entry
  47.         m_SampleDescriptions.Append(NULL);
  48.         // create an entry for the description
  49.         AP4_SampleDescription* sample_description = sample_table->GetSampleDescription(i);
  50.         AP4_Atom* entry = sample_description->ToAtom();
  51.         m_Children.Add(entry);
  52.         // update the size
  53.         m_Size += entry->GetSize();
  54.     }
  55. }
  56. /*----------------------------------------------------------------------
  57. |       AP4_StsdAtom::AP4_StsdAtom
  58. +---------------------------------------------------------------------*/
  59. AP4_StsdAtom::AP4_StsdAtom(AP4_Size         size,
  60.                            AP4_ByteStream&  stream,
  61.                            AP4_AtomFactory& atom_factory) :
  62.     AP4_ContainerAtom(AP4_ATOM_TYPE_STSD, size, true, stream)
  63. {
  64.     // read the number of entries
  65.     AP4_UI32 entry_count;
  66.     stream.ReadUI32(entry_count);
  67.     // read all entries
  68.     AP4_Size bytes_available = size-AP4_FULL_ATOM_HEADER_SIZE-4;
  69.     for (unsigned int i=0; i<entry_count; i++) {
  70.         AP4_Atom* atom;
  71.         if (AP4_SUCCEEDED(atom_factory.CreateAtomFromStream(stream, 
  72.                                                             bytes_available,
  73.                                                             atom))) {
  74.             atom->SetParent(this);
  75.             m_Children.Add(atom);
  76.         }
  77.     }
  78.     // initialize the sample description cache
  79.     m_SampleDescriptions.EnsureCapacity(m_Children.ItemCount());
  80.     for (AP4_Ordinal i=0; i<m_Children.ItemCount(); i++) {
  81.         m_SampleDescriptions.Append(NULL);
  82.     }
  83. }
  84. /*----------------------------------------------------------------------
  85. |       AP4_StsdAtom::~AP4_StsdAtom
  86. +---------------------------------------------------------------------*/
  87. AP4_StsdAtom::~AP4_StsdAtom()
  88. {
  89.     for (AP4_Ordinal i=0; i<m_SampleDescriptions.ItemCount(); i++) {
  90.         delete m_SampleDescriptions[i];
  91.     }
  92. }
  93. /*----------------------------------------------------------------------
  94. |       AP4_StsdAtom::WriteFields
  95. +---------------------------------------------------------------------*/
  96. AP4_Result
  97. AP4_StsdAtom::WriteFields(AP4_ByteStream& stream)
  98. {
  99.     AP4_Result result;
  100.     // entry count
  101.     result = stream.WriteUI32(m_Children.ItemCount());
  102.     if (AP4_FAILED(result)) return result;
  103.     // entries
  104.     return m_Children.Apply(AP4_AtomListWriter(stream));
  105. }
  106. /*----------------------------------------------------------------------
  107. |       AP4_StsdAtom::OnChildChanged
  108. +---------------------------------------------------------------------*/
  109. void
  110. AP4_StsdAtom::OnChildChanged(AP4_Atom*)
  111. {
  112.     // remcompute our size
  113.     m_Size = GetHeaderSize()+4;
  114.     m_Children.Apply(AP4_AtomSizeAdder(m_Size));
  115.     // update our parent
  116.     if (m_Parent) m_Parent->OnChildChanged(this);
  117. }
  118. /*----------------------------------------------------------------------
  119. |       AP4_StsdAtom::GetSampleDescription
  120. +---------------------------------------------------------------------*/
  121. AP4_SampleDescription*
  122. AP4_StsdAtom::GetSampleDescription(AP4_Ordinal index)
  123. {
  124.     // check index
  125.     if (index >= m_Children.ItemCount()) return NULL;
  126.     // return the description if we already have it in the internal table
  127.     if (m_SampleDescriptions[index]) return m_SampleDescriptions[index];
  128.     // create and cache a sample description for this entry
  129.     AP4_Atom* entry;
  130.     m_Children.Get(index, entry);
  131.     AP4_SampleEntry* sample_entry = dynamic_cast<AP4_SampleEntry*>(entry);
  132.     if (sample_entry == NULL) return NULL;
  133.     m_SampleDescriptions[index] = sample_entry->ToSampleDescription();
  134.     return m_SampleDescriptions[index];
  135. }
  136. /*----------------------------------------------------------------------
  137. |       AP4_StsdAtom::GetSampleEntry
  138. +---------------------------------------------------------------------*/
  139. AP4_SampleEntry*
  140. AP4_StsdAtom::GetSampleEntry(AP4_Ordinal index)
  141. {
  142.     // check index
  143.     if (index >= m_Children.ItemCount()) return NULL;
  144.     // return the sample entry
  145.     AP4_Atom* entry;
  146.     m_Children.Get(index, entry);
  147.     return dynamic_cast<AP4_SampleEntry*>(entry);
  148. }
  149. /*----------------------------------------------------------------------
  150. |       AP4_StsdAtom::GetSampleDescriptionCount
  151. +---------------------------------------------------------------------*/
  152. AP4_Cardinal
  153. AP4_StsdAtom::GetSampleDescriptionCount()
  154. {
  155.     return m_Children.ItemCount();
  156. }
  157. /*----------------------------------------------------------------------
  158. |       AP4_StsdAtom::InspectFields
  159. +---------------------------------------------------------------------*/
  160. AP4_Result
  161. AP4_StsdAtom::InspectFields(AP4_AtomInspector& inspector)
  162. {
  163.     inspector.AddField("entry-count", m_Children.ItemCount());
  164.     
  165.     // inspect children
  166.     m_Children.Apply(AP4_AtomListInspector(inspector));
  167.     return AP4_SUCCESS;
  168. }