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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - stsc Atoms 
  4. |
  5. |    Copyright 2002-2005 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 "Ap4StscAtom.h"
  33. #include "Ap4AtomFactory.h"
  34. #include "Ap4Utils.h"
  35. /*----------------------------------------------------------------------
  36. |       AP4_StscAtom::AP4_StscAtom
  37. +---------------------------------------------------------------------*/
  38. AP4_StscAtom::AP4_StscAtom() :
  39.     AP4_Atom(AP4_ATOM_TYPE_STSC, 4+AP4_FULL_ATOM_HEADER_SIZE, true),   
  40.     m_CachedChunkGroup(0)
  41. {
  42. }
  43. /*----------------------------------------------------------------------
  44. |       AP4_StscAtom::AP4_StscAtom
  45. +---------------------------------------------------------------------*/
  46. AP4_StscAtom::AP4_StscAtom(AP4_Size size, AP4_ByteStream& stream) :
  47.     AP4_Atom(AP4_ATOM_TYPE_STSC, size, true, stream),
  48.     m_CachedChunkGroup(0)
  49. {
  50.     AP4_UI32 first_sample = 1;
  51.     AP4_UI32 entry_count;
  52.     stream.ReadUI32(entry_count);
  53.     while (entry_count--) {
  54.         AP4_UI32 first_chunk;
  55.         AP4_UI32 samples_per_chunk;
  56.         AP4_UI32 sample_description_index;
  57.         if (stream.ReadUI32(first_chunk)              == AP4_SUCCESS &&
  58.             stream.ReadUI32(samples_per_chunk)        == AP4_SUCCESS &&
  59.             stream.ReadUI32(sample_description_index) == AP4_SUCCESS) {
  60.             if (m_Entries.ItemCount() != 0) {
  61.                 AP4_Ordinal prev = m_Entries.ItemCount()-1;
  62.                 m_Entries[prev].m_ChunkCount = 
  63.                     first_chunk-m_Entries[prev].m_FirstChunk;
  64.                 first_sample += 
  65.                     m_Entries[prev].m_ChunkCount *
  66.                     m_Entries[prev].m_SamplesPerChunk;
  67.             }
  68.             m_Entries.Append(AP4_StscTableEntry(first_chunk, 
  69.                                                 first_sample,
  70.                                                 samples_per_chunk, 
  71.                                                 sample_description_index));
  72.         }
  73.     }
  74. }
  75. /*----------------------------------------------------------------------
  76. |       AP4_StscAtom::WriteFields
  77. +---------------------------------------------------------------------*/
  78. AP4_Result
  79. AP4_StscAtom::WriteFields(AP4_ByteStream& stream)
  80. {
  81.     AP4_Result result;
  82.     // entry count
  83.     AP4_Cardinal entry_count = m_Entries.ItemCount();
  84.     result = stream.WriteUI32(entry_count);
  85.     // entries
  86.     for (AP4_Ordinal i=0; i<entry_count; i++) {
  87.         stream.WriteUI32(m_Entries[i].m_FirstChunk);
  88.         if (AP4_FAILED(result)) return result;
  89.         stream.WriteUI32(m_Entries[i].m_SamplesPerChunk);
  90.         if (AP4_FAILED(result)) return result;
  91.         stream.WriteUI32(m_Entries[i].m_SampleDescriptionIndex);
  92.         if (AP4_FAILED(result)) return result;
  93.     }
  94.     return result;
  95. }
  96. /*----------------------------------------------------------------------
  97. |       AP4_StscAtom::AddEntry
  98. +---------------------------------------------------------------------*/
  99. AP4_Result 
  100. AP4_StscAtom::AddEntry(AP4_Cardinal chunk_count,
  101.                        AP4_Cardinal samples_per_chunk,
  102.                        AP4_Ordinal  sample_description_index)
  103. {
  104.     AP4_Ordinal first_chunk;
  105.     AP4_Ordinal first_sample;
  106.     AP4_Cardinal entry_count = m_Entries.ItemCount();
  107.     if (entry_count == 0) {
  108.         // first entry
  109.         first_chunk = 1;
  110.         first_sample = 1;
  111.     } else {
  112.         first_chunk = m_Entries[entry_count-1].m_FirstChunk+
  113.                       m_Entries[entry_count-1].m_ChunkCount;
  114.         first_sample = m_Entries[entry_count-1].m_FirstSample+
  115.                        m_Entries[entry_count-1].m_ChunkCount*
  116.                        m_Entries[entry_count-1].m_SamplesPerChunk;
  117.     }
  118.     m_Entries.Append(AP4_StscTableEntry(first_chunk, first_sample, chunk_count, samples_per_chunk, sample_description_index));
  119.     // update the atom size
  120.     m_Size += 12;
  121.     return AP4_SUCCESS;
  122. }
  123. /*----------------------------------------------------------------------
  124. |       AP4_StscAtom::GetChunkForSample
  125. +---------------------------------------------------------------------*/
  126. AP4_Result
  127. AP4_StscAtom::GetChunkForSample(AP4_Ordinal  sample,
  128.                                 AP4_Ordinal& chunk,
  129.                                 AP4_Ordinal& skip,
  130.                                 AP4_Ordinal& sample_description)
  131. {
  132.     // preconditions
  133.     AP4_ASSERT(sample > 0);
  134.     // decide whether to start the search from the cached index
  135.     // or from the start
  136.     AP4_Ordinal group;
  137.     if (m_CachedChunkGroup < m_Entries.ItemCount() &&
  138.         m_Entries[m_CachedChunkGroup].m_FirstSample <= sample) {
  139.         group = m_CachedChunkGroup;
  140.     } else {
  141.         group = 0;
  142.     }
  143.     // find which group of chunk contains this one
  144.     while (group < m_Entries.ItemCount()) {
  145.         AP4_Cardinal sample_count = 
  146.             m_Entries[group].m_ChunkCount*m_Entries[group].m_SamplesPerChunk;
  147.         if (sample_count == 0) {
  148.             // unlimited samples in this group (last group)
  149.             if (m_Entries[group].m_FirstSample > sample) {
  150.                 // something is wrong
  151.                 return AP4_ERROR_INVALID_FORMAT;
  152.             }
  153.         } else {
  154.             // normal group
  155.             if (m_Entries[group].m_FirstSample + sample_count <= sample) {
  156.                 // the sample is not in this group
  157.                 group++;
  158.                 continue;
  159.             }
  160.         }
  161.         // the sample is in this group
  162.         if (m_Entries[group].m_SamplesPerChunk == 0) {
  163.             // something is wrong
  164.             return AP4_ERROR_INVALID_FORMAT;
  165.         }
  166.         unsigned int chunk_offset = 
  167.             ((sample-m_Entries[group].m_FirstSample) / 
  168.             m_Entries[group].m_SamplesPerChunk);
  169.         chunk = m_Entries[group].m_FirstChunk + chunk_offset;
  170.         skip = sample -
  171.             (m_Entries[group].m_FirstSample +
  172.              m_Entries[group].m_SamplesPerChunk*chunk_offset);
  173.         sample_description = m_Entries[group].m_SampleDescriptionIndex;
  174.         // cache the result (to accelerate finding the right group
  175.         // next time around
  176.         m_CachedChunkGroup = group;
  177.         return AP4_SUCCESS;
  178.     }
  179.     // chunk not found
  180.     chunk = 0;
  181.     skip = 0;
  182.     sample_description = 0;
  183.     return AP4_ERROR_OUT_OF_RANGE;
  184. }
  185. /*----------------------------------------------------------------------
  186. |       AP4_StscAtom::InspectFields
  187. +---------------------------------------------------------------------*/
  188. AP4_Result
  189. AP4_StscAtom::InspectFields(AP4_AtomInspector& inspector)
  190. {
  191.     inspector.AddField("entry_count", m_Entries.ItemCount());
  192.     // dump table entries
  193.     //for (unsigned int i=0; i<m_Entries.GetItemCount(); i++) {
  194.     //    char dump[256];
  195.     //    sprintf(dump, "  f=%ld, spc=%ld, sdi=%ldn", 
  196.     //            m_Entries[i].m_FirstChunk,
  197.     //            m_Entries[i].m_SamplesPerChunk,
  198.     //            m_Entries[i].m_SampleDescriptionIndex);
  199.     //    stream.WriteString(prefix);
  200.     //    stream.WriteString(dump);
  201.     //}
  202.     return AP4_SUCCESS;
  203. }