summaryrefslogtreecommitdiffstats
path: root/debian/mp4v2/mp4v2-2.0.0~dfsg0/src/mp4util.cpp
blob: 47bd74ead7bb1825623904024bbbe758333dc239 (plain)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 * The Original Code is MPEG4IP.
 *
 * The Initial Developer of the Original Code is Cisco Systems Inc.
 * Portions created by Cisco Systems Inc. are
 * Copyright (C) Cisco Systems Inc. 2001-2005.  All Rights Reserved.
 *
 * Contributor(s):
 *      Dave Mackie     [email protected]
 *              Bill May                [email protected]
 */

#include "src/impl.h"

namespace mp4v2 { namespace impl {

///////////////////////////////////////////////////////////////////////////////

bool MP4NameFirstMatches(const char* s1, const char* s2)
{
    if (s1 == NULL || *s1 == '\0' || s2 == NULL || *s2 == '\0') {
        return false;
    }

    if (*s2 == '*') {
        return true;
    }

    while (*s1 != '\0') {
        if (*s2 == '\0' || strchr("[.", *s2)) {
            break;
        }
        if (tolower(*s1) != tolower(*s2)) {
            return false;
        }
        s1++;
        s2++;
    }
    return true;
}

bool MP4NameFirstIndex(const char* s, uint32_t* pIndex)
{
    if (s == NULL) {
        return false;
    }

    while (*s != '\0' && *s != '.') {
        if (*s == '[') {
            s++;
            ASSERT(pIndex);
            if (sscanf(s, "%u", pIndex) != 1) {
                return false;
            }
            return true;
        }
        s++;
    }
    return false;
}

char* MP4NameFirst(const char *s)
{
    if (s == NULL) {
        return NULL;
    }

    const char* end = s;

    while (*end != '\0' && *end != '.') {
        end++;
    }

    char* first = (char*)MP4Calloc((end - s) + 1);

    if (first) {
        strncpy(first, s, end - s);
    }

    return first;
}

const char* MP4NameAfterFirst(const char *s)
{
    if (s == NULL) {
        return NULL;
    }

    while (*s != '\0') {
        if (*s == '.') {
            s++;
            if (*s == '\0') {
                return NULL;
            }
            return s;
        }
        s++;
    }
    return NULL;
}

char* MP4ToBase16(const uint8_t* pData, uint32_t dataSize)
{
    if (dataSize) {
        ASSERT(pData);
    }
    uint32_t size = 2 * dataSize + 1;
    char* s = (char*)MP4Calloc(size);

    uint32_t i, j;
    for (i = 0, j = 0; i < dataSize; i++) {
        size -= snprintf(&s[j], size, "%02x", pData[i]);
        j += 2;
    }

    return s;   /* N.B. caller is responsible for free'ing s */
}

char* MP4ToBase64(const uint8_t* pData, uint32_t dataSize)
{
    if (pData == NULL || dataSize == 0) return NULL;

    static const char encoding[64] = {
        'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
        'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
        'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
        'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
    };

    char* s = (char*)MP4Calloc((((dataSize + 2) * 4) / 3) + 1);

    const uint8_t* src = pData;
    if (pData == NULL) return NULL;
    char* dest = s;
    uint32_t numGroups = dataSize / 3;

    for (uint32_t i = 0; i < numGroups; i++) {
        *dest++ = encoding[src[0] >> 2];
        *dest++ = encoding[((src[0] & 0x03) << 4) | (src[1] >> 4)];
        *dest++ = encoding[((src[1] & 0x0F) << 2) | (src[2] >> 6)];
        *dest++ = encoding[src[2] & 0x3F];
        src += 3;
    }

    if (dataSize % 3 == 1) {
        *dest++ = encoding[src[0] >> 2];
        *dest++ = encoding[((src[0] & 0x03) << 4)];
        *dest++ = '=';
        *dest++ = '=';
    } else if (dataSize % 3 == 2) {
        *dest++ = encoding[src[0] >> 2];
        *dest++ = encoding[((src[0] & 0x03) << 4) | (src[1] >> 4)];
        *dest++ = encoding[((src[1] & 0x0F) << 2)];
        *dest++ = '=';
    }
    *dest = '\0';
    return s;   /* N.B. caller is responsible for free'ing s */
}

static bool convertBase64 (const char data, uint8_t *value)
{
    static const uint8_t decodingarr64[128] = {
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
        0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
        0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
        0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
        0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
        0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
        0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
        0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
        0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
    };
    uint8_t index = (uint8_t)data;
    if ((index & 0x80) != 0) return false;

    if (decodingarr64[index] == 0xff) return false;
    *value = decodingarr64[index];
    return true;
}

uint8_t *Base64ToBinary (const char *pData, uint32_t decodeSize, uint32_t *pDataSize)
{
    uint8_t *ret;
    uint32_t size, ix, groups;
    if (pData == NULL ||  decodeSize == 0 || pDataSize == NULL)
        return NULL;

    if ((decodeSize % 4) != 0) {
        // must be multiples of 4 characters
        return NULL;
    }
    size = (decodeSize * 3) / 4;
    groups = decodeSize / 4;
    ret = (uint8_t *)MP4Calloc(size);
    if (ret == NULL) return NULL;
    for (ix = 0; ix < groups; ix++) {
        uint8_t value[4];
        for (uint8_t jx = 0; jx < 4; jx++) {
            if (pData[jx] == '=') {
                if (ix != (groups - 1)) {
                    free(ret);
                    return NULL;
                }
                size--;
                value[jx] = 0;
            } else if (convertBase64(pData[jx], &value[jx]) == false) {
                free(ret);
                return NULL;
            }
        }
        ret[(ix * 3)] = value[0] << 2 | ((value[1] >> 4) & 0x3);
        ret[(ix * 3) + 1] = (value[1] << 4) | (value[2] >> 2 & 0xf);
        ret[(ix * 3) + 2] = ((value[2] & 0x3) << 6) | value[3];
        pData += 4;
    }
    *pDataSize = size;
    return ret;
}

// log2 of value, rounded up
static uint8_t ilog2(uint64_t value)
{
    uint64_t powerOf2 = 1;
    for (uint8_t i = 0; i < 64; i++) {
        if (value <= powerOf2) {
            return i;
        }
        powerOf2 <<= 1;
    }
    return 64;
}

uint64_t MP4ConvertTime(uint64_t t,
                        uint32_t oldTimeScale, uint32_t newTimeScale)
{
    // avoid float point exception
    if (oldTimeScale == 0) {
        throw new Exception("division by zero", __FILE__, __LINE__, __FUNCTION__ );
    }

    if (oldTimeScale == newTimeScale) return t;

    // check if we can safely use integer operations
    if (ilog2(t) + ilog2(newTimeScale) <= 64) {
        return (t * newTimeScale) / oldTimeScale;
    }

    // final resort is to use floating point
    double d = (double)newTimeScale;
    d *= double(t);
    d /= (double)oldTimeScale;
    d += 0.5; // round up.

    return (uint64_t)d;
}

const char* MP4NormalizeTrackType (const char* type)
{
    if (!strcasecmp(type, "vide")
            || !strcasecmp(type, "video")
            || !strcasecmp(type, "mp4v")
            || !strcasecmp(type, "avc1")
            || !strcasecmp(type, "s263")  // 3GPP H.263
            || !strcasecmp(type, "encv")) {
        return MP4_VIDEO_TRACK_TYPE;
    }

    if (!strcasecmp(type, "soun")
            || !strcasecmp(type, "sound")
            || !strcasecmp(type, "audio")
            || !strcasecmp(type, "enca")
            || !strcasecmp(type, "samr")  // 3GPP AMR
            || !strcasecmp(type, "sawb")  // 3GPP AMR/WB
            || !strcasecmp(type, "mp4a")) {
        return MP4_AUDIO_TRACK_TYPE;
    }

    if (!strcasecmp(type, "sdsm")
            || !strcasecmp(type, "scene")
            || !strcasecmp(type, "bifs")) {
        return MP4_SCENE_TRACK_TYPE;
    }

    if (!strcasecmp(type, "odsm")
            || !strcasecmp(type, "od")) {
        return MP4_OD_TRACK_TYPE;
    }
    if (strcasecmp(type, "cntl") == 0) {
        return MP4_CNTL_TRACK_TYPE;
    }

    log.verbose1f("Attempt to normalize %s did not match",type);

    return type;
}

MP4Timestamp MP4GetAbsTimestamp() {
    /* MP4 epoch is midnight, January 1, 1904
     * offset from midnight, January 1, 1970 is 2082844800 seconds
     * 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60
     */
    return time::getLocalTimeSeconds() + 2082844800;
}

///////////////////////////////////////////////////////////////////////////////

uint32_t STRTOINT32( const char* s )
{
#if defined( MP4V2_INTSTRING_ALIGNMENT )
    // it seems ARM integer instructions require 4-byte alignment so we
    // manually copy string-data into the integer before performing ops
    uint32_t tmp;
    memcpy( &tmp, s, sizeof(tmp) );
    return MP4V2_NTOHL( tmp );
#else
    return MP4V2_NTOHL(*(uint32_t *)s);
#endif
}

void INT32TOSTR( uint32_t i, char* s )
{
#if defined( MP4V2_INTSTRING_ALIGNMENT )
    // it seems ARM integer instructions require 4-byte alignment so we
    // manually copy string-data into the integer before performing ops
    uint32_t tmp = MP4V2_HTONL( i );
    memcpy( s, &tmp, sizeof(tmp) );
#else
    *(uint32_t *)s = MP4V2_HTONL(i);
#endif
    s[4] = 0;
}

///////////////////////////////////////////////////////////////////////////////

}} // namespace mp4v2::impl