Ap4Utils.cpp

00001 /*****************************************************************
00002 |
00003 |    AP4 - Utilities
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 "Ap4Utils.h"
00034 
00035 /*----------------------------------------------------------------------
00036 |       AP4_BytesToUInt64BE
00037 +---------------------------------------------------------------------*/
00038 unsigned long long 
00039 AP4_BytesToUInt64BE(const unsigned char* bytes)
00040 {
00041     return 
00042         ( ((unsigned long long)bytes[0])<<56 ) |
00043         ( ((unsigned long long)bytes[1])<<48 ) |
00044         ( ((unsigned long long)bytes[2])<<40 ) |
00045         ( ((unsigned long long)bytes[3])<<32 ) |
00046         ( ((unsigned long long)bytes[4])<<24 ) |
00047         ( ((unsigned long long)bytes[5])<<16 ) |
00048         ( ((unsigned long long)bytes[6])<<8  ) |
00049         ( ((unsigned long long)bytes[7])     );    
00050 }
00051 
00052 /*----------------------------------------------------------------------
00053 |       AP4_BytesToUInt32BE
00054 +---------------------------------------------------------------------*/
00055 unsigned long 
00056 AP4_BytesToUInt32BE(const unsigned char* bytes)
00057 {
00058     return 
00059         ( ((unsigned long)bytes[0])<<24 ) |
00060         ( ((unsigned long)bytes[1])<<16 ) |
00061         ( ((unsigned long)bytes[2])<<8  ) |
00062         ( ((unsigned long)bytes[3])     );    
00063 }
00064 
00065 /*----------------------------------------------------------------------
00066 |       AP4_BytesToUInt24BE
00067 +---------------------------------------------------------------------*/
00068 unsigned long
00069 AP4_BytesToUInt24BE(const unsigned char* bytes)
00070 {
00071     return 
00072         ( ((unsigned long)bytes[0])<<16 ) |
00073         ( ((unsigned long)bytes[1])<<8  ) |
00074         ( ((unsigned long)bytes[2])     );    
00075 }
00076 
00077 /*----------------------------------------------------------------------
00078 |       AP4_BytesToUInt16BE
00079 +---------------------------------------------------------------------*/
00080 unsigned short
00081 AP4_BytesToUInt16BE(const unsigned char* bytes)
00082 {
00083     return 
00084         ( ((unsigned short)bytes[0])<<8  ) |
00085         ( ((unsigned short)bytes[1])     );    
00086 }
00087 
00088 /*----------------------------------------------------------------------
00089 |       AP4_BytesFromUInt64BE
00090 +---------------------------------------------------------------------*/
00091 void
00092 AP4_BytesFromUInt64BE(unsigned char* bytes, unsigned long long value)
00093 {
00094     bytes[0] = (unsigned char)(value >> 56);
00095     bytes[1] = (unsigned char)(value >> 48);
00096     bytes[2] = (unsigned char)(value >> 40);
00097     bytes[3] = (unsigned char)(value >> 32);
00098     bytes[4] = (unsigned char)(value >> 24);
00099     bytes[5] = (unsigned char)(value >> 16);
00100     bytes[6] = (unsigned char)(value >>  8);
00101     bytes[7] = (unsigned char)(value      );
00102 }
00103 
00104 /*----------------------------------------------------------------------
00105 |       AP4_BytesFromUInt32BE
00106 +---------------------------------------------------------------------*/
00107 void
00108 AP4_BytesFromUInt32BE(unsigned char* bytes, unsigned long value)
00109 {
00110     bytes[0] = (unsigned char)(value >> 24);
00111     bytes[1] = (unsigned char)(value >> 16);
00112     bytes[2] = (unsigned char)(value >>  8);
00113     bytes[3] = (unsigned char)(value      );
00114 }
00115 
00116 /*----------------------------------------------------------------------
00117 |       AP4_BytesFromUInt24BE
00118 +---------------------------------------------------------------------*/
00119 void
00120 AP4_BytesFromUInt24BE(unsigned char* bytes, unsigned long value)
00121 {
00122     bytes[0] = (unsigned char)(value >> 16);
00123     bytes[1] = (unsigned char)(value >>  8);
00124     bytes[2] = (unsigned char)(value      );
00125 }
00126 
00127 /*----------------------------------------------------------------------
00128 |       AP4_BytesFromUInt16BE
00129 +---------------------------------------------------------------------*/
00130 void
00131 AP4_BytesFromUInt16BE(unsigned char* bytes, unsigned short value)
00132 {
00133     bytes[0] = (unsigned char)(value >> 8);
00134     bytes[1] = (unsigned char)(value     );
00135 }
00136 
00137 /*----------------------------------------------------------------------
00138 |       AP4_MakePrefixString
00139 +---------------------------------------------------------------------*/
00140 static void
00141 AP4_MakePrefixString(AP4_Offset indent, char* prefix, AP4_Size size)
00142 {
00143     if (size == 0) return;
00144     if (indent >= size-1) indent = size-1;
00145     for (unsigned int i=0; i<indent; i++) {
00146         prefix[i] = ' ';
00147     }
00148     prefix[indent] = '\0';    
00149 }
00150 
00151 /*----------------------------------------------------------------------
00152 |       AP4_DurationMsFromUnits
00153 +---------------------------------------------------------------------*/
00154 unsigned long
00155 AP4_DurationMsFromUnits(unsigned long units, unsigned long units_per_second)
00156 {
00157         if (units_per_second == 0) return 0;
00158         return (unsigned long)(((float)units*1000.0f)/(float)units_per_second);
00159 }
00160 
00161 /*----------------------------------------------------------------------
00162 |       AP4_ConvertTime
00163 +---------------------------------------------------------------------*/
00164 unsigned long 
00165 AP4_ConvertTime(unsigned long time_value,
00166                 unsigned long from_time_scale,
00167                 unsigned long to_time_scale)
00168 {
00169     if (from_time_scale == 0) return 0;
00170     float ratio = (float)to_time_scale/(float)from_time_scale;
00171     return ((unsigned long)((float)time_value*ratio));
00172 }
00173 
00174 /*----------------------------------------------------------------------
00175 |       AP4_FormatFourChars
00176 +---------------------------------------------------------------------*/
00177 void
00178 AP4_FormatFourChars(char* str, AP4_UI32 value) {
00179     str[0] = (value >> 24) & 0xFF;
00180     str[1] = (value >> 16) & 0xFF;
00181     str[2] = (value >>  8) & 0xFF;
00182     str[3] = (value      ) & 0xFF;
00183     str[4] = '\0';
00184 }
00185 
00186 /*----------------------------------------------------------------------
00187 |       AP4_SplitArgs
00188 +---------------------------------------------------------------------*/
00189 AP4_Result
00190 AP4_SplitArgs(char* arg, char*& arg0, char*& arg1)
00191 {
00192     arg0 = arg;
00193     char* c = arg;
00194     while (*c != 0 && *c != ':') {
00195         c++;
00196     }
00197     if (*c == ':') {
00198         *c++ = '\0';
00199         arg1 = c;
00200         return AP4_SUCCESS;
00201     } else {
00202         return AP4_FAILURE;
00203     }
00204 }
00205 
00206 /*----------------------------------------------------------------------
00207 |       AP4_SplitArgs
00208 +---------------------------------------------------------------------*/
00209 AP4_Result
00210 AP4_SplitArgs(char* arg, char*& arg0, char*& arg1, char*& arg2)
00211 {
00212     AP4_Result result = AP4_SplitArgs(arg, arg0, arg1);
00213     if (AP4_FAILED(result)) return result;
00214     return AP4_SplitArgs(arg1, arg1, arg2);
00215 }
00216 
00217 /*----------------------------------------------------------------------
00218 |       AP4_HexNibble
00219 +---------------------------------------------------------------------*/
00220 static unsigned char
00221 AP4_HexNibble(char c)
00222 {
00223     switch (c) {
00224         case '0': return 0;
00225         case '1': return 1;
00226         case '2': return 2;
00227         case '3': return 3;
00228         case '4': return 4;
00229         case '5': return 5;
00230         case '6': return 6;
00231         case '7': return 7;
00232         case '8': return 8;
00233         case '9': return 9;
00234         case 'a': case 'A': return 10;
00235         case 'b': case 'B': return 11;
00236         case 'c': case 'C': return 12;
00237         case 'd': case 'D': return 13;
00238         case 'e': case 'E': return 14;
00239         case 'f': case 'F': return 15;
00240         default: return 0;
00241     }
00242 }
00243 
00244 /*----------------------------------------------------------------------
00245 |       AP4_ParseHex
00246 +---------------------------------------------------------------------*/
00247 AP4_Result
00248 AP4_ParseHex(const char* hex, unsigned char* bytes, unsigned int count)
00249 {
00250     if (strlen(hex) != 2*count) return AP4_ERROR_INVALID_PARAMETERS;
00251     for (unsigned int i=0; i<count; i++) {
00252         bytes[i] = (AP4_HexNibble(hex[2*i]) << 4) | (AP4_HexNibble(hex[2*i+1]));
00253     }
00254     return AP4_SUCCESS;
00255 }
00256 
00257 /*----------------------------------------------------------------------
00258 |       AP4_PrintInspector::AP4_PrintInspector
00259 +---------------------------------------------------------------------*/
00260 AP4_PrintInspector::AP4_PrintInspector(AP4_ByteStream& stream) :
00261     m_Stream(&stream),
00262     m_Indent(0)
00263 {
00264     m_Stream->AddReference();
00265 }
00266 
00267 /*----------------------------------------------------------------------
00268 |       AP4_PrintInspector::~AP4_PrintInspector
00269 +---------------------------------------------------------------------*/
00270 AP4_PrintInspector::~AP4_PrintInspector()
00271 {
00272     m_Stream->Release();
00273 }
00274 
00275 /*----------------------------------------------------------------------
00276 |       AP4_PrintInspector::StartElement
00277 +---------------------------------------------------------------------*/
00278 void
00279 AP4_PrintInspector::StartElement(const char* name, const char* info)
00280 {
00281     char prefix[256];
00282     AP4_MakePrefixString(m_Indent, prefix, sizeof(prefix));
00283     m_Stream->WriteString(prefix);
00284     m_Stream->WriteString(name);
00285     if (info) {
00286         m_Stream->Write(" ", 1);
00287         m_Stream->WriteString(info);
00288     }
00289     m_Stream->Write("\n", 1);
00290 
00291     m_Indent += 2;
00292 }
00293 
00294 /*----------------------------------------------------------------------
00295 |       AP4_PrintInspector::EndElement
00296 +---------------------------------------------------------------------*/
00297 void
00298 AP4_PrintInspector::EndElement()
00299 {
00300     m_Indent -= 2;
00301 }
00302 
00303 /*----------------------------------------------------------------------
00304 |       AP4_PrintInspector::AddField
00305 +---------------------------------------------------------------------*/
00306 void
00307 AP4_PrintInspector::AddField(const char* name, const char* value, FormatHint hint)
00308 {
00309     char prefix[256];
00310     AP4_MakePrefixString(m_Indent, prefix, sizeof(prefix));
00311     m_Stream->WriteString(prefix);
00312     
00313     m_Stream->WriteString(name);
00314     m_Stream->WriteString(" = ");
00315     m_Stream->WriteString(value);
00316     m_Stream->Write("\n", 1);
00317 }
00318 
00319 /*----------------------------------------------------------------------
00320 |       AP4_PrintInspector::AddField
00321 +---------------------------------------------------------------------*/
00322 void
00323 AP4_PrintInspector::AddField(const char* name, AP4_UI32 value, FormatHint hint)
00324 {
00325     char prefix[256];
00326     AP4_MakePrefixString(m_Indent, prefix, sizeof(prefix));
00327     m_Stream->WriteString(prefix);
00328 
00329     char str[32];
00330     AP4_StringFormat(str, sizeof(str), "%d", value);
00331     m_Stream->WriteString(name);
00332     m_Stream->WriteString(" = ");
00333     m_Stream->WriteString(str);
00334     m_Stream->Write("\n", 1);
00335 }

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