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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - Utilities
  4. |
  5. |    Copyright 2002 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 "Ap4.h"
  32. #include "Ap4Utils.h"
  33. /*----------------------------------------------------------------------
  34. |       AP4_BytesToUInt64BE
  35. +---------------------------------------------------------------------*/
  36. unsigned long long 
  37. AP4_BytesToUInt64BE(const unsigned char* bytes)
  38. {
  39.     return 
  40.         ( ((unsigned long long)bytes[0])<<56 ) |
  41.         ( ((unsigned long long)bytes[1])<<48 ) |
  42.         ( ((unsigned long long)bytes[2])<<40 ) |
  43.         ( ((unsigned long long)bytes[3])<<32 ) |
  44.         ( ((unsigned long long)bytes[4])<<24 ) |
  45.         ( ((unsigned long long)bytes[5])<<16 ) |
  46.         ( ((unsigned long long)bytes[6])<<8  ) |
  47.         ( ((unsigned long long)bytes[7])     );    
  48. }
  49. /*----------------------------------------------------------------------
  50. |       AP4_BytesToUInt32BE
  51. +---------------------------------------------------------------------*/
  52. unsigned long 
  53. AP4_BytesToUInt32BE(const unsigned char* bytes)
  54. {
  55.     return 
  56.         ( ((unsigned long)bytes[0])<<24 ) |
  57.         ( ((unsigned long)bytes[1])<<16 ) |
  58.         ( ((unsigned long)bytes[2])<<8  ) |
  59.         ( ((unsigned long)bytes[3])     );    
  60. }
  61. /*----------------------------------------------------------------------
  62. |       AP4_BytesToUInt24BE
  63. +---------------------------------------------------------------------*/
  64. unsigned long
  65. AP4_BytesToUInt24BE(const unsigned char* bytes)
  66. {
  67.     return 
  68.         ( ((unsigned long)bytes[0])<<16 ) |
  69.         ( ((unsigned long)bytes[1])<<8  ) |
  70.         ( ((unsigned long)bytes[2])     );    
  71. }
  72. /*----------------------------------------------------------------------
  73. |       AP4_BytesToUInt16BE
  74. +---------------------------------------------------------------------*/
  75. unsigned short
  76. AP4_BytesToUInt16BE(const unsigned char* bytes)
  77. {
  78.     return 
  79.         ( ((unsigned short)bytes[0])<<8  ) |
  80.         ( ((unsigned short)bytes[1])     );    
  81. }
  82. /*----------------------------------------------------------------------
  83. |       AP4_BytesFromUInt64BE
  84. +---------------------------------------------------------------------*/
  85. void
  86. AP4_BytesFromUInt64BE(unsigned char* bytes, unsigned long long value)
  87. {
  88.     bytes[0] = (unsigned char)(value >> 56);
  89.     bytes[1] = (unsigned char)(value >> 48);
  90.     bytes[2] = (unsigned char)(value >> 40);
  91.     bytes[3] = (unsigned char)(value >> 32);
  92.     bytes[4] = (unsigned char)(value >> 24);
  93.     bytes[5] = (unsigned char)(value >> 16);
  94.     bytes[6] = (unsigned char)(value >>  8);
  95.     bytes[7] = (unsigned char)(value      );
  96. }
  97. /*----------------------------------------------------------------------
  98. |       AP4_BytesFromUInt32BE
  99. +---------------------------------------------------------------------*/
  100. void
  101. AP4_BytesFromUInt32BE(unsigned char* bytes, unsigned long value)
  102. {
  103.     bytes[0] = (unsigned char)(value >> 24);
  104.     bytes[1] = (unsigned char)(value >> 16);
  105.     bytes[2] = (unsigned char)(value >>  8);
  106.     bytes[3] = (unsigned char)(value      );
  107. }
  108. /*----------------------------------------------------------------------
  109. |       AP4_BytesFromUInt24BE
  110. +---------------------------------------------------------------------*/
  111. void
  112. AP4_BytesFromUInt24BE(unsigned char* bytes, unsigned long value)
  113. {
  114.     bytes[0] = (unsigned char)(value >> 16);
  115.     bytes[1] = (unsigned char)(value >>  8);
  116.     bytes[2] = (unsigned char)(value      );
  117. }
  118. /*----------------------------------------------------------------------
  119. |       AP4_BytesFromUInt16BE
  120. +---------------------------------------------------------------------*/
  121. void
  122. AP4_BytesFromUInt16BE(unsigned char* bytes, unsigned short value)
  123. {
  124.     bytes[0] = (unsigned char)(value >> 8);
  125.     bytes[1] = (unsigned char)(value     );
  126. }
  127. /*----------------------------------------------------------------------
  128. |       AP4_MakePrefixString
  129. +---------------------------------------------------------------------*/
  130. static void
  131. AP4_MakePrefixString(AP4_Offset indent, char* prefix, AP4_Size size)
  132. {
  133.     if (size == 0) return;
  134.     if (indent >= size-1) indent = size-1;
  135.     for (unsigned int i=0; i<indent; i++) {
  136.         prefix[i] = ' ';
  137.     }
  138.     prefix[indent] = '';    
  139. }
  140. /*----------------------------------------------------------------------
  141. |       AP4_DurationMsFromUnits
  142. +---------------------------------------------------------------------*/
  143. unsigned long
  144. AP4_DurationMsFromUnits(unsigned long units, unsigned long units_per_second)
  145. {
  146. if (units_per_second == 0) return 0;
  147. return (unsigned long)(((float)units*1000.0f)/(float)units_per_second);
  148. }
  149. /*----------------------------------------------------------------------
  150. |       AP4_ConvertTime
  151. +---------------------------------------------------------------------*/
  152. unsigned long 
  153. AP4_ConvertTime(unsigned long time_value,
  154.                 unsigned long from_time_scale,
  155.                 unsigned long to_time_scale)
  156. {
  157.     if (from_time_scale == 0) return 0;
  158.     float ratio = (float)to_time_scale/(float)from_time_scale;
  159.     return ((unsigned long)((float)time_value*ratio));
  160. }
  161. /*----------------------------------------------------------------------
  162. |       AP4_FormatFourChars
  163. +---------------------------------------------------------------------*/
  164. void
  165. AP4_FormatFourChars(char* str, AP4_UI32 value) {
  166.     str[0] = (value >> 24) & 0xFF;
  167.     str[1] = (value >> 16) & 0xFF;
  168.     str[2] = (value >>  8) & 0xFF;
  169.     str[3] = (value      ) & 0xFF;
  170.     str[4] = '';
  171. }
  172. /*----------------------------------------------------------------------
  173. |       AP4_SplitArgs
  174. +---------------------------------------------------------------------*/
  175. AP4_Result
  176. AP4_SplitArgs(char* arg, char*& arg0, char*& arg1)
  177. {
  178.     arg0 = arg;
  179.     char* c = arg;
  180.     while (*c != 0 && *c != ':') {
  181.         c++;
  182.     }
  183.     if (*c == ':') {
  184.         *c++ = '';
  185.         arg1 = c;
  186.         return AP4_SUCCESS;
  187.     } else {
  188.         return AP4_FAILURE;
  189.     }
  190. }
  191. /*----------------------------------------------------------------------
  192. |       AP4_SplitArgs
  193. +---------------------------------------------------------------------*/
  194. AP4_Result
  195. AP4_SplitArgs(char* arg, char*& arg0, char*& arg1, char*& arg2)
  196. {
  197.     AP4_Result result = AP4_SplitArgs(arg, arg0, arg1);
  198.     if (AP4_FAILED(result)) return result;
  199.     return AP4_SplitArgs(arg1, arg1, arg2);
  200. }
  201. /*----------------------------------------------------------------------
  202. |       AP4_HexNibble
  203. +---------------------------------------------------------------------*/
  204. static unsigned char
  205. AP4_HexNibble(char c)
  206. {
  207.     switch (c) {
  208.         case '0': return 0;
  209.         case '1': return 1;
  210.         case '2': return 2;
  211.         case '3': return 3;
  212.         case '4': return 4;
  213.         case '5': return 5;
  214.         case '6': return 6;
  215.         case '7': return 7;
  216.         case '8': return 8;
  217.         case '9': return 9;
  218.         case 'a': case 'A': return 10;
  219.         case 'b': case 'B': return 11;
  220.         case 'c': case 'C': return 12;
  221.         case 'd': case 'D': return 13;
  222.         case 'e': case 'E': return 14;
  223.         case 'f': case 'F': return 15;
  224.         default: return 0;
  225.     }
  226. }
  227. /*----------------------------------------------------------------------
  228. |       AP4_ParseHex
  229. +---------------------------------------------------------------------*/
  230. AP4_Result
  231. AP4_ParseHex(const char* hex, unsigned char* bytes, unsigned int count)
  232. {
  233.     if (strlen(hex) != 2*count) return AP4_ERROR_INVALID_PARAMETERS;
  234.     for (unsigned int i=0; i<count; i++) {
  235.         bytes[i] = (AP4_HexNibble(hex[2*i]) << 4) | (AP4_HexNibble(hex[2*i+1]));
  236.     }
  237.     return AP4_SUCCESS;
  238. }
  239. /*----------------------------------------------------------------------
  240. |       AP4_PrintInspector::AP4_PrintInspector
  241. +---------------------------------------------------------------------*/
  242. AP4_PrintInspector::AP4_PrintInspector(AP4_ByteStream& stream) :
  243.     m_Stream(&stream),
  244.     m_Indent(0)
  245. {
  246.     m_Stream->AddReference();
  247. }
  248. /*----------------------------------------------------------------------
  249. |       AP4_PrintInspector::~AP4_PrintInspector
  250. +---------------------------------------------------------------------*/
  251. AP4_PrintInspector::~AP4_PrintInspector()
  252. {
  253.     m_Stream->Release();
  254. }
  255. /*----------------------------------------------------------------------
  256. |       AP4_PrintInspector::StartElement
  257. +---------------------------------------------------------------------*/
  258. void
  259. AP4_PrintInspector::StartElement(const char* name, const char* info)
  260. {
  261.     char prefix[256];
  262.     AP4_MakePrefixString(m_Indent, prefix, sizeof(prefix));
  263.     m_Stream->WriteString(prefix);
  264.     m_Stream->WriteString(name);
  265.     if (info) {
  266.         m_Stream->Write(" ", 1);
  267.         m_Stream->WriteString(info);
  268.     }
  269.     m_Stream->Write("n", 1);
  270.     m_Indent += 2;
  271. }
  272. /*----------------------------------------------------------------------
  273. |       AP4_PrintInspector::EndElement
  274. +---------------------------------------------------------------------*/
  275. void
  276. AP4_PrintInspector::EndElement()
  277. {
  278.     m_Indent -= 2;
  279. }
  280. /*----------------------------------------------------------------------
  281. |       AP4_PrintInspector::AddField
  282. +---------------------------------------------------------------------*/
  283. void
  284. AP4_PrintInspector::AddField(const char* name, const char* value, FormatHint hint)
  285. {
  286.     char prefix[256];
  287.     AP4_MakePrefixString(m_Indent, prefix, sizeof(prefix));
  288.     m_Stream->WriteString(prefix);
  289.     
  290.     m_Stream->WriteString(name);
  291.     m_Stream->WriteString(" = ");
  292.     m_Stream->WriteString(value);
  293.     m_Stream->Write("n", 1);
  294. }
  295. /*----------------------------------------------------------------------
  296. |       AP4_PrintInspector::AddField
  297. +---------------------------------------------------------------------*/
  298. void
  299. AP4_PrintInspector::AddField(const char* name, AP4_UI32 value, FormatHint hint)
  300. {
  301.     char prefix[256];
  302.     AP4_MakePrefixString(m_Indent, prefix, sizeof(prefix));
  303.     m_Stream->WriteString(prefix);
  304.     char str[32];
  305.     AP4_StringFormat(str, sizeof(str), "%d", value);
  306.     m_Stream->WriteString(name);
  307.     m_Stream->WriteString(" = ");
  308.     m_Stream->WriteString(str);
  309.     m_Stream->Write("n", 1);
  310. }