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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - Movie
  4. |
  5. |    Copyright 2002-2005 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 "Ap4File.h"
  32. #include "Ap4Atom.h"
  33. #include "Ap4TrakAtom.h"
  34. #include "Ap4MoovAtom.h"
  35. #include "Ap4MvhdAtom.h"
  36. #include "Ap4AtomFactory.h"
  37. #include "Ap4Movie.h"
  38. /*----------------------------------------------------------------------
  39. |       AP4_TrackFinderById
  40. +---------------------------------------------------------------------*/
  41. class AP4_TrackFinderById : public AP4_List<AP4_Track>::Item::Finder
  42. {
  43. public:
  44.     AP4_TrackFinderById(AP4_UI32 track_id) : m_TrackId(track_id) {}
  45.     AP4_Result Test(AP4_Track* track) const {
  46.         return track->GetId() == m_TrackId ? AP4_SUCCESS : AP4_FAILURE;
  47.     }
  48. private:
  49.     AP4_UI32 m_TrackId;
  50. };
  51. /*----------------------------------------------------------------------
  52. |       AP4_TrackFinderByType
  53. +---------------------------------------------------------------------*/
  54. class AP4_TrackFinderByType : public AP4_List<AP4_Track>::Item::Finder
  55. {
  56. public:
  57.     AP4_TrackFinderByType(AP4_Track::Type type, AP4_Ordinal index = 0) : 
  58.       m_Type(type), m_Index(index) {}
  59.     AP4_Result Test(AP4_Track* track) const {
  60.         if (track->GetType() == m_Type && m_Index-- == 0) {
  61.             return AP4_SUCCESS;
  62.         } else {
  63.             return AP4_FAILURE;
  64.         }
  65.     }
  66. private:
  67.     AP4_Track::Type     m_Type;
  68.     mutable AP4_Ordinal m_Index;
  69. };
  70. /*----------------------------------------------------------------------
  71. |       AP4_Movie::AP4_Movie
  72. +---------------------------------------------------------------------*/
  73. AP4_Movie::AP4_Movie(AP4_UI32 time_scale)
  74. {
  75.     m_MoovAtom = new AP4_MoovAtom();
  76.     m_MvhdAtom = new AP4_MvhdAtom(0, 0, 
  77.                                   time_scale, 
  78.                                   0, 
  79.                                   0x00010000,
  80.                                   0x0100);
  81.     m_MoovAtom->AddChild(m_MvhdAtom);
  82. }
  83. /*----------------------------------------------------------------------
  84. |       AP4_Movie::AP4_Moovie
  85. +---------------------------------------------------------------------*/
  86. AP4_Movie::AP4_Movie(AP4_MoovAtom* moov, AP4_ByteStream& mdat) :
  87.     m_MoovAtom(moov)
  88. {
  89.     // ignore null atoms
  90.     if (moov == NULL) return;
  91.     // get the time scale
  92.     AP4_UI32 time_scale;
  93.     m_MvhdAtom = dynamic_cast<AP4_MvhdAtom*>(moov->GetChild(AP4_ATOM_TYPE_MVHD));
  94.     if (m_MvhdAtom) {
  95.         time_scale = m_MvhdAtom->GetTimeScale();
  96.     } else {
  97.         time_scale = 0;
  98.     }
  99.     // get all tracks
  100.     AP4_List<AP4_TrakAtom>* trak_atoms;
  101.     trak_atoms = &moov->GetTrakAtoms();
  102.     AP4_List<AP4_TrakAtom>::Item* item = trak_atoms->FirstItem();
  103.     while (item) {
  104.         AP4_Track* track = new AP4_Track(*item->GetData(), 
  105.                                          mdat,
  106.                                          time_scale);
  107.         m_Tracks.Add(track);
  108.         item = item->GetNext();
  109.     }
  110. }
  111.     
  112. /*----------------------------------------------------------------------
  113. |       AP4_Movie::~AP4_Movie
  114. +---------------------------------------------------------------------*/
  115. AP4_Movie::~AP4_Movie()
  116. {
  117.     m_Tracks.DeleteReferences();
  118.     delete m_MoovAtom;
  119. }
  120. /*----------------------------------------------------------------------
  121. |       AP4_Movie::Inspect
  122. +---------------------------------------------------------------------*/
  123. AP4_Result
  124. AP4_Movie::Inspect(AP4_AtomInspector& inspector)
  125. {
  126.     // dump the moov atom
  127.     return m_MoovAtom->Inspect(inspector);
  128. }
  129. /*----------------------------------------------------------------------
  130. |       AP4_Movie::GetTrack
  131. +---------------------------------------------------------------------*/
  132. AP4_Track*
  133. AP4_Movie::GetTrack(AP4_UI32 track_id)
  134. {
  135.     AP4_Track* track = NULL;
  136.     if (AP4_SUCCEEDED(m_Tracks.Find(AP4_TrackFinderById(track_id), track))) {
  137.         return track;
  138.     } else {
  139.         return NULL;
  140.     }
  141. }
  142. /*----------------------------------------------------------------------
  143. |       AP4_Movie::GetTrack
  144. +---------------------------------------------------------------------*/
  145. AP4_Track*
  146. AP4_Movie::GetTrack(AP4_Track::Type track_type, AP4_Ordinal index)
  147. {
  148.     AP4_Track* track = NULL;
  149.     if (AP4_SUCCEEDED(m_Tracks.Find(AP4_TrackFinderByType(track_type, index), track))) {
  150.         return track;
  151.     } else {
  152.         return NULL;
  153.     }
  154. }
  155. /*----------------------------------------------------------------------
  156. |       AP4_Movie::AddTrack
  157. +---------------------------------------------------------------------*/
  158. AP4_Result
  159. AP4_Movie::AddTrack(AP4_Track* track)
  160. {
  161.     // assign an ID to the track unless it already has one
  162.     if (track->GetId() == 0) {
  163.         track->SetId(m_Tracks.ItemCount()+1);
  164.     }
  165.     // if we don't have a time scale, use the one from the track
  166.     if (m_MvhdAtom->GetTimeScale() == 0) {
  167.         m_MvhdAtom->SetTimeScale(track->GetMediaTimeScale());
  168.     }
  169.     // adjust the parent time scale of the track
  170.     track->SetMovieTimeScale(m_MvhdAtom->GetTimeScale());
  171.     // update the movie duration
  172.     if (m_MvhdAtom->GetDuration() < track->GetDuration()) {
  173.         m_MvhdAtom->SetDuration(track->GetDuration());
  174.     }
  175.     
  176.     // attach the track as a child
  177.     m_MoovAtom->AddChild(track->GetTrakAtom());
  178.     m_Tracks.Add(track);
  179.     return AP4_SUCCESS;
  180. }
  181. /*----------------------------------------------------------------------
  182. |       AP4_Movie::GetTimeScale
  183. +---------------------------------------------------------------------*/
  184. AP4_UI32
  185. AP4_Movie::GetTimeScale()
  186. {
  187.     if (m_MvhdAtom) {
  188.         return m_MvhdAtom->GetTimeScale();
  189.     } else {
  190.         return 0;
  191.     }
  192. }
  193. /*----------------------------------------------------------------------
  194. |       AP4_Movie::GetDuration
  195. +---------------------------------------------------------------------*/
  196. AP4_UI32
  197. AP4_Movie::GetDuration()
  198. {
  199.     if (m_MvhdAtom) {
  200.         return m_MvhdAtom->GetDuration();
  201.     } else {
  202.         return 0;
  203.     }
  204. }
  205. /*----------------------------------------------------------------------
  206. |       AP4_Movie::GetDurationMs
  207. +---------------------------------------------------------------------*/
  208. AP4_UI32
  209. AP4_Movie::GetDurationMs()
  210. {
  211.     if (m_MvhdAtom) {
  212.         return m_MvhdAtom->GetDurationMs();
  213.     } else {
  214.         return 0;
  215.     }
  216. }