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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - Descriptors
  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 "Ap4.h"
  32. #include "Ap4Descriptor.h"
  33. #include "Ap4Utils.h"
  34. /*----------------------------------------------------------------------
  35. |       AP4_Descriptor::MinHeaderSize
  36. +---------------------------------------------------------------------*/
  37. AP4_Size
  38. AP4_Descriptor::MinHeaderSize(AP4_Size payload_size)
  39. {
  40.     // compute how many bytes are needed to encode the payload size
  41.     // plus tag
  42.     return 2+(payload_size/128);
  43. }
  44. /*----------------------------------------------------------------------
  45. |       AP4_Descriptor::AP4_Descriptor
  46. +---------------------------------------------------------------------*/
  47. AP4_Descriptor::AP4_Descriptor(Tag      tag, 
  48.                                AP4_Size header_size, 
  49.                                AP4_Size payload_size) :
  50.     m_Tag(tag),
  51.     m_HeaderSize(header_size),
  52.     m_PayloadSize(payload_size)
  53. {
  54.     AP4_ASSERT(header_size >= 1+1);
  55.     AP4_ASSERT(header_size <= 1+4);
  56. }
  57. /*----------------------------------------------------------------------
  58. |       AP4_Descriptor::Write
  59. +---------------------------------------------------------------------*/
  60. AP4_Result
  61. AP4_Descriptor::Write(AP4_ByteStream& stream)
  62. {
  63.     AP4_Result result;
  64.     // write the tag
  65.     result = stream.WriteUI08(m_Tag);
  66.     if (AP4_FAILED(result)) return result;
  67.     // write the size
  68.     AP4_ASSERT(m_HeaderSize-1 <= 8);
  69.     AP4_ASSERT(m_HeaderSize >= 2);
  70.     unsigned int size = m_PayloadSize;
  71.     unsigned char bytes[8];
  72.     // last bytes of the encoded size
  73.     bytes[m_HeaderSize-2] = size&0x7F;
  74.     // leading bytes of the encoded size
  75.     for (int i=m_HeaderSize-3; i>=0; i--) {
  76.         // move to the next 7 bits
  77.         size >>= 7;
  78.         // output a byte with a top bit marker
  79.         bytes[i] = (size&0x7F) | 0x80;
  80.     }
  81.     result = stream.Write(bytes, m_HeaderSize-1);
  82.     if (AP4_FAILED(result)) return result;
  83.     // write the fields
  84.     WriteFields(stream);
  85.     return result;
  86. }
  87. /*----------------------------------------------------------------------
  88. |       AP4_Descriptor::Inspect
  89. +---------------------------------------------------------------------*/
  90. AP4_Result
  91. AP4_Descriptor::Inspect(AP4_AtomInspector& inspector)
  92. {
  93.     char name[6];
  94.     AP4_StringFormat(name, sizeof(name), "#[%02x]", m_Tag);
  95.     char info[64];
  96.     AP4_StringFormat(info, sizeof(info), "size=%ld+%ld",
  97.         GetHeaderSize(),
  98.         m_PayloadSize);
  99.     inspector.StartElement(name, info);
  100.     inspector.EndElement();
  101.     return AP4_SUCCESS;
  102. }