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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - Data Buffer
  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 "Ap4DataBuffer.h"
  32. /*----------------------------------------------------------------------
  33. |       AP4_DataBuffer::AP4_DataBuffer
  34. +---------------------------------------------------------------------*/
  35. AP4_DataBuffer::AP4_DataBuffer() :
  36.     m_BufferIsLocal(true),
  37.     m_Buffer(NULL),
  38.     m_BufferSize(0),
  39.     m_DataSize(0)
  40. {
  41. }
  42. /*----------------------------------------------------------------------
  43. |       AP4_DataBuffer::AP4_DataBuffer
  44. +---------------------------------------------------------------------*/
  45. AP4_DataBuffer::AP4_DataBuffer(AP4_Size buffer_size) :
  46.     m_BufferIsLocal(true),
  47.     m_Buffer(NULL),
  48.     m_BufferSize(buffer_size),
  49.     m_DataSize(0)
  50. {
  51.     m_Buffer = new AP4_Byte[buffer_size];
  52. }
  53. /*----------------------------------------------------------------------
  54. |       AP4_DataBuffer::AP4_DataBuffer
  55. +---------------------------------------------------------------------*/
  56. AP4_DataBuffer::AP4_DataBuffer(const AP4_DataBuffer& other) :
  57.     m_BufferIsLocal(true),
  58.     m_Buffer(NULL),
  59.     m_BufferSize(other.m_DataSize),
  60.     m_DataSize(other.m_DataSize)
  61. {
  62.     m_Buffer = new AP4_Byte[m_BufferSize];
  63.     memcpy(m_Buffer, other.m_Buffer, m_BufferSize);
  64. }
  65. /*----------------------------------------------------------------------
  66. |       AP4_DataBuffer::~AP4_DataBuffer
  67. +---------------------------------------------------------------------*/
  68. AP4_DataBuffer::~AP4_DataBuffer()
  69. {
  70.     if (m_BufferIsLocal) {
  71.         delete[] m_Buffer;
  72.     }
  73. }
  74. /*----------------------------------------------------------------------
  75. |       AP4_DataBuffer::SetBuffer
  76. +---------------------------------------------------------------------*/
  77. AP4_Result
  78. AP4_DataBuffer::SetBuffer(AP4_Byte* buffer, AP4_Size buffer_size)
  79. {
  80.     if (m_BufferIsLocal) {
  81.         // destroy the local buffer
  82.         delete[] m_Buffer;
  83.     }
  84.     // we're now using an external buffer
  85.     m_BufferIsLocal = false;
  86.     m_Buffer = buffer;
  87.     m_BufferSize = buffer_size;
  88.     return AP4_SUCCESS;
  89. }
  90. /*----------------------------------------------------------------------
  91. |       AP4_DataBuffer::SetBufferSize
  92. +---------------------------------------------------------------------*/
  93. AP4_Result
  94. AP4_DataBuffer::SetBufferSize(AP4_Size buffer_size)
  95. {
  96.     if (m_BufferIsLocal) {
  97.         return ReallocateBuffer(buffer_size);
  98.     } else {
  99.         return AP4_FAILURE; // you cannot change the
  100.                             // buffer management mode
  101.     }
  102. }
  103. /*----------------------------------------------------------------------
  104. |       AP4_DataBuffer::SetDataSize
  105. +---------------------------------------------------------------------*/
  106. AP4_Result
  107. AP4_DataBuffer::SetDataSize(AP4_Size size)
  108. {
  109.     if (size > m_BufferSize) {
  110.         if (m_BufferIsLocal) {
  111.             AP4_Result result = ReallocateBuffer(size);
  112.             if (AP4_FAILED(result)) return result;
  113.         } else { 
  114.             return AP4_FAILURE;
  115.         }
  116.     }
  117.     m_DataSize = size;
  118.     return AP4_SUCCESS;
  119. }
  120. /*----------------------------------------------------------------------
  121. |       AP4_DataBuffer::SetData
  122. +---------------------------------------------------------------------*/
  123. AP4_Result
  124. AP4_DataBuffer::SetData(AP4_Byte* data, AP4_Size size)
  125. {
  126.     if (size > m_BufferSize) {
  127.         if (m_BufferIsLocal) {
  128.             AP4_Result result = ReallocateBuffer(size);
  129.             if (AP4_FAILED(result)) return result;
  130.         } else {
  131.             return AP4_FAILURE;
  132.         }
  133.     }
  134.     memcpy(m_Buffer, data, size);
  135.     m_DataSize = size;
  136.     return AP4_SUCCESS;
  137. }
  138. /*----------------------------------------------------------------------
  139. |       AP4_DataBuffer::ReallocateBuffer
  140. +---------------------------------------------------------------------*/
  141. AP4_Result
  142. AP4_DataBuffer::ReallocateBuffer(AP4_Size size)
  143. {
  144.     // check that the existing data fits
  145.     if (m_DataSize > size) return AP4_FAILURE;
  146.     // allocate a new buffer
  147.     AP4_Byte* new_buffer = new AP4_Byte[size];
  148.     // copy the contents of the previous buffer ,is any
  149. if (m_Buffer && m_DataSize) {
  150. memcpy(new_buffer, m_Buffer, m_DataSize);
  151. }
  152.     // destroy the previous buffer
  153.     delete[] m_Buffer;
  154.     // use the new buffer
  155.     m_Buffer = new_buffer;
  156.     m_BufferSize = size;
  157.     return AP4_SUCCESS;
  158. }