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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - mdhd Atoms 
  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 "Ap4MdhdAtom.h"
  33. #include "Ap4AtomFactory.h"
  34. #include "Ap4Utils.h"
  35. /*----------------------------------------------------------------------
  36. |       AP4_MdhdAtom::AP4_MdhdAtom
  37. +---------------------------------------------------------------------*/
  38. AP4_MdhdAtom::AP4_MdhdAtom(AP4_UI32    creation_time,
  39.                            AP4_UI32    modification_time,
  40.                            AP4_UI32    time_scale,
  41.                            AP4_UI32    duration,
  42.                            const char* language) :
  43.     AP4_Atom(AP4_ATOM_TYPE_MDHD, 20+AP4_FULL_ATOM_HEADER_SIZE, true),
  44.     m_CreationTime(creation_time),
  45.     m_ModificationTime(modification_time),
  46.     m_TimeScale(time_scale),
  47.     m_Duration(duration)
  48. {
  49.     m_Language[0] = language[0];
  50.     m_Language[1] = language[1];
  51.     m_Language[2] = language[2];
  52. }
  53. /*----------------------------------------------------------------------
  54. |       AP4_MdhdAtom::AP4_MdhdAtom
  55. +---------------------------------------------------------------------*/
  56. AP4_MdhdAtom::AP4_MdhdAtom(AP4_Size size, AP4_ByteStream& stream) :
  57.     AP4_Atom(AP4_ATOM_TYPE_MDHD, size, true, stream),
  58.     m_CreationTime(0),
  59.     m_ModificationTime(0),
  60.     m_TimeScale(0),
  61.     m_Duration(0)
  62. {
  63.     m_Language[0] = 0;
  64.     m_Language[1] = 0;
  65.     m_Language[2] = 0;
  66.     if (m_Version == 0) {
  67.         // we only deal with version 0 for now
  68.         stream.ReadUI32(m_CreationTime);
  69.         stream.ReadUI32(m_ModificationTime);
  70.         stream.ReadUI32(m_TimeScale);
  71.         stream.ReadUI32(m_Duration);
  72.     } else {
  73.         // stream.ReadUI64(m_CreationTime);
  74.         stream.Read((void*)m_Reserved1, sizeof(m_Reserved1));
  75.         // stream.ReadUI64(m_ModificationTime);
  76.         stream.Read((void*)m_Reserved2, sizeof(m_Reserved2));
  77.         stream.ReadUI32(m_TimeScale);
  78.         //stream.ReadUI64(m_Duration);
  79.         stream.Read((void*)m_Reserved3, sizeof(m_Reserved3));
  80.     }
  81.     
  82.     unsigned char lang[2];
  83.     stream.Read(lang, 2, NULL);
  84.     char l0 = ((lang[0]>>2)&0x1F);
  85.     char l1 = (((lang[0]&0x3)<<3) | ((lang[1]>>5)&0x7));
  86.     char l2 = ((lang[1]&0x1F));
  87.     if (l0) {
  88.         m_Language[0] = l0+0x60;
  89.     }
  90.     if (l1) {
  91.         m_Language[1] = l1+0x60;
  92.     }
  93.     if (l2) {
  94.         m_Language[2] = l2+0x60;
  95.     }
  96. }
  97. /*----------------------------------------------------------------------
  98. |       AP4_MdhdAtom::WriteFields
  99. +---------------------------------------------------------------------*/
  100. AP4_Result
  101. AP4_MdhdAtom::WriteFields(AP4_ByteStream& stream)
  102. {
  103.     AP4_Result result;
  104.     if (m_Version == 0) {
  105.         // we only deal with version 0 for the moment
  106.         result = stream.WriteUI32(m_CreationTime);
  107.         if (AP4_FAILED(result)) return result;
  108.         result = stream.WriteUI32(m_ModificationTime);
  109.         if (AP4_FAILED(result)) return result;
  110.         result = stream.WriteUI32(m_TimeScale);
  111.         if (AP4_FAILED(result)) return result;
  112.         result = stream.WriteUI32(m_Duration);
  113.         if (AP4_FAILED(result)) return result;
  114.     } else {
  115.         result = stream.Write(m_Reserved1, sizeof(m_Reserved1));
  116.         if (AP4_FAILED(result)) return result;
  117.     }
  118.     // write the language
  119.     AP4_UI08 l0 = (m_Language[0]==0)?0:(m_Language[0]-0x60);
  120.     AP4_UI08 l1 = (m_Language[1]==0)?0:(m_Language[1]-0x60);
  121.     AP4_UI08 l2 = (m_Language[2]==0)?0:(m_Language[2]-0x60);
  122.     result = stream.WriteUI08(l0<<2 | l1>>3);
  123.     if (AP4_FAILED(result)) return result;
  124.     result = stream.WriteUI08(l1<<5 | l2);
  125.     if (AP4_FAILED(result)) return result;
  126.     // pre-defined
  127.     return stream.WriteUI16(0);
  128. }
  129. /*----------------------------------------------------------------------
  130. |       AP4_MdhdAtom::GetDurationMs
  131. +---------------------------------------------------------------------*/
  132. AP4_UI32
  133. AP4_MdhdAtom::GetDurationMs()
  134. {
  135.     return AP4_DurationMsFromUnits(m_Duration, m_TimeScale);
  136. }
  137. /*----------------------------------------------------------------------
  138. |       AP4_MdhdAtom::InspectFields
  139. +---------------------------------------------------------------------*/
  140. AP4_Result
  141. AP4_MdhdAtom::InspectFields(AP4_AtomInspector& inspector)
  142. {
  143.     inspector.AddField("timescale", m_TimeScale);
  144.     inspector.AddField("duration", m_Duration);
  145.     inspector.AddField("duration(ms)", GetDurationMs());
  146.     char language[4];
  147.     AP4_StringFormat(language, sizeof(language), 
  148.         "%c%c%c", 
  149.         m_Language[0] ? m_Language[0]:'-',
  150.         m_Language[1] ? m_Language[1]:'-',
  151.         m_Language[2] ? m_Language[2]:'-');
  152.     inspector.AddField("language", (const char*)language);
  153.     return AP4_SUCCESS;
  154. }