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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - stts Atoms 
  4. |
  5. |    Copyright 2003 Gilles Boccon-Gibod & Julien Boeuf
  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 "Ap4SttsAtom.h"
  33. #include "Ap4AtomFactory.h"
  34. #include "Ap4Utils.h"
  35. /*----------------------------------------------------------------------
  36. |       AP4_SttsAtom::AP4_SttsAtom
  37. +---------------------------------------------------------------------*/
  38. AP4_SttsAtom::AP4_SttsAtom() :
  39.     AP4_Atom(AP4_ATOM_TYPE_STTS, AP4_FULL_ATOM_HEADER_SIZE+4, true)
  40. {
  41. }
  42. /*----------------------------------------------------------------------
  43. |       AP4_SttsAtom::AP4_SttsAtom
  44. +---------------------------------------------------------------------*/
  45. AP4_SttsAtom::AP4_SttsAtom(AP4_Size size, AP4_ByteStream& stream) :
  46.     AP4_Atom(AP4_ATOM_TYPE_STTS, size, true, stream)
  47. {
  48.     AP4_UI32 entry_count;
  49.     stream.ReadUI32(entry_count);
  50.     while (entry_count--) {
  51.         AP4_UI32 sample_count;
  52.         AP4_UI32 sample_duration;
  53.         if (stream.ReadUI32(sample_count)    == AP4_SUCCESS &&
  54.             stream.ReadUI32(sample_duration) == AP4_SUCCESS) {
  55.             m_Entries.Append(AP4_SttsTableEntry(sample_count,
  56.                                                 sample_duration));
  57.         }
  58.     }
  59. }
  60. /*----------------------------------------------------------------------
  61. |       AP4_SttsAtom::GetDts
  62. +---------------------------------------------------------------------*/
  63. AP4_Result
  64. AP4_SttsAtom::GetDts(AP4_Ordinal sample, AP4_TimeStamp& dts)
  65. {
  66.     AP4_Ordinal sample_count_in_entry = sample;
  67.     dts = 0;
  68.     for (AP4_UI32 i = 0; i < m_Entries.ItemCount(); i++) {
  69.         AP4_SttsTableEntry& entry = m_Entries[i];
  70.         // check if we have the correct entry 
  71.         if (sample_count_in_entry <= entry.m_SampleCount) {
  72.             dts += (sample_count_in_entry - 1) * entry.m_SampleDuration;
  73.             return AP4_SUCCESS;
  74.         } else {
  75.             dts += entry.m_SampleCount * entry.m_SampleDuration;
  76.             sample_count_in_entry -= entry.m_SampleCount;
  77.         }
  78.     }
  79.     // sample is greater than the number of samples
  80.     return AP4_FAILURE;
  81. }
  82. /*----------------------------------------------------------------------
  83. |       AP4_SttsAtom::AddEntry
  84. +---------------------------------------------------------------------*/
  85. AP4_Result
  86. AP4_SttsAtom::AddEntry(AP4_UI32 sample_count, AP4_UI32 sample_duration)
  87. {
  88.     m_Entries.Append(AP4_SttsTableEntry(sample_count, sample_duration));
  89.     m_Size += 8;
  90.     return AP4_SUCCESS;
  91. }
  92. /*----------------------------------------------------------------------
  93. |       AP4_SttsAtom::WriteFields
  94. +---------------------------------------------------------------------*/
  95. AP4_Result
  96. AP4_SttsAtom::WriteFields(AP4_ByteStream& stream)
  97. {
  98.     AP4_Result result;
  99.     // write the entry count
  100.     AP4_Cardinal entry_count = m_Entries.ItemCount();
  101.     result = stream.WriteUI32(entry_count);
  102.     if (AP4_FAILED(result)) return result;
  103.     // write the entries
  104.     for (AP4_Ordinal i=0; i<entry_count; i++) {
  105.         // sample count
  106.         result = stream.WriteUI32(m_Entries[i].m_SampleCount);
  107.         if (AP4_FAILED(result)) return result;
  108.         // time offset
  109.         result = stream.WriteUI32(m_Entries[i].m_SampleDuration);
  110.         if (AP4_FAILED(result)) return result;
  111.     }
  112.     return AP4_SUCCESS;
  113. }
  114. /*----------------------------------------------------------------------
  115. |       AP4_SttsAtom::GetSampleIndexForTimeStamp
  116. +---------------------------------------------------------------------*/
  117. AP4_Result
  118. AP4_SttsAtom::GetSampleIndexForTimeStamp(AP4_TimeStamp ts, AP4_Ordinal& sample)
  119. {
  120.     // init
  121.     AP4_Cardinal entry_count = m_Entries.ItemCount();
  122.     AP4_Duration accumulated = 0;
  123.     sample = 0;
  124.     
  125.     for (AP4_Ordinal i=0; i<entry_count; i++) {
  126.         AP4_Duration next_accumulated = accumulated 
  127.             + m_Entries[i].m_SampleCount * m_Entries[i].m_SampleDuration;
  128.         
  129.         // check if the ts is in the range of this entry
  130.         if (ts < next_accumulated) {
  131.             sample += (ts - accumulated) / m_Entries[i].m_SampleDuration;
  132.             return AP4_SUCCESS;
  133.         }
  134.         // update accumulated and sample
  135.         accumulated = next_accumulated;
  136.         sample += m_Entries[i].m_SampleCount;
  137.     }
  138.     // ts not in range of the table
  139.     return AP4_FAILURE;
  140. }
  141. /*----------------------------------------------------------------------
  142. |       AP4_SttsAtom::InspectFields
  143. +---------------------------------------------------------------------*/
  144. AP4_Result
  145. AP4_SttsAtom::InspectFields(AP4_AtomInspector& inspector)
  146. {
  147.     inspector.AddField("entry_count", m_Entries.ItemCount());
  148.     return AP4_SUCCESS;
  149. }