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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - Atom Based Sample Tables
  4. |
  5. |    Copyright 2003 Gilles Boccon-Gibod & Julien Boeuf
  6. |
  7. |
  8. |    This atom is part of AP4 (MP4 Audio Proatom 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 atom 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 "Ap4AtomSampleTable.h"
  32. #include "Ap4ByteStream.h"
  33. #include "Ap4StsdAtom.h"
  34. #include "Ap4StscAtom.h"
  35. #include "Ap4StcoAtom.h"
  36. #include "Ap4Co64Atom.h"
  37. #include "Ap4StszAtom.h"
  38. #include "Ap4SttsAtom.h"
  39. #include "Ap4CttsAtom.h"
  40. #include "Ap4StssAtom.h"
  41. #include "Ap4Sample.h"
  42. #include "Ap4Atom.h"
  43. /*----------------------------------------------------------------------
  44. |       AP4_AtomSampleTable::AP4_AtomSampleTable
  45. +---------------------------------------------------------------------*/
  46. AP4_AtomSampleTable::AP4_AtomSampleTable(AP4_ContainerAtom* stbl, 
  47.                                          AP4_ByteStream&    sample_stream) :
  48.     m_SampleStream(sample_stream)
  49. {
  50.     m_StscAtom = dynamic_cast<AP4_StscAtom*>(stbl->GetChild(AP4_ATOM_TYPE_STSC));
  51.     m_StcoAtom = dynamic_cast<AP4_StcoAtom*>(stbl->GetChild(AP4_ATOM_TYPE_STCO));
  52.     m_Co64Atom = dynamic_cast<AP4_Co64Atom*>(stbl->GetChild(AP4_ATOM_TYPE_CO64));
  53.     m_StszAtom = dynamic_cast<AP4_StszAtom*>(stbl->GetChild(AP4_ATOM_TYPE_STSZ));
  54.     m_CttsAtom = dynamic_cast<AP4_CttsAtom*>(stbl->GetChild(AP4_ATOM_TYPE_CTTS));
  55.     m_SttsAtom = dynamic_cast<AP4_SttsAtom*>(stbl->GetChild(AP4_ATOM_TYPE_STTS));
  56.     m_StssAtom = dynamic_cast<AP4_StssAtom*>(stbl->GetChild(AP4_ATOM_TYPE_STSS));
  57.     m_StsdAtom = dynamic_cast<AP4_StsdAtom*>(stbl->GetChild(AP4_ATOM_TYPE_STSD));
  58.     // keep a reference to the sample stream
  59.     m_SampleStream.AddReference();
  60. }
  61. /*----------------------------------------------------------------------
  62. |       AP4_AtomSampleTable::~AP4_AtomSampleTable
  63. +---------------------------------------------------------------------*/
  64. AP4_AtomSampleTable::~AP4_AtomSampleTable()
  65. {
  66.     m_SampleStream.Release();
  67. }
  68. /*----------------------------------------------------------------------
  69. |       AP4_AtomSampleTable::GetSample
  70. +---------------------------------------------------------------------*/
  71. AP4_Result
  72. AP4_AtomSampleTable::GetSample(AP4_Ordinal index, 
  73.                                AP4_Sample& sample)
  74. {
  75.     AP4_Result result;
  76.     // MP4 uses 1-based indexes internally, so adjust by one
  77.     index++;
  78. // find out in which chunk this sample is located
  79.     AP4_Ordinal chunk, skip, desc;
  80.     result = m_StscAtom->GetChunkForSample(index, chunk, skip, desc);
  81.     if (AP4_FAILED(result)) return result;
  82.     
  83.     // check that the result is within bounds
  84.     if (skip > index) return AP4_ERROR_INTERNAL;
  85.     // get the atom offset for this chunk
  86.     AP4_Offset offset;
  87. if (m_StcoAtom) result = m_StcoAtom->GetChunkOffset(chunk, offset); 
  88. else if (m_Co64Atom) result = m_Co64Atom->GetChunkOffset(chunk, offset); 
  89. else result = AP4_ERROR_INTERNAL;    
  90.     if (AP4_FAILED(result)) return result;
  91.     
  92.     // compute the additional offset inside the chunk
  93.     for (unsigned int i = index-skip; i < index; i++) {
  94.         AP4_Size size;
  95.         result = m_StszAtom->GetSampleSize(i, size); 
  96.         if (AP4_FAILED(result)) return result;
  97.         offset += size;
  98.     }
  99.     // set the description index
  100.     sample.SetDescriptionIndex(desc-1); // adjust for 0-based indexes
  101.     // set the dts and cts
  102.     AP4_TimeStamp cts_offset, dts;
  103.     result = m_SttsAtom->GetDts(index, dts);
  104.     if (AP4_FAILED(result)) return result;
  105.     sample.SetDts(dts);
  106.     if (m_CttsAtom == NULL) {
  107.         sample.SetCts(dts);
  108.     } else {
  109.         result = m_CttsAtom->GetCtsOffset(index, cts_offset); 
  110.     if (AP4_FAILED(result)) return result;
  111.         sample.SetCts(dts + cts_offset);
  112.     }     
  113.     // set the size
  114.     AP4_Size sample_size;
  115.     result = m_StszAtom->GetSampleSize(index, sample_size);
  116.     if (AP4_FAILED(result)) return result;
  117.     sample.SetSize(sample_size);
  118.     // set the offset
  119.     sample.SetOffset(offset);
  120.     // set the data stream
  121.     sample.SetDataStream(m_SampleStream);
  122.     return AP4_SUCCESS;
  123. }
  124. /*----------------------------------------------------------------------
  125. |       AP4_AtomSampleTable::GetSampleCount
  126. +---------------------------------------------------------------------*/
  127. AP4_Cardinal
  128. AP4_AtomSampleTable::GetSampleCount()
  129. {
  130.     return m_StszAtom ? m_StszAtom->GetSampleCount() : 0;
  131. }
  132. /*----------------------------------------------------------------------
  133. |       AP4_AtomSampleTable::GetSampleDescription
  134. +---------------------------------------------------------------------*/
  135. AP4_SampleDescription*
  136. AP4_AtomSampleTable::GetSampleDescription(AP4_Ordinal index)
  137. {
  138.     return m_StsdAtom ? m_StsdAtom->GetSampleDescription(index) : NULL;
  139. }
  140. /*----------------------------------------------------------------------
  141. |       AP4_AtomSampleTable::GetSampleDescriptionCount
  142. +---------------------------------------------------------------------*/
  143. AP4_Cardinal
  144. AP4_AtomSampleTable::GetSampleDescriptionCount()
  145. {
  146.     return m_StsdAtom ? m_StsdAtom->GetSampleDescriptionCount() : 0;
  147. }
  148. /*----------------------------------------------------------------------
  149. |       AP4_AtomSampleTable::GetChunkForSample
  150. +---------------------------------------------------------------------*/
  151. AP4_Result 
  152. AP4_AtomSampleTable::GetChunkForSample(AP4_Ordinal  sample,
  153.                                        AP4_Ordinal& chunk,
  154.                                        AP4_Ordinal& skip,
  155.                                        AP4_Ordinal& sample_description)
  156. {
  157.     return m_StscAtom ? m_StscAtom->GetChunkForSample(sample, chunk, skip, sample_description) : AP4_FAILURE;
  158. }
  159. /*----------------------------------------------------------------------
  160. |       AP4_AtomSampleTable::GetChunkOffset
  161. +---------------------------------------------------------------------*/
  162. AP4_Result 
  163. AP4_AtomSampleTable::GetChunkOffset(AP4_Ordinal chunk, AP4_Offset& offset)
  164. {
  165.     return
  166. m_StcoAtom ? m_StcoAtom->GetChunkOffset(chunk, offset) :
  167. m_Co64Atom ? m_Co64Atom->GetChunkOffset(chunk, offset) :
  168. AP4_FAILURE;
  169. }
  170. /*----------------------------------------------------------------------
  171. |       AP4_AtomSampleTable::SetChunkOffset
  172. +---------------------------------------------------------------------*/
  173. AP4_Result 
  174. AP4_AtomSampleTable::SetChunkOffset(AP4_Ordinal chunk, AP4_Offset offset)
  175. {
  176.     return 
  177. m_StcoAtom ? m_StcoAtom->SetChunkOffset(chunk, offset) : 
  178. m_Co64Atom ? m_Co64Atom->SetChunkOffset(chunk, offset) : 
  179. AP4_FAILURE;
  180. }
  181. /*----------------------------------------------------------------------
  182. |       AP4_AtomSampleTable::SetSampleSize
  183. +---------------------------------------------------------------------*/
  184. AP4_Result 
  185. AP4_AtomSampleTable::SetSampleSize(AP4_Ordinal sample, AP4_Size size)
  186. {
  187.     return m_StszAtom ? m_StszAtom->SetSampleSize(sample, size) : AP4_FAILURE;
  188. }
  189. /*----------------------------------------------------------------------
  190. |       AP4_AtomSampleTable::GetSample
  191. +---------------------------------------------------------------------*/
  192. AP4_Result 
  193. AP4_AtomSampleTable::GetSampleIndexForTimeStamp(AP4_TimeStamp ts, 
  194.                                                 AP4_Ordinal& index)
  195. {
  196.     return m_SttsAtom ? m_SttsAtom->GetSampleIndexForTimeStamp(ts, index) 
  197.                       : AP4_FAILURE;
  198. }