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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - Sample Objects
  4. |
  5. |    Copyright 2002 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 "Ap4Sample.h"
  32. #include "Ap4Utils.h"
  33. /*----------------------------------------------------------------------
  34. |       AP4_Sample::AP4_Sample
  35. +---------------------------------------------------------------------*/
  36. AP4_Sample::AP4_Sample() :
  37.     m_DataStream(NULL),
  38.     m_Offset(0),
  39.     m_Size(0),
  40.     m_DescriptionIndex(0),
  41.     m_Dts(0),
  42.     m_Cts(0)
  43. {
  44. }
  45. /*----------------------------------------------------------------------
  46. |       AP4_Sample::AP4_Sample
  47. +---------------------------------------------------------------------*/
  48. AP4_Sample::AP4_Sample(AP4_ByteStream& data_stream,
  49.                        AP4_Offset      offset,
  50.                        AP4_Size        size,
  51.                        AP4_Ordinal     description_index,
  52.                        AP4_TimeStamp   dts,
  53.                        AP4_Offset      cts_offset /* = 0 */ ) :
  54.     m_Offset(offset),
  55.     m_Size(size),
  56.     m_DescriptionIndex(description_index),
  57.     m_Dts(dts),
  58.     m_Cts(dts + cts_offset)
  59. {
  60.     m_DataStream = &data_stream;
  61.     AP4_ADD_REFERENCE(m_DataStream);
  62. }
  63. /*----------------------------------------------------------------------
  64. |       AP4_Sample::AP4_Sample
  65. +---------------------------------------------------------------------*/
  66. AP4_Sample::AP4_Sample(const AP4_Sample& other) :
  67.     m_DataStream(other.m_DataStream),
  68.     m_Offset(other.m_Offset),
  69.     m_Size(other.m_Size),
  70.     m_DescriptionIndex(other.m_DescriptionIndex),
  71.     m_Dts(other.m_Dts),
  72.     m_Cts(other.m_Cts)
  73. {
  74.     AP4_ADD_REFERENCE(m_DataStream);
  75. }
  76. /*----------------------------------------------------------------------
  77. |       AP4_Sample::~AP4_Sample
  78. +---------------------------------------------------------------------*/
  79. AP4_Sample::~AP4_Sample()
  80. {
  81.     AP4_RELEASE(m_DataStream);
  82. }
  83. /*----------------------------------------------------------------------
  84. |       AP4_Sample::operator=
  85. +---------------------------------------------------------------------*/
  86. AP4_Sample&
  87. AP4_Sample::operator=(const AP4_Sample& other)
  88. {
  89.     AP4_RELEASE(m_DataStream);
  90.     m_DataStream = other.m_DataStream;
  91.     AP4_ADD_REFERENCE(m_DataStream);
  92.     m_Offset           = other.m_Offset;
  93.     m_Size             = other.m_Size;
  94.     m_DescriptionIndex = other.m_DescriptionIndex;
  95.     m_Dts              = other.m_Dts;
  96.     m_Cts              = other.m_Cts;
  97.     return *this;
  98. }
  99. /*----------------------------------------------------------------------
  100. |       AP4_Sample::ReadData
  101. +---------------------------------------------------------------------*/
  102. AP4_Result
  103. AP4_Sample::ReadData(AP4_DataBuffer& data)
  104. {
  105.     return ReadData(data, m_Size);
  106. }
  107. /*----------------------------------------------------------------------
  108. |       AP4_Sample::ReadData
  109. +---------------------------------------------------------------------*/
  110. AP4_Result
  111. AP4_Sample::ReadData(AP4_DataBuffer& data, AP4_Size size, AP4_Offset offset)
  112. {
  113.     // check that we have a stream
  114.     if (m_DataStream == NULL) return AP4_FAILURE;
  115.     // shortcut
  116.     if (size == 0) return AP4_SUCCESS;
  117.     // check the size
  118.     if (m_Size < size+offset) return AP4_FAILURE;
  119.     // set the buffer size
  120.     AP4_Result result = data.SetDataSize(size);
  121.     if (AP4_FAILED(result)) return result;
  122.     // get the data from the stream
  123.     m_DataStream->Seek(m_Offset+offset);
  124. return m_DataStream->Read(data.UseData(), size);
  125. }
  126. /*----------------------------------------------------------------------
  127. |       AP4_Sample::GetDataStream
  128. +---------------------------------------------------------------------*/
  129. AP4_ByteStream*
  130. AP4_Sample::GetDataStream()
  131. {
  132.     AP4_ADD_REFERENCE(m_DataStream);
  133.     return m_DataStream;
  134. }
  135. /*----------------------------------------------------------------------
  136. |       AP4_Sample::SetDataStream
  137. +---------------------------------------------------------------------*/
  138. void
  139. AP4_Sample::SetDataStream(AP4_ByteStream& stream)
  140. {
  141.     AP4_RELEASE(m_DataStream);
  142.     m_DataStream = &stream;
  143.     AP4_ADD_REFERENCE(m_DataStream);
  144. }