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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - trak 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 "Ap4TrakAtom.h"
  33. #include "Ap4MdhdAtom.h"
  34. #include "Ap4VmhdAtom.h"
  35. #include "Ap4SmhdAtom.h"
  36. #include "Ap4HmhdAtom.h"
  37. #include "Ap4NmhdAtom.h"
  38. #include "Ap4DrefAtom.h"
  39. #include "Ap4UrlAtom.h"
  40. #include "Ap4StcoAtom.h"
  41. #include "Ap4Co64Atom.h"
  42. #include "Ap4AtomFactory.h"
  43. #include "Ap4Utils.h"
  44. /*----------------------------------------------------------------------
  45. |       AP4_TrakAtom::AP4_TrakAtom
  46. +---------------------------------------------------------------------*/
  47. AP4_TrakAtom::AP4_TrakAtom(AP4_SampleTable* sample_table,
  48.                            AP4_Atom::Type   hdlr_type,
  49.                            const char*      hdlr_name, 
  50.                            AP4_UI32         track_id,
  51.                            AP4_UI32         creation_time,
  52.                            AP4_UI32         modification_time,
  53.                            AP4_UI32         track_duration,
  54.                            AP4_UI32         media_time_scale,
  55.                            AP4_UI32         media_duration,
  56.                            AP4_UI16         volume,
  57.                            const char*      language,
  58.                            AP4_UI32         width,
  59.                            AP4_UI32         height) :
  60.     AP4_ContainerAtom(AP4_ATOM_TYPE_TRAK)
  61. {
  62.     AP4_Result result;
  63.     // create a tkhd atom
  64.     m_TkhdAtom = new AP4_TkhdAtom(creation_time, 
  65.                                   modification_time, 
  66.                                   track_id,
  67.                                   track_duration, 
  68.                                   volume, 
  69.                                   width, 
  70.                                   height);
  71.     // create an edts
  72.     // create a mdia atom
  73.     AP4_ContainerAtom* mdia = new AP4_ContainerAtom(AP4_ATOM_TYPE_MDIA);
  74.     // create a hdlr atom for the mdia atom
  75.     m_HdlrAtom = new AP4_HdlrAtom(hdlr_type, hdlr_name);
  76.     // create a minf atom 
  77.     AP4_ContainerAtom* minf = new AP4_ContainerAtom(AP4_ATOM_TYPE_MINF);
  78.     // create a media header atom for minf (vmhd, smhd, hmhd or nmhd)
  79.     AP4_Atom* minf_header;
  80.     switch (hdlr_type) {
  81.         case AP4_HANDLER_TYPE_VIDE:
  82.             minf_header = new AP4_VmhdAtom(0, 0, 0, 0);
  83.             break;
  84.         case AP4_HANDLER_TYPE_SOUN:
  85.             minf_header = new AP4_SmhdAtom(0);
  86.             break;
  87.         default:
  88.             minf_header = new AP4_NmhdAtom();
  89.             break;
  90.     }
  91.     // create a dinf atom for minf
  92.     AP4_ContainerAtom* dinf = new AP4_ContainerAtom(AP4_ATOM_TYPE_DINF);
  93.     // create a url atom as a ref for dref
  94.     AP4_Atom* url = new AP4_UrlAtom(); // local ref
  95.     // create a dref atom for dinf
  96.     AP4_DrefAtom* dref = new AP4_DrefAtom(&url, 1);
  97.     // create a stbl atom for minf
  98.     AP4_ContainerAtom* stbl;
  99.     result = sample_table->GenerateStblAtom(stbl);
  100.     if (AP4_FAILED(result)) stbl = NULL;
  101.     
  102.     // populate the dinf atom
  103.     dinf->AddChild(dref);
  104.     // populate the minf atom
  105.     minf->AddChild(minf_header);
  106.     minf->AddChild(dinf);
  107.     if (stbl) minf->AddChild(stbl);
  108.     // create a mdhd atom for the mdia atom
  109.     AP4_MdhdAtom* mdhd = new AP4_MdhdAtom(creation_time,
  110.                                           modification_time,
  111.                                           media_time_scale,
  112.                                           media_duration,
  113.                                           language);
  114.     // populate the mdia atom
  115.     mdia->AddChild(mdhd);
  116.     mdia->AddChild(m_HdlrAtom);
  117.     mdia->AddChild(minf);
  118.     // attach the children
  119.     AddChild(m_TkhdAtom);
  120.     AddChild(mdia);
  121. }
  122. /*----------------------------------------------------------------------
  123. |       AP4_TrakAtom::AP4_TrakAtom
  124. +---------------------------------------------------------------------*/
  125. AP4_TrakAtom::AP4_TrakAtom(AP4_Size         size,
  126.                            AP4_ByteStream&  stream,
  127.                            AP4_AtomFactory& atom_factory) :
  128.     AP4_ContainerAtom(AP4_ATOM_TYPE_TRAK, size, false, stream, atom_factory),
  129.     m_HdlrAtom(NULL),
  130.     m_TkhdAtom(NULL)
  131. {
  132.     AP4_Atom* tkhd = FindChild("tkhd");
  133.     if (tkhd != NULL) {
  134.         m_TkhdAtom = dynamic_cast<AP4_TkhdAtom*>(tkhd);
  135.     } else {
  136.         m_TkhdAtom  = NULL;
  137.     }
  138. }
  139. /*----------------------------------------------------------------------
  140. |       AP4_TrakAtom::GetDuration
  141. +---------------------------------------------------------------------*/
  142. AP4_UI32
  143. AP4_TrakAtom::GetDuration()
  144. {
  145.     if (m_TkhdAtom) {
  146.         return m_TkhdAtom->GetDuration();
  147.     } else {
  148.         return 0;
  149.     }
  150. }
  151. /*----------------------------------------------------------------------
  152. |       AP4_TrakAtom::SetDuration
  153. +---------------------------------------------------------------------*/
  154. AP4_Result
  155. AP4_TrakAtom::SetDuration(AP4_UI32 duration)
  156. {
  157.     if (m_TkhdAtom) {
  158.         return m_TkhdAtom->SetDuration(duration);
  159.     } else {
  160.         return AP4_FAILURE;
  161.     }
  162. }
  163. /*----------------------------------------------------------------------
  164. |       AP4_TrakAtom::AdjustChunkOffsets
  165. +---------------------------------------------------------------------*/
  166. AP4_Result    
  167. AP4_TrakAtom::AdjustChunkOffsets(AP4_Offset offset)
  168. {
  169.     if (AP4_Atom* atom = FindChild("mdia/minf/stbl/co64")) {
  170.         AP4_Co64Atom* co64 = dynamic_cast<AP4_Co64Atom*>(atom);
  171.         co64->AdjustChunkOffsets(offset);
  172. }
  173.     AP4_Atom* atom = FindChild("mdia/minf/stbl/stco");
  174.     if (atom != NULL) {
  175.         AP4_StcoAtom* stco = dynamic_cast<AP4_StcoAtom*>(atom);
  176.         stco->AdjustChunkOffsets(offset);
  177.         return AP4_SUCCESS;
  178.     } else {
  179.         return AP4_FAILURE;
  180.     }
  181. }