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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - mvhd 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 "Ap4MvhdAtom.h"
  33. #include "Ap4AtomFactory.h"
  34. #include "Ap4Utils.h"
  35. /*----------------------------------------------------------------------
  36. |       AP4_MvhdAtom::AP4_MvhdAtom
  37. +---------------------------------------------------------------------*/
  38. AP4_MvhdAtom::AP4_MvhdAtom(AP4_UI32 creation_time,
  39.                            AP4_UI32 modification_time,
  40.                            AP4_UI32 time_scale,
  41.                            AP4_UI32 duration,
  42.                            AP4_UI32 rate,
  43.                            AP4_UI16 volume) :
  44.     AP4_Atom(AP4_ATOM_TYPE_MVHD, 96+AP4_FULL_ATOM_HEADER_SIZE, true),
  45.     m_CreationTime(creation_time),
  46.     m_ModificationTime(modification_time),
  47.     m_TimeScale(time_scale),
  48.     m_Duration(duration),
  49.     m_Rate(rate),
  50.     m_Volume(volume),
  51.     m_NextTrackId(0xFFFFFFFF)
  52. {
  53.     m_Matrix[0] = 0x00010000;
  54.     m_Matrix[1] = 0;
  55.     m_Matrix[2] = 0;
  56.     m_Matrix[3] = 0;
  57.     m_Matrix[4] = 0x00010000;
  58.     m_Matrix[5] = 0;
  59.     m_Matrix[6] = 0;
  60.     m_Matrix[7] = 0;
  61.     m_Matrix[8] = 0x40000000;
  62.     memset(m_Reserved1, 0, sizeof(m_Reserved1));
  63.     memset(m_Reserved2, 0, sizeof(m_Reserved2));
  64.     memset(m_Predefined, 0, sizeof(m_Predefined));
  65. }
  66. /*----------------------------------------------------------------------
  67. |       AP4_MvhdAtom::AP4_MvhdAtom
  68. +---------------------------------------------------------------------*/
  69. AP4_MvhdAtom::AP4_MvhdAtom(AP4_Size size, AP4_ByteStream& stream) :
  70.     AP4_Atom(AP4_ATOM_TYPE_MVHD, size, true, stream)
  71. {
  72.     if (m_Version == 0) {
  73.         // we only deal with version 0 for now
  74.         stream.ReadUI32(m_CreationTime);
  75.         stream.ReadUI32(m_ModificationTime);
  76.         stream.ReadUI32(m_TimeScale);
  77.         stream.ReadUI32(m_Duration);
  78.     } else {
  79.         stream.Read(m_DataVersion1, sizeof(m_DataVersion1));
  80.     }
  81.     stream.ReadUI32(m_Rate);
  82.     stream.ReadUI16(m_Volume);
  83.     stream.Read(m_Reserved1, sizeof(m_Reserved1));
  84.     stream.Read(m_Reserved2, sizeof(m_Reserved2));
  85.     for (int i=0; i<9; i++) {
  86.         stream.ReadUI32(m_Matrix[i]);
  87.     }
  88.     stream.Read(m_Predefined, sizeof(m_Predefined));
  89.     stream.ReadUI32(m_NextTrackId);
  90. }
  91. /*----------------------------------------------------------------------
  92. |       AP4_MvhdAtom::WriteFields
  93. +---------------------------------------------------------------------*/
  94. AP4_Result
  95. AP4_MvhdAtom::WriteFields(AP4_ByteStream& stream)
  96. {
  97.     AP4_Result result;
  98.     
  99.     if (m_Version == 0) {
  100.         // we only deal with version 0 for now
  101.         result = stream.WriteUI32(m_CreationTime);
  102.         if (AP4_FAILED(result)) return result;
  103.         result = stream.WriteUI32(m_ModificationTime);
  104.         if (AP4_FAILED(result)) return result;
  105.         result = stream.WriteUI32(m_TimeScale);
  106.         if (AP4_FAILED(result)) return result;
  107.         result = stream.WriteUI32(m_Duration);
  108.     } else {
  109.         stream.Write(m_DataVersion1, sizeof(m_DataVersion1));
  110.     }
  111.     // rate & volume
  112.     result = stream.WriteUI32(m_Rate);
  113.     if (AP4_FAILED(result)) return result;
  114.     result = stream.WriteUI16(m_Volume);
  115.     if (AP4_FAILED(result)) return result;
  116.     // reserved
  117.     result = stream.Write(m_Reserved1, sizeof(m_Reserved1));
  118.     if (AP4_FAILED(result)) return result;
  119.     result = stream.Write(m_Reserved2, sizeof(m_Reserved2));
  120.     if (AP4_FAILED(result)) return result;
  121.     // matrix
  122.     for (int i=0; i<9; i++) {
  123.         result = stream.WriteUI32(m_Matrix[i]);
  124.         if (AP4_FAILED(result)) return result;
  125.     }
  126.     // pre-defined
  127.     result = stream.Write(m_Predefined, sizeof(m_Predefined));
  128.     if (AP4_FAILED(result)) return result;
  129.     // next track id
  130.     return stream.WriteUI32(m_NextTrackId);
  131. }
  132. /*----------------------------------------------------------------------
  133. |       AP4_MvhdAtom::GetDurationMs
  134. +---------------------------------------------------------------------*/
  135. unsigned long
  136. AP4_MvhdAtom::GetDurationMs()
  137. {
  138.     if (m_TimeScale) {
  139.         return AP4_ConvertTime(m_Duration, m_TimeScale, 1000);
  140.     } else {
  141.         return 0;
  142.     }
  143. }
  144. /*----------------------------------------------------------------------
  145. |       AP4_MvhdAtom::InspectFields
  146. +---------------------------------------------------------------------*/
  147. AP4_Result
  148. AP4_MvhdAtom::InspectFields(AP4_AtomInspector& inspector)
  149. {
  150.     inspector.AddField("timescale", m_TimeScale);
  151.     inspector.AddField("duration", m_Duration);
  152.     inspector.AddField("duration(ms)", GetDurationMs());
  153.     return AP4_SUCCESS;
  154. }