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

多媒体编程

开发平台:

Visual C++

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