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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - DecoderConfig 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 "Ap4DecoderConfigDescriptor.h"
  33. #include "Ap4DescriptorFactory.h"
  34. #include "Ap4Utils.h"
  35. /*----------------------------------------------------------------------
  36. |       AP4_DecoderConfigDescriptor::AP4_DecoderConfigDescriptor
  37. +---------------------------------------------------------------------*/
  38. AP4_DecoderConfigDescriptor::AP4_DecoderConfigDescriptor(
  39.     AP4_UI08 stream_type,
  40.     AP4_UI08 oti,
  41.     AP4_UI32 buffer_size,
  42.     AP4_UI32 max_bitrate,
  43.     AP4_UI32 avg_bitrate,
  44.     AP4_DecoderSpecificInfoDescriptor* dsi) :
  45.     AP4_Descriptor(AP4_DESCRIPTOR_TAG_DECODER_CONFIG, 2, 13),
  46.     m_StreamType(stream_type),
  47.     m_ObjectTypeIndication(oti),
  48.     m_BufferSize(buffer_size),
  49.     m_MaxBitrate(max_bitrate),
  50.     m_AverageBitrate(avg_bitrate)
  51. {
  52.     if (dsi) {
  53.         m_SubDescriptors.Add(dsi);
  54.         m_PayloadSize += dsi->GetSize();
  55.         m_HeaderSize = MinHeaderSize(m_PayloadSize);
  56.     }
  57. }
  58. /*----------------------------------------------------------------------
  59. |       AP4_DecoderConfigDescriptor::AP4_DecoderConfigDescriptor
  60. +---------------------------------------------------------------------*/
  61. AP4_DecoderConfigDescriptor::AP4_DecoderConfigDescriptor(
  62.     AP4_ByteStream& stream, AP4_Size header_size, AP4_Size payload_size) :
  63.     AP4_Descriptor(AP4_DESCRIPTOR_TAG_DECODER_CONFIG, 
  64.                    header_size, 
  65.                    payload_size)
  66. {
  67.     // record the start position
  68.     AP4_Offset start;
  69.     stream.Tell(start);
  70.     // read descriptor fields
  71.     stream.ReadUI08(m_ObjectTypeIndication);
  72.     unsigned char bits;
  73.     stream.ReadUI08(bits);
  74.     m_StreamType = (bits>>2)&0x3F;
  75.     m_UpStream   = bits&2 ? true:false; 
  76.     stream.ReadUI24(m_BufferSize);
  77.     stream.ReadUI32(m_MaxBitrate);
  78.     stream.ReadUI32(m_AverageBitrate);
  79.     // read other descriptors
  80.     AP4_SubStream* substream = new AP4_SubStream(stream, start+13, payload_size-13);
  81.     AP4_Descriptor* descriptor = NULL;
  82.     while (AP4_DescriptorFactory::CreateDescriptorFromStream(*substream, 
  83.                                                              descriptor) 
  84.            == AP4_SUCCESS) {
  85.         m_SubDescriptors.Add(descriptor);
  86.     }
  87.     substream->Release();
  88. }
  89. /*----------------------------------------------------------------------
  90. |       AP4_DecoderConfigDescriptor::~AP4_DecoderConfigDescriptor
  91. +---------------------------------------------------------------------*/
  92. AP4_DecoderConfigDescriptor::~AP4_DecoderConfigDescriptor()
  93. {
  94.     m_SubDescriptors.DeleteReferences();
  95. }
  96. /*----------------------------------------------------------------------
  97. |       AP4_DecoderConfigDescriptor::WriteFields
  98. +---------------------------------------------------------------------*/
  99. AP4_Result
  100. AP4_DecoderConfigDescriptor::WriteFields(AP4_ByteStream& stream)
  101. {
  102.     stream.WriteUI08(m_ObjectTypeIndication);
  103.     AP4_UI08 bits = (m_StreamType<<2) | (m_UpStream? 2 : 0) | 1;
  104.     stream.WriteUI08(bits);
  105.     stream.WriteUI24(m_BufferSize);
  106.     stream.WriteUI32(m_MaxBitrate);
  107.     stream.WriteUI32(m_AverageBitrate);
  108.     m_SubDescriptors.Apply(AP4_DescriptorListWriter(stream));
  109.     return AP4_SUCCESS;
  110. }
  111. /*----------------------------------------------------------------------
  112. |       AP4_DecoderConfigDescriptor::Inspect
  113. +---------------------------------------------------------------------*/
  114. AP4_Result
  115. AP4_DecoderConfigDescriptor::Inspect(AP4_AtomInspector& inspector)
  116. {
  117.     char info[64];
  118.     AP4_StringFormat(info, sizeof(info), "size=%ld+%ld", 
  119.         GetHeaderSize(),
  120.         m_PayloadSize);
  121.     inspector.StartElement("#[DecoderConfig]", info);
  122.     inspector.AddField("stream_type", m_StreamType);
  123.     inspector.AddField("object_type", m_ObjectTypeIndication);
  124.     inspector.AddField("up_stream", m_UpStream);
  125.     inspector.AddField("buffer_size", m_BufferSize);
  126.     inspector.AddField("max_bitrate", m_MaxBitrate);
  127.     inspector.AddField("avg_bitrate", m_AverageBitrate);
  128.     // inspect children
  129.     m_SubDescriptors.Apply(AP4_DescriptorListInspector(inspector));
  130.     inspector.EndElement();
  131.     return AP4_SUCCESS;
  132. }
  133. /*----------------------------------------------------------------------
  134. |       AP4_DecoderConfigDescriptor::GetDecoderSpecificInfoDescriptor
  135. +---------------------------------------------------------------------*/
  136. const AP4_DecoderSpecificInfoDescriptor*
  137. AP4_DecoderConfigDescriptor::GetDecoderSpecificInfoDescriptor() const
  138. {
  139.     // find the decoder specific info
  140.     AP4_Descriptor* descriptor = NULL;
  141.     AP4_Result result = 
  142.         m_SubDescriptors.Find(AP4_DescriptorFinder(AP4_DESCRIPTOR_TAG_DECODER_SPECIFIC_INFO),
  143.                               descriptor);
  144.     
  145.     // return it
  146.     if (AP4_SUCCEEDED(result)) {
  147.         return dynamic_cast<AP4_DecoderSpecificInfoDescriptor*>(descriptor);
  148.     } else {
  149.         return NULL;
  150.     }
  151. }