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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - ctts 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 "Ap4CttsAtom.h"
  33. #include "Ap4AtomFactory.h"
  34. #include "Ap4Utils.h"
  35. /*----------------------------------------------------------------------
  36. |       AP4_CttsAtom::AP4_CttsAtom
  37. +---------------------------------------------------------------------*/
  38. AP4_CttsAtom::AP4_CttsAtom(AP4_Size size, AP4_ByteStream& stream) :
  39.     AP4_Atom(AP4_ATOM_TYPE_CTTS, size, true, stream)
  40. {
  41.     AP4_UI32 entry_count;
  42.     stream.ReadUI32(entry_count);
  43.     while (entry_count--) {
  44.         AP4_UI32 sample_count;
  45.         AP4_UI32 sample_offset;
  46.         if (stream.ReadUI32(sample_count)  == AP4_SUCCESS &&
  47.             stream.ReadUI32(sample_offset) == AP4_SUCCESS) {
  48.             m_Entries.Append(AP4_CttsTableEntry(sample_count,
  49.                                                 sample_offset));
  50.         }
  51.     }
  52. }
  53. /*----------------------------------------------------------------------
  54. |       AP4_CttsAtom::GetCtsOffset
  55. +---------------------------------------------------------------------*/
  56. AP4_Result
  57. AP4_CttsAtom::GetCtsOffset(AP4_Ordinal sample, AP4_Offset& cts_offset)
  58. {
  59.     AP4_Ordinal current_sample = 0;
  60.     for (unsigned int i = 0; i < m_Entries.ItemCount(); i++) {
  61.         AP4_CttsTableEntry& entry = m_Entries[i];
  62.         current_sample += entry.m_SampleCount;        
  63.         // check if we have the right entry 
  64.         if (current_sample >= sample) {
  65.             cts_offset = entry.m_SampleOffset;
  66.             return AP4_SUCCESS;
  67.         }
  68.     }
  69.     // sample is greater than the number of samples
  70.     return AP4_FAILURE;
  71. }
  72. /*----------------------------------------------------------------------
  73. |       AP4_CttsAtom::WriteFields
  74. +---------------------------------------------------------------------*/
  75. AP4_Result
  76. AP4_CttsAtom::WriteFields(AP4_ByteStream& stream)
  77. {
  78.     AP4_Result result;
  79.     // write the entry count
  80.     AP4_Cardinal entry_count = m_Entries.ItemCount();
  81.     result = stream.WriteUI32(entry_count);
  82.     if (AP4_FAILED(result)) return result;
  83.     // write the entries
  84.     for (AP4_Ordinal i=0; i<entry_count; i++) {
  85.         // sample count
  86.         result = stream.WriteUI32(m_Entries[i].m_SampleCount);
  87.         if (AP4_FAILED(result)) return result;
  88.         // time offset
  89.         result = stream.WriteUI32(m_Entries[i].m_SampleOffset);
  90.         if (AP4_FAILED(result)) return result;
  91.     }
  92.     return AP4_SUCCESS;
  93. }
  94. /*----------------------------------------------------------------------
  95. |       AP4_CttsAtom::InspectFields
  96. +---------------------------------------------------------------------*/
  97. AP4_Result
  98. AP4_CttsAtom::InspectFields(AP4_AtomInspector& inspector)
  99. {
  100.     inspector.AddField("entry_count", m_Entries.ItemCount());
  101.     return AP4_SUCCESS;
  102. }