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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - File Writer
  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 "Ap4MoovAtom.h"
  32. #include "Ap4FileWriter.h"
  33. /*----------------------------------------------------------------------
  34. |       AP4_FileWriter::AP4_FileWriter
  35. +---------------------------------------------------------------------*/
  36. AP4_FileWriter::AP4_FileWriter(AP4_File& file) : m_File(file)
  37. {
  38. }
  39. /*----------------------------------------------------------------------
  40. |       AP4_FileWriter::~AP4_FileWriter
  41. +---------------------------------------------------------------------*/
  42. AP4_FileWriter::~AP4_FileWriter()
  43. {
  44. }
  45. /*----------------------------------------------------------------------
  46. |       AP4_FileWriter::Write
  47. +---------------------------------------------------------------------*/
  48. AP4_Result
  49. AP4_FileWriter::Write(AP4_ByteStream& stream)
  50. {
  51.     //AP4_Result result;
  52.     // get the movie object
  53.     AP4_Movie* movie = m_File.GetMovie();
  54.     if (movie == NULL) return AP4_SUCCESS;
  55.     // compute the final offset of the sample data in mdat
  56.     AP4_Offset data_offset = movie->GetMoovAtom()->GetSize()+AP4_ATOM_HEADER_SIZE;
  57.     // adjust the tracks
  58.     AP4_List<AP4_Track>::Item* track_item = movie->GetTracks().FirstItem();
  59.     while (track_item) {
  60.         AP4_Track* track = track_item->GetData();
  61.         track->GetTrakAtom()->AdjustChunkOffsets(data_offset);
  62.         track_item = track_item->GetNext();
  63.     }
  64.     // write the moov atom
  65.     movie->GetMoovAtom()->Write(stream);
  66.     // create an mdat
  67.     stream.WriteUI32(0);
  68.     stream.Write("mdat", 4);
  69.     AP4_Track* track = movie->GetTracks().FirstItem()->GetData();
  70.     AP4_Cardinal sample_count = track->GetSampleCount();
  71.     for (AP4_Ordinal i=0; i<sample_count; i++) {
  72.         AP4_Sample sample;
  73.         AP4_DataBuffer data;
  74.         track->ReadSample(i, sample, data);
  75.         stream.Write(data.GetData(), data.GetDataSize());
  76.     }
  77.     // TODO: update the mdat size
  78.     return AP4_SUCCESS;
  79. }