1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
/*****************************************************************
|
| AP4 - Utilities
|
| Copyright 2002-2008 Axiomatic Systems, LLC
|
|
| This file is part of Bento4/AP4 (MP4 Atom Processing Library).
|
| Unless you have obtained Bento4 under a difference license,
| this version of Bento4 is Bento4|GPL.
| Bento4|GPL is free software; you can redistribute it and/or modify
| it under the terms of the GNU General Public License as published by
| the Free Software Foundation; either version 2, or (at your option)
| any later version.
|
| Bento4|GPL is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
| GNU General Public License for more details.
|
| You should have received a copy of the GNU General Public License
| along with Bento4|GPL; see the file COPYING. If not, write to the
| Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
| 02111-1307, USA.
|
****************************************************************/
#ifndef _AP4_UTILS_H_
#define _AP4_UTILS_H_
/*----------------------------------------------------------------------
| includes
+---------------------------------------------------------------------*/
#include "Ap4Config.h"
#include "Ap4Types.h"
#include "Ap4Results.h"
#include "Ap4Config.h"
#include "Ap4List.h"
#include "Ap4String.h"
#include "Ap4DataBuffer.h"
/*----------------------------------------------------------------------
| global options
+---------------------------------------------------------------------*/
class AP4_GlobalOptions
{
public:
static bool GetBool(const char* name);
static void SetBool(const char* name, bool value);
private:
struct Entry {
AP4_String m_Name;
bool m_BoolValue;
};
static Entry* GetEntry(const char* name, bool autocreate);
static AP4_List<Entry>* g_Entries;
};
/*----------------------------------------------------------------------
| non-inline functions
+---------------------------------------------------------------------*/
double AP4_BytesToDoubleBE(const unsigned char* bytes);
AP4_UI64 AP4_BytesToUInt64BE(const unsigned char* bytes);
void AP4_BytesFromDoubleBE(unsigned char* bytes, double value);
void AP4_BytesFromUInt64BE(unsigned char* bytes, AP4_UI64 value);
/*----------------------------------------------------------------------
| AP4_BytesToUInt32BE
+---------------------------------------------------------------------*/
inline AP4_UI32
AP4_BytesToUInt32BE(const unsigned char* bytes)
{
return
( ((AP4_UI32)bytes[0])<<24 ) |
( ((AP4_UI32)bytes[1])<<16 ) |
( ((AP4_UI32)bytes[2])<<8 ) |
( ((AP4_UI32)bytes[3]) );
}
/*----------------------------------------------------------------------
| AP4_BytesToInt32BE
+---------------------------------------------------------------------*/
inline AP4_SI32
AP4_BytesToInt32BE(const unsigned char* bytes)
{
return AP4_BytesToUInt32BE(bytes);
}
/*----------------------------------------------------------------------
| AP4_BytesToUInt24BE
+---------------------------------------------------------------------*/
inline AP4_UI32
AP4_BytesToUInt24BE(const unsigned char* bytes)
{
return
( ((AP4_UI32)bytes[0])<<16 ) |
( ((AP4_UI32)bytes[1])<<8 ) |
( ((AP4_UI32)bytes[2]) );
}
/*----------------------------------------------------------------------
| AP4_BytesToInt16BE
+---------------------------------------------------------------------*/
inline AP4_UI16
AP4_BytesToUInt16BE(const unsigned char* bytes)
{
return
( ((AP4_UI16)bytes[0])<<8 ) |
( ((AP4_UI16)bytes[1]) );
}
/*----------------------------------------------------------------------
| AP4_BytesToInt16BE
+---------------------------------------------------------------------*/
inline AP4_SI16
AP4_BytesToInt16BE(const unsigned char* bytes)
{
return (AP4_SI16)AP4_BytesToUInt16BE(bytes);
}
/*----------------------------------------------------------------------
| AP4_BytesFromUInt32BE
+---------------------------------------------------------------------*/
inline void
AP4_BytesFromUInt32BE(unsigned char* bytes, AP4_UI32 value)
{
bytes[0] = (unsigned char)((value >> 24)&0xFF);
bytes[1] = (unsigned char)((value >> 16)&0xFF);
bytes[2] = (unsigned char)((value >> 8)&0xFF);
bytes[3] = (unsigned char)((value )&0xFF);
}
/*----------------------------------------------------------------------
| AP4_BytesFromUInt24BE
+---------------------------------------------------------------------*/
inline void
AP4_BytesFromUInt24BE(unsigned char* bytes, AP4_UI32 value)
{
bytes[0] = (unsigned char)((value >> 16)&0xFF);
bytes[1] = (unsigned char)((value >> 8)&0xFF);
bytes[2] = (unsigned char)((value )&0xFF);
}
/*----------------------------------------------------------------------
| AP4_BytesFromUInt16BE
+---------------------------------------------------------------------*/
inline void
AP4_BytesFromUInt16BE(unsigned char* bytes, AP4_UI16 value)
{
bytes[0] = (unsigned char)((value >> 8)&0xFF);
bytes[1] = (unsigned char)((value )&0xFF);
}
/*----------------------------------------------------------------------
| time functions
+---------------------------------------------------------------------*/
AP4_UI32 AP4_DurationMsFromUnits(AP4_UI64 units,
AP4_UI32 units_per_second);
AP4_UI64 AP4_ConvertTime(AP4_UI64 time_value,
AP4_UI32 from_time_scale,
AP4_UI32 to_time_scale);
/*----------------------------------------------------------------------
| random numbers
+---------------------------------------------------------------------*/
AP4_Result
AP4_System_GenerateRandomBytes(AP4_UI08* buffer, AP4_Size buffer_size);
/*----------------------------------------------------------------------
| string utils
+---------------------------------------------------------------------*/
#if defined (AP4_CONFIG_HAVE_STDIO_H)
#include <stdio.h>
#endif
#if defined (AP4_CONFIG_HAVE_SNPRINTF)
#define AP4_FormatString AP4_snprintf
#else
int AP4_FormatString(char* str, AP4_Size size, const char* format, ...);
#endif
#if defined(AP4_CONFIG_HAVE_VSNPRINTF)
#define AP4_FormatStringVN(s,c,f,a) AP4_vsnprintf(s,c,f,a)
#else
extern int AP4_FormatStringVN(char *buffer, size_t count, const char *format, va_list argptr);
#endif
#if defined (AP4_CONFIG_HAVE_STRING_H)
#include <string.h>
#define AP4_StringLength(x) strlen(x)
#define AP4_CopyMemory(x,y,z) memcpy(x,y,z)
#define AP4_CompareMemory(x, y, z) memcmp(x, y, z)
#define AP4_SetMemory(x,y,z) memset(x,y,z)
#define AP4_CompareStrings(x,y) strcmp(x,y)
#endif
unsigned char AP4_HexNibble(char c);
char AP4_NibbleHex(unsigned int nibble);
void AP4_FormatFourChars(char* str, AP4_UI32 value);
void AP4_FormatFourCharsPrintable(char* str, AP4_UI32 value);
AP4_Result
AP4_ParseHex(const char* hex, unsigned char* bytes, unsigned int count);
AP4_Result
AP4_FormatHex(const AP4_UI08* data, unsigned int data_size, char* hex);
AP4_Result
AP4_SplitArgs(char* arg, char*& arg0, char*& arg1, char*& arg2);
AP4_Result
AP4_SplitArgs(char* arg, char*& arg0, char*& arg1);
AP4_UI32
AP4_ParseIntegerU(const char* value);
/*----------------------------------------------------------------------
| AP4_BitWriter
+---------------------------------------------------------------------*/
class AP4_BitWriter
{
public:
AP4_BitWriter(AP4_Size size) : m_DataSize(size), m_BitCount(0) {
if (size) {
m_Data = new unsigned char[size];
AP4_SetMemory(m_Data, 0, size);
} else {
m_Data = NULL;
}
}
~AP4_BitWriter() { delete[] m_Data; }
void Write(AP4_UI32 bits, unsigned int bit_count);
unsigned int GetBitCount() { return m_BitCount; }
const unsigned char* GetData() { return m_Data; }
private:
unsigned char* m_Data;
unsigned int m_DataSize;
unsigned int m_BitCount;
};
/*----------------------------------------------------------------------
| AP4_BitReader
+---------------------------------------------------------------------*/
class AP4_BitReader
{
public:
// types
typedef unsigned int BitsWord;
// constructor and destructor
AP4_BitReader(const AP4_UI08* data, unsigned int data_size);
~AP4_BitReader();
// methods
AP4_Result Reset();
int ReadBit();
AP4_UI32 ReadBits(unsigned int bit_count);
int PeekBit();
AP4_UI32 PeekBits(unsigned int bit_count);
AP4_Result SkipBytes(AP4_Size byte_count);
void SkipBit();
void SkipBits(unsigned int bit_count);
AP4_UI32 BitsLeft();
private:
// methods
BitsWord ReadCache() const;
// members
AP4_DataBuffer m_Buffer;
unsigned int m_Position;
BitsWord m_Cache;
unsigned int m_BitsCached;
};
#endif // _AP4_UTILS_H_
|