00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "Ap4.h"
00033 #include "Ap4MdhdAtom.h"
00034 #include "Ap4AtomFactory.h"
00035 #include "Ap4Utils.h"
00036
00037
00038
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
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
00072 stream.ReadUI32(m_CreationTime);
00073 stream.ReadUI32(m_ModificationTime);
00074 stream.ReadUI32(m_TimeScale);
00075 stream.ReadUI32(m_Duration);
00076 } else {
00077
00078 stream.Read((void*)m_Reserved1, sizeof(m_Reserved1));
00079
00080 stream.Read((void*)m_Reserved2, sizeof(m_Reserved2));
00081 stream.ReadUI32(m_TimeScale);
00082
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
00104
00105 AP4_Result
00106 AP4_MdhdAtom::WriteFields(AP4_ByteStream& stream)
00107 {
00108 AP4_Result result;
00109
00110 if (m_Version == 0) {
00111
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
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
00135 return stream.WriteUI16(0);
00136 }
00137
00138
00139
00140
00141 AP4_UI32
00142 AP4_MdhdAtom::GetDurationMs()
00143 {
00144 return AP4_DurationMsFromUnits(m_Duration, m_TimeScale);
00145 }
00146
00147
00148
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 }