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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - Track Objects
  4. |
  5. |    Copyright 2002 Gilles Boccon-Gibod & Julien Boeuf
  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 "Ap4ByteStream.h"
  32. #include "Ap4HdlrAtom.h"
  33. #include "Ap4MvhdAtom.h"
  34. #include "Ap4Track.h"
  35. #include "Ap4Utils.h"
  36. #include "Ap4Sample.h"
  37. #include "Ap4DataBuffer.h"
  38. #include "Ap4TrakAtom.h"
  39. #include "Ap4MoovAtom.h"
  40. #include "Ap4AtomSampleTable.h"
  41. #include "Ap4SdpAtom.h"
  42. #include "Ap4MdhdAtom.h"
  43. /*----------------------------------------------------------------------
  44. |       AP4_Track::AP4_Track
  45. +---------------------------------------------------------------------*/
  46. AP4_Track::AP4_Track(Type             type,
  47.                      AP4_SampleTable* sample_table,
  48.                      AP4_UI32         track_id, 
  49.                      AP4_UI32         movie_time_scale,
  50.                      AP4_UI32         media_time_scale,
  51.                      AP4_UI32         media_duration,
  52.                      const char*      language,
  53.                      AP4_UI32         width,
  54.                      AP4_UI32         height) :
  55.     m_TrakAtomIsOwned(true),
  56.     m_Type(type),
  57.     m_SampleTable(sample_table),
  58.     m_SampleTableIsOwned(false),
  59.     m_MovieTimeScale(movie_time_scale ? 
  60.                      movie_time_scale : 
  61.                      AP4_TRACK_DEFAULT_MOVIE_TIMESCALE),
  62.     m_MediaTimeScale(media_time_scale)
  63. {
  64.     // compute the default volume value
  65.     unsigned int volume = 0;
  66.     if (type == TYPE_AUDIO) volume = 0x100;
  67.     // compute the handler type and name
  68.     AP4_Atom::Type hdlr_type;
  69.     const char* hdlr_name;
  70.     switch (type) {
  71.         case TYPE_AUDIO:
  72.             hdlr_type = AP4_HANDLER_TYPE_SOUN;
  73.             hdlr_name = "Bento4 Sound Handler";
  74.             break;
  75.         case TYPE_VIDEO:
  76.             hdlr_type = AP4_HANDLER_TYPE_VIDE;
  77.             hdlr_name = "Bento4 Video Handler";
  78.             break;
  79.         case TYPE_HINT:
  80.             hdlr_type = AP4_HANDLER_TYPE_HINT;
  81.             hdlr_name = "Bento4 Hint Handler";
  82.             break;
  83.         default:
  84.             hdlr_type = 0;
  85.             hdlr_name = NULL;
  86.             break;
  87.     }
  88.     // compute the track duration in units of the movie time scale
  89.     AP4_UI32 track_duration = AP4_ConvertTime(media_duration,
  90.                                               media_time_scale,
  91.                                               movie_time_scale);
  92.     // create a trak atom
  93.     m_TrakAtom = new AP4_TrakAtom(sample_table,
  94.                                   hdlr_type, 
  95.                                   hdlr_name,
  96.                                   track_id, 
  97.                                   0, 
  98.                                   0, 
  99.                                   track_duration,
  100.                                   media_time_scale,
  101.                                   media_duration,
  102.                                   volume, 
  103.                                   language,
  104.                                   width, 
  105.                                   height);
  106. }
  107. /*----------------------------------------------------------------------
  108. |       AP4_Track::AP4_Track
  109. +---------------------------------------------------------------------*/
  110. AP4_Track::AP4_Track(AP4_TrakAtom&   atom, 
  111.                      AP4_ByteStream& sample_stream, 
  112.                      AP4_UI32        movie_time_scale) :
  113.     m_TrakAtom(&atom),
  114.     m_TrakAtomIsOwned(false),
  115.     m_Type(TYPE_UNKNOWN),
  116.     m_SampleTable(NULL),
  117.     m_SampleTableIsOwned(true),
  118.     m_MovieTimeScale(movie_time_scale),
  119.     m_MediaTimeScale(0)
  120. {
  121.     // find the handler type
  122.     AP4_Atom* sub = atom.FindChild("mdia/hdlr");
  123.     if (sub) {
  124.         AP4_HdlrAtom* hdlr = dynamic_cast<AP4_HdlrAtom*>(sub);
  125.         if (hdlr) {
  126.             AP4_Atom::Type type = hdlr->GetHandlerType();
  127.             if (type == AP4_HANDLER_TYPE_SOUN) {
  128.                 m_Type = TYPE_AUDIO;
  129.             } else if (type == AP4_HANDLER_TYPE_VIDE) {
  130.                 m_Type = TYPE_VIDEO;
  131.             } else if (type == AP4_HANDLER_TYPE_TEXT) {
  132.                 m_Type = TYPE_TEXT;
  133.             } else if (type == AP4_HANDLER_TYPE_TX3G) {
  134.                 m_Type = TYPE_TEXT;
  135.             } else if (type == AP4_HANDLER_TYPE_HINT) {
  136.                 m_Type = TYPE_HINT;
  137.             }
  138.         }
  139.     }
  140.     // get the media time scale
  141.     sub = atom.FindChild("mdia/mdhd");
  142.     if (sub) {
  143.         AP4_MdhdAtom* mdhd = dynamic_cast<AP4_MdhdAtom*>(sub);
  144.         if (mdhd) {
  145.             m_MediaTimeScale = mdhd->GetTimeScale();
  146.         }
  147.     }
  148.     // create a facade for the stbl atom
  149.     AP4_ContainerAtom* stbl = dynamic_cast<AP4_ContainerAtom*>(
  150.         atom.FindChild("mdia/minf/stbl"));
  151.     if (stbl) {
  152.         m_SampleTable = new AP4_AtomSampleTable(stbl, sample_stream);
  153.     }
  154. }
  155. /*----------------------------------------------------------------------
  156. |       AP4_Track::~AP4_Track
  157. +---------------------------------------------------------------------*/
  158. AP4_Track::~AP4_Track()
  159. {
  160.     if (m_TrakAtomIsOwned) delete m_TrakAtom;
  161.     if (m_SampleTableIsOwned) delete m_SampleTable;
  162. }
  163. /*----------------------------------------------------------------------
  164. |       AP4_Track::GetId
  165. +---------------------------------------------------------------------*/
  166. AP4_UI32
  167. AP4_Track::GetId()
  168. {
  169.     return m_TrakAtom->GetId();
  170. }
  171. /*----------------------------------------------------------------------
  172. |       AP4_Track::SetId
  173. +---------------------------------------------------------------------*/
  174. AP4_Result
  175. AP4_Track::SetId(AP4_UI32 id)
  176. {
  177.     m_TrakAtom->SetId(id);
  178.     return AP4_SUCCESS;
  179. }
  180. /*----------------------------------------------------------------------
  181. |       AP4_Track::GetDuration
  182. +---------------------------------------------------------------------*/
  183. AP4_UI32
  184. AP4_Track::GetDuration()
  185. {
  186.     return m_TrakAtom->GetDuration();
  187. }
  188. /*----------------------------------------------------------------------
  189. |       AP4_Track::GetDurationMs
  190. +---------------------------------------------------------------------*/
  191. AP4_Duration
  192. AP4_Track::GetDurationMs()
  193. {
  194.     AP4_UI32 duration = m_TrakAtom->GetDuration();
  195.     return AP4_DurationMsFromUnits(duration, m_MovieTimeScale);
  196. }
  197. /*----------------------------------------------------------------------
  198. |       AP4_Track::GetSampleCount
  199. +---------------------------------------------------------------------*/
  200. AP4_Cardinal
  201. AP4_Track::GetSampleCount()
  202. {
  203.     // delegate to the sample table
  204.     return m_SampleTable ? m_SampleTable->GetSampleCount() : 0;
  205. }
  206. /*----------------------------------------------------------------------
  207. |       AP4_Track::GetSample
  208. +---------------------------------------------------------------------*/
  209. AP4_Result 
  210. AP4_Track::GetSample(AP4_Ordinal index, AP4_Sample& sample)
  211. {
  212.     // delegate to the sample table
  213.     return m_SampleTable ? m_SampleTable->GetSample(index, sample) : AP4_FAILURE;
  214. }
  215. /*----------------------------------------------------------------------
  216. |       AP4_Track::GetSampleDescription
  217. +---------------------------------------------------------------------*/
  218. AP4_SampleDescription*
  219. AP4_Track::GetSampleDescription(AP4_Ordinal index)
  220. {
  221.     // delegate to the sample table
  222.     return m_SampleTable ? m_SampleTable->GetSampleDescription(index) : NULL;
  223. }
  224. /*----------------------------------------------------------------------
  225. |       AP4_Track::ReadSample
  226. +---------------------------------------------------------------------*/
  227. AP4_Result   
  228. AP4_Track::ReadSample(AP4_Ordinal     index, 
  229.                       AP4_Sample&     sample,
  230.                       AP4_DataBuffer& data)
  231. {
  232.     AP4_Result result;
  233.     // get the sample
  234.     result = GetSample(index, sample);
  235.     if (AP4_FAILED(result)) return result;
  236.     // read the data
  237.     return sample.ReadData(data);
  238. }
  239. /*----------------------------------------------------------------------
  240. |       AP4_Track::GetSampleIndexForTimeStampMs
  241. +---------------------------------------------------------------------*/
  242. AP4_Result  
  243. AP4_Track::GetSampleIndexForTimeStampMs(AP4_TimeStamp ts, AP4_Ordinal& index)
  244. {
  245.     // convert the ts in the timescale of the track's media
  246.     ts = AP4_ConvertTime(ts, 1000, m_MediaTimeScale);
  247.     return m_SampleTable->GetSampleIndexForTimeStamp(ts, index);
  248. }
  249. /*----------------------------------------------------------------------
  250. |       AP4_Track::SetMovieTimeScale
  251. +---------------------------------------------------------------------*/
  252. AP4_Result
  253. AP4_Track::SetMovieTimeScale(AP4_UI32 time_scale)
  254. {
  255.     // check that we can convert
  256.     if (m_MovieTimeScale == 0) return AP4_FAILURE;
  257.     // convert from one time scale to the other
  258.     m_TrakAtom->SetDuration(AP4_ConvertTime(m_TrakAtom->GetDuration(), 
  259.                                             m_MovieTimeScale,
  260.                                             time_scale));
  261.     
  262.     // keep the new movie timescale
  263.     m_MovieTimeScale = time_scale;
  264.     return AP4_SUCCESS;
  265. }
  266. /*----------------------------------------------------------------------
  267. |       AP4_Track::GetMediaTimeScale
  268. +---------------------------------------------------------------------*/
  269. AP4_UI32
  270. AP4_Track::GetMediaTimeScale()
  271. {
  272.     return m_MediaTimeScale;
  273. }
  274. // save the implementation for later
  275. #if 0 
  276. /*----------------------------------------------------------------------
  277. |       AP4_HintTrack::SetSdpText
  278. +---------------------------------------------------------------------*/
  279. void
  280. AP4_HintTrack::SetSdpText(const char* text)
  281. {
  282.     // build an sdp atom
  283.     AP4_SdpAtom* sdp = new AP4_SdpAtom(text);
  284.     // build the hnti
  285.     AP4_ContainerAtom* hnti = new AP4_ContainerAtom(AP4_ATOM_TYPE_HNTI);
  286.     hnti->AddChild(sdp);
  287.     // check if there's already a user data atom
  288.     AP4_ContainerAtom* udta = dynamic_cast<AP4_ContainerAtom*>(m_TrakAtom->FindChild("udta"));
  289.     if (udta == NULL) {
  290.         // otherwise create it
  291.         udta = new AP4_ContainerAtom(AP4_ATOM_TYPE_UDTA);
  292.         m_TrakAtom->AddChild(udta);
  293.     }
  294.     udta->AddChild(hnti);
  295. }
  296. #endif
  297. /*----------------------------------------------------------------------
  298. |       AP4_Track::GetTrackName
  299. +---------------------------------------------------------------------*/
  300. AP4_String
  301. AP4_Track::GetTrackName()
  302. {
  303. AP4_String TrackName;
  304. if(AP4_HdlrAtom* hdlr = dynamic_cast<AP4_HdlrAtom*>(m_TrakAtom->FindChild("mdia/hdlr")))
  305. TrackName = hdlr->GetHandlerName();
  306. return TrackName;
  307. }
  308. /*----------------------------------------------------------------------
  309. |       AP4_Track::GetTrackLanguage
  310. +---------------------------------------------------------------------*/
  311. AP4_String
  312. AP4_Track::GetTrackLanguage()
  313. {
  314. AP4_String TrackLanguage;
  315. if(AP4_MdhdAtom* mdhd = dynamic_cast<AP4_MdhdAtom*>(m_TrakAtom->FindChild("mdia/mdhd")))
  316. TrackLanguage = mdhd->GetLanguage().c_str();
  317. return TrackLanguage;
  318. }