Ap4MdhdAtom.cpp

00001 /*****************************************************************
00002 |
00003 |    AP4 - mdhd Atoms 
00004 |
00005 |    Copyright 2002 Gilles Boccon-Gibod
00006 |
00007 |
00008 |    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
00009 |
00010 |    Unless you have obtained Bento4 under a difference license,
00011 |    this version of Bento4 is Bento4|GPL.
00012 |    Bento4|GPL is free software; you can redistribute it and/or modify
00013 |    it under the terms of the GNU General Public License as published by
00014 |    the Free Software Foundation; either version 2, or (at your option)
00015 |    any later version.
00016 |
00017 |    Bento4|GPL is distributed in the hope that it will be useful,
00018 |    but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 |    GNU General Public License for more details.
00021 |
00022 |    You should have received a copy of the GNU General Public License
00023 |    along with Bento4|GPL; see the file COPYING.  If not, write to the
00024 |    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
00025 |    02111-1307, USA.
00026 |
00027  ****************************************************************/
00028 
00029 /*----------------------------------------------------------------------
00030 |       includes
00031 +---------------------------------------------------------------------*/
00032 #include "Ap4.h"
00033 #include "Ap4MdhdAtom.h"
00034 #include "Ap4AtomFactory.h"
00035 #include "Ap4Utils.h"
00036 
00037 /*----------------------------------------------------------------------
00038 |       AP4_MdhdAtom::AP4_MdhdAtom
00039 +---------------------------------------------------------------------*/
00040 AP4_MdhdAtom::AP4_MdhdAtom(AP4_UI32    creation_time,
00041                            AP4_UI32    modification_time,
00042                            AP4_UI32    time_scale,
00043                            AP4_UI32    duration,
00044                            const char* language) :
00045     AP4_Atom(AP4_ATOM_TYPE_MDHD, 20+AP4_FULL_ATOM_HEADER_SIZE, true),
00046     m_CreationTime(creation_time),
00047     m_ModificationTime(modification_time),
00048     m_TimeScale(time_scale),
00049     m_Duration(duration)
00050 {
00051     m_Language[0] = language[0];
00052     m_Language[1] = language[1];
00053     m_Language[2] = language[2];
00054 }
00055 
00056 /*----------------------------------------------------------------------
00057 |       AP4_MdhdAtom::AP4_MdhdAtom
00058 +---------------------------------------------------------------------*/
00059 AP4_MdhdAtom::AP4_MdhdAtom(AP4_Size size, AP4_ByteStream& stream) :
00060     AP4_Atom(AP4_ATOM_TYPE_MDHD, size, true, stream),
00061     m_CreationTime(0),
00062     m_ModificationTime(0),
00063     m_TimeScale(0),
00064     m_Duration(0)
00065 {
00066     m_Language[0] = 0;
00067     m_Language[1] = 0;
00068     m_Language[2] = 0;
00069 
00070     if (m_Version == 0) {
00071         // we only deal with version 0 for now
00072         stream.ReadUI32(m_CreationTime);
00073         stream.ReadUI32(m_ModificationTime);
00074         stream.ReadUI32(m_TimeScale);
00075         stream.ReadUI32(m_Duration);
00076     } else {
00077         // stream.ReadUI64(m_CreationTime);
00078         stream.Read((void*)m_Reserved1, sizeof(m_Reserved1));
00079         // stream.ReadUI64(m_ModificationTime);
00080         stream.Read((void*)m_Reserved2, sizeof(m_Reserved2));
00081         stream.ReadUI32(m_TimeScale);
00082         //stream.ReadUI64(m_Duration);
00083         stream.Read((void*)m_Reserved3, sizeof(m_Reserved3));
00084     }
00085     
00086     unsigned char lang[2];
00087     stream.Read(lang, 2, NULL);
00088     char l0 = ((lang[0]>>2)&0x1F);
00089     char l1 = (((lang[0]&0x3)<<3) | ((lang[1]>>5)&0x7));
00090     char l2 = ((lang[1]&0x1F));
00091     if (l0) {
00092         m_Language[0] = l0+0x60;
00093     }
00094     if (l1) {
00095         m_Language[1] = l1+0x60;
00096     }
00097     if (l2) {
00098         m_Language[2] = l2+0x60;
00099     }
00100 }
00101 
00102 /*----------------------------------------------------------------------
00103 |       AP4_MdhdAtom::WriteFields
00104 +---------------------------------------------------------------------*/
00105 AP4_Result
00106 AP4_MdhdAtom::WriteFields(AP4_ByteStream& stream)
00107 {
00108     AP4_Result result;
00109 
00110     if (m_Version == 0) {
00111         // we only deal with version 0 for the moment
00112         result = stream.WriteUI32(m_CreationTime);
00113         if (AP4_FAILED(result)) return result;
00114         result = stream.WriteUI32(m_ModificationTime);
00115         if (AP4_FAILED(result)) return result;
00116         result = stream.WriteUI32(m_TimeScale);
00117         if (AP4_FAILED(result)) return result;
00118         result = stream.WriteUI32(m_Duration);
00119         if (AP4_FAILED(result)) return result;
00120     } else {
00121         result = stream.Write(m_Reserved1, sizeof(m_Reserved1));
00122         if (AP4_FAILED(result)) return result;
00123     }
00124 
00125     // write the language
00126     AP4_UI08 l0 = (m_Language[0]==0)?0:(m_Language[0]-0x60);
00127     AP4_UI08 l1 = (m_Language[1]==0)?0:(m_Language[1]-0x60);
00128     AP4_UI08 l2 = (m_Language[2]==0)?0:(m_Language[2]-0x60);
00129     result = stream.WriteUI08(l0<<2 | l1>>3);
00130     if (AP4_FAILED(result)) return result;
00131     result = stream.WriteUI08(l1<<5 | l2);
00132     if (AP4_FAILED(result)) return result;
00133 
00134     // pre-defined
00135     return stream.WriteUI16(0);
00136 }
00137 
00138 /*----------------------------------------------------------------------
00139 |       AP4_MdhdAtom::GetDurationMs
00140 +---------------------------------------------------------------------*/
00141 AP4_UI32
00142 AP4_MdhdAtom::GetDurationMs()
00143 {
00144     return AP4_DurationMsFromUnits(m_Duration, m_TimeScale);
00145 }
00146 
00147 /*----------------------------------------------------------------------
00148 |       AP4_MdhdAtom::InspectFields
00149 +---------------------------------------------------------------------*/
00150 AP4_Result
00151 AP4_MdhdAtom::InspectFields(AP4_AtomInspector& inspector)
00152 {
00153     inspector.AddField("timescale", m_TimeScale);
00154     inspector.AddField("duration", m_Duration);
00155     inspector.AddField("duration(ms)", GetDurationMs());
00156     char language[4];
00157     AP4_StringFormat(language, sizeof(language), 
00158         "%c%c%c", 
00159         m_Language[0] ? m_Language[0]:'-',
00160         m_Language[1] ? m_Language[1]:'-',
00161         m_Language[2] ? m_Language[2]:'-');
00162     inspector.AddField("language", (const char*)language);
00163 
00164     return AP4_SUCCESS;
00165 }

Generated on Tue Dec 13 14:47:19 2005 for guliverkli by  doxygen 1.4.5