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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - Byte Stream support
  4. |
  5. |    Copyright 2002 Gilles Boccon-Gibod
  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 "Ap4ByteStream.h"
  32. #include "Ap4Utils.h"
  33. #include "Ap4Debug.h"
  34. /*----------------------------------------------------------------------
  35. |       constants
  36. +---------------------------------------------------------------------*/
  37. const int AP4_BYTE_STREAM_COPY_BUFFER_SIZE = 4096;
  38. /*----------------------------------------------------------------------
  39. |       AP4_ByteStream::WriteString
  40. +---------------------------------------------------------------------*/
  41. AP4_Result
  42. AP4_ByteStream::WriteString(const char* buffer)
  43. {
  44.     AP4_Size string_length = static_cast<AP4_Size>(strlen(buffer));
  45.     // shortcut
  46.     if ((buffer == NULL) || (string_length == 0)) return AP4_SUCCESS;
  47.     // write the string
  48.     return Write((const void*)buffer, string_length);
  49. }
  50. /*----------------------------------------------------------------------
  51. |       AP4_ByteStream::WriteUI64
  52. +---------------------------------------------------------------------*/
  53. AP4_Result
  54. AP4_ByteStream::WriteUI64(AP4_UI64 value)
  55. {
  56.     unsigned char buffer[8];
  57.     // convert value to bytes
  58.     AP4_BytesFromUInt64BE(buffer, value);
  59.     // write bytes to the stream
  60.     return Write((void*)buffer, 8);
  61. }
  62. /*----------------------------------------------------------------------
  63. |       AP4_ByteStream::WriteUI32
  64. +---------------------------------------------------------------------*/
  65. AP4_Result
  66. AP4_ByteStream::WriteUI32(AP4_UI32 value)
  67. {
  68.     unsigned char buffer[4];
  69.     // convert value to bytes
  70.     AP4_BytesFromUInt32BE(buffer, value);
  71.     // write bytes to the stream
  72.     return Write((void*)buffer, 4);
  73. }
  74. /*----------------------------------------------------------------------
  75. |       AP4_ByteStream::WriteUI24
  76. +---------------------------------------------------------------------*/
  77. AP4_Result
  78. AP4_ByteStream::WriteUI24(AP4_UI32 value)
  79. {
  80.     unsigned char buffer[3];
  81.     // convert value to bytes
  82.     AP4_BytesFromUInt24BE(buffer, value);
  83.     // write bytes to the stream
  84.     return Write((void*)buffer, 3);
  85. }
  86. /*----------------------------------------------------------------------
  87. |       AP4_ByteStream::WriteUI16
  88. +---------------------------------------------------------------------*/
  89. AP4_Result
  90. AP4_ByteStream::WriteUI16(AP4_UI16 value)
  91. {
  92.     unsigned char buffer[2];
  93.     // convert value to bytes
  94.     AP4_BytesFromUInt16BE(buffer, value);
  95.     // write bytes to the stream
  96.     return Write((void*)buffer, 2);
  97. }
  98. /*----------------------------------------------------------------------
  99. |       AP4_ByteStream::WriteUI08
  100. +---------------------------------------------------------------------*/
  101. AP4_Result
  102. AP4_ByteStream::WriteUI08(AP4_UI08 value)
  103. {
  104.     return Write((void*)&value, 1);
  105. }
  106. /*----------------------------------------------------------------------
  107. |       AP4_ByteStream::ReadUI64
  108. +---------------------------------------------------------------------*/
  109. AP4_Result
  110. AP4_ByteStream::ReadUI64(AP4_UI64& value)
  111. {
  112.     unsigned char buffer[8];
  113.     // read bytes from the stream
  114.     AP4_Result result;
  115.     result = Read((void*)buffer, 8);
  116.     if (AP4_FAILED(result)) {
  117.         value = 0;
  118.         return result;
  119.     }
  120.     // convert bytes to value
  121.     value = AP4_BytesToUInt64BE(buffer);
  122.     
  123.     return AP4_SUCCESS;
  124. }
  125. /*----------------------------------------------------------------------
  126. |       AP4_ByteStream::ReadUI32
  127. +---------------------------------------------------------------------*/
  128. AP4_Result
  129. AP4_ByteStream::ReadUI32(AP4_UI32& value)
  130. {
  131.     unsigned char buffer[4];
  132.     // read bytes from the stream
  133.     AP4_Result result;
  134.     result = Read((void*)buffer, 4);
  135.     if (AP4_FAILED(result)) {
  136.         value = 0;
  137.         return result;
  138.     }
  139.     // convert bytes to value
  140.     value = AP4_BytesToUInt32BE(buffer);
  141.     
  142.     return AP4_SUCCESS;
  143. }
  144. /*----------------------------------------------------------------------
  145. |       AP4_ByteStream::ReadUI24
  146. +---------------------------------------------------------------------*/
  147. AP4_Result
  148. AP4_ByteStream::ReadUI24(AP4_UI32& value)
  149. {
  150.     unsigned char buffer[3];
  151.     // read bytes from the stream
  152.     AP4_Result result;
  153.     result = Read((void*)buffer, 3);
  154.     if (AP4_FAILED(result)) {
  155.         value = 0;
  156.         return result;
  157.     }
  158.     // convert bytes to value
  159.     value = AP4_BytesToUInt24BE(buffer);
  160.     
  161.     return AP4_SUCCESS;
  162. }
  163. /*----------------------------------------------------------------------
  164. |       AP4_ByteStream::ReadUI16
  165. +---------------------------------------------------------------------*/
  166. AP4_Result
  167. AP4_ByteStream::ReadUI16(AP4_UI16& value)
  168. {
  169.     unsigned char buffer[2];
  170.     // read bytes from the stream
  171.     AP4_Result result;
  172.     result = Read((void*)buffer, 2);
  173.     if (AP4_FAILED(result)) {
  174.         value = 0;
  175.         return result;
  176.     }
  177.     // convert bytes to value
  178.     value = AP4_BytesToUInt16BE(buffer);
  179.     
  180.     return AP4_SUCCESS;
  181. }
  182. /*----------------------------------------------------------------------
  183. |       AP4_ByteStream::ReadUI08
  184. +---------------------------------------------------------------------*/
  185. AP4_Result
  186. AP4_ByteStream::ReadUI08(AP4_UI08& value)
  187. {
  188.     unsigned char buffer[1];
  189.     // read bytes from the stream
  190.     AP4_Result result;
  191.     result = Read((void*)buffer, 1);
  192.     if (AP4_FAILED(result)) {        
  193.         value = 0;
  194.         return result;
  195.     }
  196.     // convert bytes to value
  197.     value = buffer[0];
  198.     
  199.     return AP4_SUCCESS;
  200. }
  201. /*----------------------------------------------------------------------
  202. |       AP4_ByteStream::ReadString
  203. +---------------------------------------------------------------------*/
  204. AP4_Result
  205. AP4_ByteStream::ReadString(char* buffer, AP4_Size size)
  206. {
  207.     if (buffer == NULL || size == 0) {
  208.         return AP4_ERROR_INVALID_PARAMETERS;
  209.     }
  210.     AP4_Size bytes_read = 0;
  211.     while (bytes_read < size-1) {      
  212.         AP4_Result result;
  213.         result = Read(&buffer[bytes_read], 1, NULL);
  214.         if (AP4_FAILED(result)) {
  215.             buffer[bytes_read] = '';
  216.             return result;
  217.         }
  218.         if (buffer[bytes_read] == '') {
  219.             // end of string
  220.             return AP4_SUCCESS;
  221.         }
  222.         bytes_read++;
  223.     }
  224.     // the string was not null terminated, terminate it
  225.     buffer[size-1] = '';
  226.     return AP4_SUCCESS;
  227. }
  228. /*----------------------------------------------------------------------
  229. |       AP4_ByteStream::CopyTo
  230. +---------------------------------------------------------------------*/
  231. AP4_Result
  232. AP4_ByteStream::CopyTo(AP4_ByteStream& stream, AP4_Size size)
  233. {
  234.     unsigned char buffer[AP4_BYTE_STREAM_COPY_BUFFER_SIZE];
  235.     while (size) {
  236.         AP4_Size bytes_read;
  237.         AP4_Size bytes_to_read;
  238.         AP4_Result result;
  239.         // decide how much to read
  240.         if (size >= sizeof(buffer)) {
  241.             bytes_to_read = sizeof(buffer);
  242.         } else {
  243.             bytes_to_read = size;
  244.         }
  245.         // read up to one buffer full
  246.         result = Read(buffer, bytes_to_read, &bytes_read);
  247.         if (AP4_FAILED(result)) return result;
  248.         // copy to destination
  249.         if (bytes_read != 0) {
  250.             result = stream.Write(buffer, bytes_read);
  251.             if (AP4_FAILED(result)) return result;
  252.         }
  253.         // update the size
  254.         size -= bytes_read;
  255.     }
  256.     return AP4_SUCCESS;
  257. }
  258. /*----------------------------------------------------------------------
  259. |       AP4_SubStream::AP4_SubStream
  260. +---------------------------------------------------------------------*/
  261. AP4_SubStream::AP4_SubStream(AP4_ByteStream& container, 
  262.                              AP4_Offset      offset, 
  263.                              AP4_Size        size) :
  264.     m_Container(container),
  265.     m_Offset(offset),
  266.     m_Size(size),
  267.     m_Position(0),
  268.     m_ReferenceCount(1)
  269. {
  270.     m_Container.AddReference();
  271. }
  272. /*----------------------------------------------------------------------
  273. |       AP4_SubStream::~AP4_SubStream
  274. +---------------------------------------------------------------------*/
  275. AP4_SubStream::~AP4_SubStream()
  276. {
  277.     m_Container.Release();
  278. }
  279. /*----------------------------------------------------------------------
  280. |       AP4_SubStream::Read
  281. +---------------------------------------------------------------------*/
  282. AP4_Result 
  283. AP4_SubStream::Read(void*     buffer, 
  284.                     AP4_Size  bytes_to_read, 
  285.                     AP4_Size* bytes_read)
  286. {
  287.     // default values
  288.     if (bytes_read) *bytes_read = 0;
  289.     // shortcut
  290.     if (bytes_to_read == 0) {
  291.         return AP4_SUCCESS;
  292.     }
  293.     // clamp to range
  294.     if (m_Position+bytes_to_read > m_Size) {
  295.         bytes_to_read = m_Size - m_Position;
  296.     }
  297.     // check for end of substream
  298.     if (bytes_to_read == 0) {
  299.         return AP4_ERROR_EOS;
  300.     }
  301.     // seek inside container
  302.     //AP4_Result result;
  303.     //result = m_Container.Seek(m_Offset+m_Position);
  304.     //if (AP4_FAILED(result)) {
  305.     //    return result;
  306.     //}
  307.     // read from the container
  308.     AP4_Size local_bytes_read;
  309.     AP4_Result result = m_Container.Read(buffer, bytes_to_read, &local_bytes_read);
  310.     if (bytes_read) *bytes_read = local_bytes_read;
  311.     if (AP4_SUCCEEDED(result)) {
  312.         m_Position += local_bytes_read;
  313.     }
  314.     return result;
  315. }
  316. /*----------------------------------------------------------------------
  317. |       AP4_SubStream::Write
  318. +---------------------------------------------------------------------*/
  319. AP4_Result 
  320. AP4_SubStream::Write(const void* buffer, 
  321.                      AP4_Size    bytes_to_write, 
  322.                      AP4_Size*   bytes_written)
  323. {
  324.     // default values
  325.     if (bytes_written) *bytes_written = 0;
  326.     // shortcut
  327.     if (bytes_to_write == 0) {
  328.         return AP4_SUCCESS;
  329.     }
  330.     // clamp to range
  331.     if (m_Position+bytes_to_write > m_Size) {
  332.         bytes_to_write = m_Size - m_Position;
  333.     }
  334.     // check for en of substream
  335.     if (bytes_to_write == 0) {
  336.         return AP4_ERROR_EOS;
  337.     }
  338.     // seek inside container
  339.     //AP4_Result result;
  340.     //result = m_Container.Seek(m_Offset+m_Position);
  341.     //if (AP4_FAILED(result)) return result;
  342.     // write to container
  343.     AP4_Size local_bytes_written;
  344.     AP4_Result result = m_Container.Write(buffer, bytes_to_write, &local_bytes_written);
  345.     if (bytes_written) *bytes_written = local_bytes_written;
  346.     if (AP4_SUCCEEDED(result)) {
  347.         m_Position += local_bytes_written;
  348.     }
  349.     return result;
  350. }
  351. /*----------------------------------------------------------------------
  352. |       AP4_SubStream::Seek
  353. +---------------------------------------------------------------------*/
  354. AP4_Result 
  355. AP4_SubStream::Seek(AP4_Offset offset)
  356. {
  357.     if (offset > m_Size) return AP4_FAILURE;
  358.     AP4_Result result;
  359.     result = m_Container.Seek(m_Offset+offset);
  360.     if (AP4_SUCCEEDED(result)) {
  361.         m_Position = offset;
  362.     }
  363.     return result;
  364. }
  365. /*----------------------------------------------------------------------
  366. |       AP4_SubStream::AddReference
  367. +---------------------------------------------------------------------*/
  368. void
  369. AP4_SubStream::AddReference()
  370. {
  371.     m_ReferenceCount++;
  372. }
  373. /*----------------------------------------------------------------------
  374. |       AP4_SubStream::Release
  375. +---------------------------------------------------------------------*/
  376. void
  377. AP4_SubStream::Release()
  378. {
  379.     if (--m_ReferenceCount == 0) {
  380.         delete this;
  381.     }
  382. }
  383. /*----------------------------------------------------------------------
  384. |       AP4_MemoryByteStream::AP4_MemoryByteStream
  385. +---------------------------------------------------------------------*/
  386. AP4_MemoryByteStream::AP4_MemoryByteStream(AP4_Size size) :
  387.     m_BufferIsLocal(true),
  388.     m_Size(size),
  389.     m_Position(0),
  390.     m_ReferenceCount(1)
  391. {
  392.     m_Buffer = new AP4_UI08[size];
  393. }
  394. /*----------------------------------------------------------------------
  395. |       AP4_MemoryByteStream::AP4_MemoryByteStream
  396. +---------------------------------------------------------------------*/
  397. AP4_MemoryByteStream::AP4_MemoryByteStream(AP4_UI08* buffer, AP4_Size size) :
  398.     m_BufferIsLocal(false),
  399.     m_Buffer(buffer),
  400.     m_Size(size),
  401.     m_Position(0),
  402.     m_ReferenceCount(1)
  403. {}
  404. /*----------------------------------------------------------------------
  405. |       AP4_MemoryByteStream::~AP4_MemoryByteStream
  406. +---------------------------------------------------------------------*/
  407. AP4_MemoryByteStream::~AP4_MemoryByteStream()
  408. {
  409.     if (m_BufferIsLocal) delete[] m_Buffer;
  410. }
  411. /*----------------------------------------------------------------------
  412. |       AP4_MemoryByteStream::Read
  413. +---------------------------------------------------------------------*/
  414. AP4_Result 
  415. AP4_MemoryByteStream::Read(void*     buffer, 
  416.                            AP4_Size  bytes_to_read, 
  417.                            AP4_Size* bytes_read)
  418. {
  419.     // default values
  420.     if (bytes_read) *bytes_read = 0;
  421.     // shortcut
  422.     if (bytes_to_read == 0) {
  423.         return AP4_SUCCESS;
  424.     }
  425.     // clamp to range
  426.     if (m_Position+bytes_to_read > m_Size) {
  427.         bytes_to_read = m_Size - m_Position;
  428.     }
  429.     // check for end of stream
  430.     if (bytes_to_read == 0) {
  431.         return AP4_ERROR_EOS;
  432.     }
  433.     // read from the memory
  434.     memcpy(buffer, &m_Buffer[m_Position], bytes_to_read);
  435.     m_Position += bytes_to_read;
  436.     if (bytes_read) *bytes_read = bytes_to_read;
  437.     return AP4_SUCCESS;
  438. }
  439. /*----------------------------------------------------------------------
  440. |       AP4_MemoryByteStream::Write
  441. +---------------------------------------------------------------------*/
  442. AP4_Result 
  443. AP4_MemoryByteStream::Write(const void* buffer, 
  444.                             AP4_Size    bytes_to_write, 
  445.                             AP4_Size*   bytes_written)
  446. {
  447.     // default values
  448.     if (bytes_written) *bytes_written = 0;
  449.     // shortcut
  450.     if (bytes_to_write == 0) {
  451.         return AP4_SUCCESS;
  452.     }
  453.     // clamp to range
  454.     if (m_Position+bytes_to_write > m_Size) {
  455.         bytes_to_write = m_Size - m_Position;
  456.     }
  457.     // check for en of stream
  458.     if (bytes_to_write == 0) {
  459.         return AP4_ERROR_EOS;
  460.     }
  461.     // write to memory
  462.     memcpy(&m_Buffer[m_Position], buffer, bytes_to_write);
  463.     m_Position += bytes_to_write;
  464.     if (bytes_written) *bytes_written = bytes_to_write;
  465.     return AP4_SUCCESS;
  466. }
  467. /*----------------------------------------------------------------------
  468. |       AP4_MemoryByteStream::Seek
  469. +---------------------------------------------------------------------*/
  470. AP4_Result 
  471. AP4_MemoryByteStream::Seek(AP4_Offset offset)
  472. {
  473.     if (offset > m_Size) return AP4_FAILURE;
  474.     m_Position = offset;
  475.     return AP4_SUCCESS;
  476. }
  477. /*----------------------------------------------------------------------
  478. |       AP4_MemoryByteStream::AddReference
  479. +---------------------------------------------------------------------*/
  480. void
  481. AP4_MemoryByteStream::AddReference()
  482. {
  483.     m_ReferenceCount++;
  484. }
  485. /*----------------------------------------------------------------------
  486. |       AP4_MemoryByteStream::Release
  487. +---------------------------------------------------------------------*/
  488. void
  489. AP4_MemoryByteStream::Release()
  490. {
  491.     if (--m_ReferenceCount == 0) {
  492.         delete this;
  493.     }
  494. }