IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Traduire en PHP du code C ou C++


Sujet :

Langage PHP

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    34
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2009
    Messages : 34
    Points : 27
    Points
    27
    Par défaut Traduire en PHP du code C ou C++
    Bonjour,

    - Peut-on traduire un code écrit en C ou C++ vers PHP ?
    - Si oui, connaissez-vous des outils permettant de le faire ?

    Merci.

  2. #2
    Membre éclairé Avatar de metagoto
    Profil pro
    Hobbyist programmateur
    Inscrit en
    Juin 2009
    Messages
    646
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Hobbyist programmateur

    Informations forums :
    Inscription : Juin 2009
    Messages : 646
    Points : 845
    Points
    845
    Par défaut
    Citation Envoyé par bernedef Voir le message
    - Peut-on traduire un code écrit en C ou C++ vers PHP ?
    Oui dans le sens où ses langages sont Turing-complete. Après, dans les faits, tout dépend de ce que les codes en C font puisqu'ils sont low level par nature alors que php est un langage de script: pas les mêmes facilités d'IO, d'accès aux ressources etc. php est implémenté en C. Ce que php peut faire, C peut le faire. L'inverse n'est pas vrai.

    Citation Envoyé par bernedef Voir le message
    - Si oui, connaissez-vous des outils permettant de le faire ?
    Le cerveau humain ?
    Des projets comme phc ( http://www.phpcompiler.org ) effectuent la transformation inverse (php -> C). Transformer du C en php automatiquement, je ne sais pas. Généralement, si la transfo n'est pas triviale, on va plutôt garder le code en C (ou C++) et écrire un wrapper autour en C de sorte d'en faire une extension pour php (un .so ou un .dll). Ca se fait à la main.

  3. #3
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    34
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2009
    Messages : 34
    Points : 27
    Points
    27
    Par défaut
    Merci pour ta réponse mais c'est déjà le code d'une extension php.

    Ce que je veux, c'est l'écrire en php directement car je ne peux pas ajouter d'extensions à php sur mon hébergement partagé.

    Je pense qu'il n'y a aura pas de problème d'incompatibilité pour convertir ce code c/c++ en PHP car c'est en fait un simple streamer de vidéos au format MP4/H.264/AAC.

    psstream.c

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    #ifdef HAVE_CONFIG_H
    #include "config.h"
    #endif
     
    #include <ctype.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    #include <time.h>
    #include <unistd.h>
     
    #include "php.h"
    #include "php_ini.h"
    #include "ext/standard/info.h"
    #include "php_psstream.h"
    #include "moov.h"
    #include "php_main.h"
    #include "SAPI.h"
     
    static int le_psstream;
     
    PHP_INI_BEGIN()
    PHP_INI_ENTRY("psstream.bandwidth_limit", "0", PHP_INI_ALL, NULL)
    PHP_INI_ENTRY("psstream.bandwidth_chunk_size", "204800", PHP_INI_ALL, NULL)
    PHP_INI_ENTRY("psstream.bandwidth_chunk_interval", "0.2", PHP_INI_ALL, NULL)
    PHP_INI_END()
     
    zend_function_entry psstream_functions[] = {
        PHP_FE(psstream_mp4,NULL)
        PHP_FE(psstream_flv,NULL)
        {NULL, NULL, NULL}
    };
     
    zend_module_entry psstream_module_entry = {
    #if ZEND_MODULE_API_NO >= 20010901
        STANDARD_MODULE_HEADER,
    #endif
        "psstream",
        psstream_functions,
        PHP_MINIT(psstream),
        PHP_MSHUTDOWN(psstream),
        PHP_RINIT(psstream),
        PHP_RSHUTDOWN(psstream),
        PHP_MINFO(psstream),
    #if ZEND_MODULE_API_NO >= 20010901
        "0.1",
    #endif
        STANDARD_MODULE_PROPERTIES
    };
     
    #ifdef COMPILE_DL_PSSTREAM
    ZEND_GET_MODULE(psstream)
    #endif
     
     
    // Startup/Shutdown functions
     
    PHP_MINIT_FUNCTION(psstream)
    {
        REGISTER_INI_ENTRIES();
        return SUCCESS;
    }
     
    PHP_MSHUTDOWN_FUNCTION(psstream)
    {
        UNREGISTER_INI_ENTRIES();
        return SUCCESS;
    }
     
    PHP_RINIT_FUNCTION(psstream)
    {
        return SUCCESS;
    }
     
    PHP_RSHUTDOWN_FUNCTION(psstream)
    {
        return SUCCESS;
    }
     
    PHP_MINFO_FUNCTION(psstream)
    {
        php_info_print_table_start();
        php_info_print_table_header(2, "psstream support", "enabled");
        php_info_print_table_row(2, "psstream version", "1.0");
        php_info_print_table_end();
     
        DISPLAY_INI_ENTRIES();
    }
     
     
    // Useful macros
     
    #define INIT_HEADERS sapi_header_line psstream_sapi_header_line = {0}
     
    #define ADD_HEADER(h) \
        psstream_sapi_header_line.line = h; \
        psstream_sapi_header_line.line_len = strlen(psstream_sapi_header_line.line); \
        sapi_header_op(SAPI_HEADER_ADD, &psstream_sapi_header_line);
     
    #define REPLACE_HEADER(h) \
        psstream_sapi_header_line.line = h; \
        psstream_sapi_header_line.line_len = strlen(psstream_sapi_header_line.line); \
        sapi_header_op(SAPI_HEADER_REPLACE, &psstream_sapi_header_line);
     
    #define SEND_HEADERS sapi_send_headers();
     
    #define STREAMING_ERROR(t) php_error(E_WARNING, "STREAMING ERROR: %s", t)
     
     
    // Helper functions
     
    #define ATOM_PREAMBLE_SIZE 8
     
    void write_char(unsigned char* outbuffer, int value) {
        outbuffer[0] = (unsigned char)(value);
    }
     
    void write_int32(unsigned char* outbuffer, long value) {
        outbuffer[0] = (unsigned char)((value >> 24) & 0xff);
        outbuffer[1] = (unsigned char)((value >> 16) & 0xff);
        outbuffer[2] = (unsigned char)((value >> 8) & 0xff);
        outbuffer[3] = (unsigned char)((value >> 0) & 0xff);
    }
     
    struct atom_t {
        unsigned char type_[4];
        uint64_t size_;
        uint64_t start_;
        uint64_t end_;
    };
     
    unsigned int atom_header_size(unsigned char* atom_bytes) {
        return (atom_bytes[0] << 24) +
               (atom_bytes[1] << 16) +
               (atom_bytes[2] << 8) +
               (atom_bytes[3]);
    }
     
    int atom_read_header(FILE* infile, struct atom_t* atom) {
        unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
     
        atom->start_ = ftell(infile);
     
        if (!fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile)) {
            return 0;
        }
     
        memcpy(&atom->type_[0], &atom_bytes[4], 4);
        atom->size_ = atom_header_size(atom_bytes);
        atom->end_ = atom->start_ + atom->size_;
     
        return 1;
    }
     
    void atom_write_header(unsigned char* outbuffer, struct atom_t* atom) {
        int i;
        write_int32(outbuffer, atom->size_);
     
        for(i = 0; i != 4; ++i) {
            write_char(outbuffer + 4 + i, atom->type_[i]);
        }
    }
     
    int atom_is(struct atom_t const* atom, const char* type) {
        return (atom->type_[0] == type[0] &&
                atom->type_[1] == type[1] &&
                atom->type_[2] == type[2] &&
                atom->type_[3] == type[3]);
    }
     
    void atom_skip(FILE* infile, struct atom_t const* atom) {
        fseek(infile, atom->end_, SEEK_SET);
    }
     
    void atom_print(struct atom_t const* atom) {
        printf("Atom(%c%c%c%c,%lld)\n", atom->type_[0], atom->type_[1],
            atom->type_[2], atom->type_[3], atom->size_);
    }
     
    double precise_time() {
        struct timeval tp = {0};
        struct timezone tz = {0};
     
        if (gettimeofday(&tp, &tz))
                return 0;
     
        return (double)(tp.tv_sec + tp.tv_usec) / 1000000.00;
    }
     
     
    // Main functions
     
    /* MP4 (H264) pseudo-streaming.
     * This function does nothing to check if the requested file is really a valid MP4/H264 file.
     */
    PHP_FUNCTION(psstream_mp4)
    {
        char *path;
        unsigned long path_size;
        double t_start = 0.0;
        double t_end = 0.0;
     
        if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|d", &path, &path_size, &t_start) == FAILURE)
            WRONG_PARAM_COUNT;
     
        if (t_start < 0) {
            php_error(E_WARNING, "Invalid start offset!");
            RETURN_FALSE;
        }
     
        FILE* infile;
        struct atom_t ftyp_atom;
        struct atom_t moov_atom;
        struct atom_t mdat_atom;
        unsigned char* moov_data = 0;
        unsigned char* ftyp_data = 0;
        struct stat filestat;
     
        if(VCWD_STAT(path, &filestat) || !(infile = VCWD_FOPEN(path, "rb"))) {
            php_error(E_WARNING, "Could not open file... [%s]", path);
            RETURN_FALSE;
        }
        unsigned long filesize = filestat.st_size;
     
        if (SG(headers_sent)) {
            php_error(E_WARNING, "Can not start streaming: headers were already sent!");
            RETURN_FALSE;
        }
     
        // Send H264 structure
        struct atom_t leaf_atom;
     
        while(ftell(infile) < filesize) {
            if(!atom_read_header(infile, &leaf_atom))
                break;
     
            atom_print(&leaf_atom);
     
            if(atom_is(&leaf_atom, "ftyp")) {
                ftyp_atom = leaf_atom;
                ftyp_data = malloc(ftyp_atom.size_);
                fseek(infile, ftyp_atom.start_, SEEK_SET);
                if (!fread(ftyp_data, ftyp_atom.size_, 1, infile)) {
                    STREAMING_ERROR("file read error");
                    RETURN_FALSE;
                }
            }
            else if(atom_is(&leaf_atom, "moov")) {
                moov_atom = leaf_atom;
                moov_data = malloc(moov_atom.size_);
                fseek(infile, moov_atom.start_, SEEK_SET);
                if (!fread(moov_data, moov_atom.size_, 1, infile)) {
                    STREAMING_ERROR("file read error");
                    RETURN_FALSE;
                }
            }
            else if(atom_is(&leaf_atom, "mdat")) {
                mdat_atom = leaf_atom;
            }
            atom_skip(infile, &leaf_atom);
        }
        fseek(infile, 0, SEEK_SET);
     
        if(!moov_data) {
            STREAMING_ERROR("null/empty moov_data");
            RETURN_FALSE;
        }
     
        unsigned int mdat_start = (ftyp_data ? ftyp_atom.size_ : 0) + moov_atom.size_;
     
        if(!moov_seek(moov_data,
                &moov_atom.size_,
                t_start, t_end,
                &mdat_atom.start_, &mdat_atom.size_,
                mdat_start - mdat_atom.start_)) {
            STREAMING_ERROR("moov_seek failed");
            RETURN_FALSE;
        }
     
        // Compute start/end file offsets
        unsigned long start = mdat_atom.start_ + ATOM_PREAMBLE_SIZE;
        unsigned long end = start + mdat_atom.size_ - ATOM_PREAMBLE_SIZE;
     
        // Send headers
        char last_modified[200];
        time_t t = time(NULL);
        struct tm *tmp;
        if (tmp = localtime(&t)) {
            strftime(last_modified, sizeof(last_modified), "Last-Modified: %a, %d %B %y %H:%M:%S GMT", tmp);
        }
     
        unsigned long delta = end - start;
        delta += ftyp_data ? ftyp_atom.size_ : 0;
        delta += moov_atom.size_;
        delta += ATOM_PREAMBLE_SIZE;
     
        char content_length[100];
        sprintf(content_length, "Content-Length: %lu", delta);
     
        INIT_HEADERS;
        ADD_HEADER("Content-Type: video/mp4");
        ADD_HEADER("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
        ADD_HEADER(last_modified);
        ADD_HEADER("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
        ADD_HEADER("Pragma: no-cache");
        ADD_HEADER(content_length);
        SEND_HEADERS;
     
        // Send meta atoms
        if(ftyp_data) {
            PHPWRITE(ftyp_data, ftyp_atom.size_);
            free(ftyp_data);
        }
     
        PHPWRITE(moov_data, moov_atom.size_);
        free(moov_data);
     
        unsigned char mdat_bytes[ATOM_PREAMBLE_SIZE];
        atom_write_header(mdat_bytes, &mdat_atom);
        PHPWRITE(mdat_bytes, ATOM_PREAMBLE_SIZE);
     
        // Configure options
        int bandwidth_limit = INI_BOOL("psstream.bandwidth_limit");
        unsigned long bandwidth_chunk_size = INI_INT("psstream.bandwidth_chunk_size");
        double bandwidth_chunk_interval = INI_FLT("psstream.bandwidth_chunk_interval");
     
        if (bandwidth_chunk_size < 1024) bandwidth_chunk_size = 1024;
        else if (bandwidth_chunk_size > 1048576) bandwidth_chunk_size = 1048576;
     
        if (bandwidth_chunk_interval < 0.1) bandwidth_chunk_interval = 0.1;
        else if (bandwidth_chunk_interval > 2) bandwidth_chunk_interval = 2;
     
        unsigned long chunk_size = bandwidth_limit ? bandwidth_chunk_size : 204800;
     
        // Set some ini settings
        zend_alter_ini_entry("session.cache_limiter", sizeof("session.cache_limiter"),
            "nocache", sizeof("nocache"), PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
     
        // Stream data
        double t_delta = 0.0;
        unsigned long result;
     
        unsigned char *buffer = (unsigned char*)malloc(chunk_size);
        if (!buffer) {
            RETURN_FALSE;
        }
     
        fseek(infile, start, SEEK_SET);
        while(start < end) {
     
            if (end - start < chunk_size) {
                chunk_size = end - start;
            }
     
            t_start = precise_time();
     
            result = fread(buffer, 1, chunk_size, infile);
     
            if (result != chunk_size) {
                free(buffer);
                RETURN_FALSE;
            }
     
            PHPWRITE(buffer, chunk_size);
            start += chunk_size;
     
            if(bandwidth_limit) {
                t_end = precise_time();
                t_delta = t_end - t_start;
     
                if(t_delta < bandwidth_chunk_interval) {
                    usleep(bandwidth_chunk_interval * 1000000 - t_delta * 1000000);
                }
            }
        }
     
        // Close file
        fclose(infile);
        free(buffer);
     
        RETURN_TRUE;
    }
     
    /* Classic FLV pseudo-streaming.
     * This function does nothing to check if the requested file is really a valid FLV file.
     */
    PHP_FUNCTION(psstream_flv)
    {
        #define FLV_SIGNATURE "FLV\x1\x1\0\0\0\x9\0\0\0\x9"
     
        char *path;
        unsigned long path_size;
        unsigned long start = 0;
     
        if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &path, &path_size, &start) == FAILURE)
            WRONG_PARAM_COUNT;
     
        FILE* infile;
        struct stat filestat;
     
        if(VCWD_STAT(path, &filestat) || !(infile = VCWD_FOPEN(path, "rb"))) {
            php_error(E_WARNING, "Could not open file... [%s]", path);
            RETURN_FALSE;
        }
        unsigned long end = filestat.st_size;
     
        if (start < 0 || start >= end) {
            php_error(E_WARNING, "Invalid start offset!");
            RETURN_FALSE;
        }
     
        if (SG(headers_sent)) {
            php_error(E_WARNING, "Can not start streaming: headers were already sent!");
            RETURN_FALSE;
        }
     
        // Send headers
        char last_modified[200];
        time_t t = time(NULL);
        struct tm *tmp;
        if (tmp = localtime(&t)) {
            strftime(last_modified, sizeof(last_modified), "Last-Modified: %a, %d %B %y %H:%M:%S GMT", tmp);
        }
     
        char content_length[100];
        sprintf(content_length, "Content-Length: %lu", end - start + sizeof(FLV_SIGNATURE) - 1);
     
        INIT_HEADERS;
        ADD_HEADER("Content-Type: video/x-flv");
        ADD_HEADER("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
        ADD_HEADER(last_modified);
        ADD_HEADER("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
        ADD_HEADER("Pragma: no-cache");
        ADD_HEADER(content_length);
        SEND_HEADERS;
     
        // Send FLV signature
        if (start > 0) {
            PHPWRITE(FLV_SIGNATURE, sizeof(FLV_SIGNATURE) - 1);
        }
     
        // Configure options
        int bandwidth_limit = INI_BOOL("psstream.bandwidth_limit");
        unsigned long bandwidth_chunk_size = INI_INT("psstream.bandwidth_chunk_size");
        double bandwidth_chunk_interval = INI_FLT("psstream.bandwidth_chunk_interval");
     
        if (bandwidth_chunk_size < 1024) bandwidth_chunk_size = 1024;
        else if (bandwidth_chunk_size > 1048576) bandwidth_chunk_size = 1048576;
     
        if (bandwidth_chunk_interval < 0.1) bandwidth_chunk_interval = 0.1;
        else if (bandwidth_chunk_interval > 2) bandwidth_chunk_interval = 2;
     
        unsigned long chunk_size = bandwidth_limit ? bandwidth_chunk_size : 204800;
     
        // Set some ini settings
        zend_alter_ini_entry("session.cache_limiter", sizeof("session.cache_limiter"),
            "nocache", sizeof("nocache"), PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
     
        // Stream data
        double t_start = 0.0;
        double t_end = 0.0;
        double t_delta = 0.0;
        unsigned long result;
     
        unsigned char *buffer = (unsigned char*)malloc(chunk_size);
        if (!buffer) {
            RETURN_FALSE;
        }
     
        fseek(infile, start, SEEK_SET);
        while(start < end) {
     
            if (end - start < chunk_size) {
                chunk_size = end - start;
            }
     
            t_start = precise_time();
     
            result = fread(buffer, 1, chunk_size, infile);
     
            if (result != chunk_size) {
                free(buffer);
                RETURN_FALSE;
            }
     
            PHPWRITE(buffer, chunk_size);
            start += chunk_size;
     
            if(bandwidth_limit) {
                t_end = precise_time();
                t_delta = t_end - t_start;
     
                if(t_delta < bandwidth_chunk_interval) {
                    usleep(bandwidth_chunk_interval * 1000000 - t_delta * 1000000);
                }
            }
        }
     
        // Close file
        fclose(infile);
        free(buffer);
     
        RETURN_TRUE;
    }
    moov.c

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    791
    792
    793
    794
    795
    796
    797
    798
    799
    800
    801
    802
    803
    804
    805
    806
    807
    808
    809
    810
    811
    812
    813
    814
    815
    816
    817
    818
    819
    820
    821
    822
    823
    824
    825
    826
    827
    828
    829
    830
    831
    832
    833
    834
    835
    836
    837
    838
    839
    840
    841
    842
    843
    844
    845
    846
    847
    848
    849
    850
    851
    852
    853
    854
    855
    856
    857
    858
    859
    860
    861
    862
    863
    864
    865
    866
    867
    868
    869
    870
    871
    872
    873
    874
    875
    876
    877
    878
    879
    880
    881
    882
    883
    884
    885
    886
    887
    888
    889
    890
    891
    892
    893
    894
    895
    896
    897
    898
    899
    900
    901
    902
    903
    904
    905
    906
    907
    908
    909
    910
    911
    912
    913
    914
    915
    916
    917
    918
    919
    920
    921
    922
    923
    924
    925
    926
    927
    928
    929
    930
    931
    932
    933
    934
    935
    936
    937
    938
    939
    940
    941
    942
    943
    944
    945
    946
    947
    948
    949
    950
    951
    952
    953
    954
    955
    956
    957
    958
    959
    960
    961
    962
    963
    964
    965
    966
    967
    968
    969
    970
    971
    972
    973
    974
    975
    976
    977
    978
    979
    980
    981
    982
    983
    984
    985
    986
    987
    988
    989
    990
    991
    992
    993
    994
    995
    996
    997
    998
    999
    1000
    1001
    1002
    1003
    1004
    1005
    1006
    1007
    1008
    1009
    1010
    1011
    1012
    1013
    1014
    1015
    1016
    1017
    1018
    1019
    1020
    1021
    1022
    1023
    1024
    1025
    1026
    1027
    1028
    1029
    1030
    1031
    1032
    1033
    1034
    1035
    1036
    1037
    1038
    1039
    1040
    1041
    1042
    1043
    1044
    1045
    1046
    1047
    1048
    1049
    1050
    1051
    1052
    1053
    1054
    1055
    1056
    1057
    1058
    1059
    1060
    1061
    1062
    1063
    1064
    1065
    1066
    1067
    1068
    1069
    1070
    1071
    1072
    1073
    1074
    1075
    1076
    1077
    1078
    1079
    1080
    1081
    1082
    1083
    1084
    1085
    1086
    1087
    1088
    1089
    1090
    1091
    1092
    1093
    1094
    1095
    1096
    1097
    1098
    1099
    1100
    1101
    1102
    1103
    1104
    1105
    1106
    1107
    1108
    1109
    1110
    1111
    1112
    1113
    1114
    1115
    1116
    1117
    1118
    1119
    1120
    1121
    1122
    1123
    1124
    1125
    1126
    1127
    1128
    1129
    1130
    1131
    1132
    1133
    1134
    1135
    1136
    1137
    1138
    1139
    1140
    1141
    1142
    1143
    1144
    1145
    1146
    1147
    1148
    1149
    1150
    1151
    1152
    1153
    1154
    1155
    1156
    1157
    1158
    1159
    1160
    1161
    1162
    1163
    1164
    1165
    1166
    1167
    1168
    1169
    1170
    1171
    1172
    1173
    1174
    1175
    1176
    1177
    1178
    1179
    1180
    1181
    1182
    1183
    1184
    1185
    1186
    1187
    1188
    1189
    1190
    1191
    1192
    1193
    1194
    1195
    1196
    1197
    1198
    1199
    1200
    1201
    1202
    1203
    1204
    1205
    1206
    1207
    1208
    1209
    1210
    1211
    1212
    1213
    1214
    1215
    1216
    1217
    1218
    1219
    1220
    1221
    1222
    1223
    1224
    1225
    1226
    1227
    1228
    1229
    1230
    1231
    1232
    1233
    1234
    1235
    1236
    1237
    1238
    1239
    1240
    1241
    1242
    1243
    1244
    1245
    1246
    1247
    1248
    1249
    1250
    1251
    1252
    1253
    1254
    1255
    1256
    1257
    1258
    1259
    1260
    1261
    1262
    1263
    1264
    1265
    1266
    1267
    1268
    1269
    1270
    1271
    1272
    1273
    1274
    1275
    1276
    1277
    1278
    1279
    1280
    1281
    1282
    1283
    1284
    1285
    1286
    1287
    1288
    1289
    1290
    1291
    1292
    1293
    1294
    1295
    1296
    1297
    1298
    1299
    1300
    1301
    1302
    1303
    1304
    1305
    1306
    1307
    1308
    1309
    1310
    1311
    1312
    1313
    1314
    1315
    1316
    1317
    1318
    1319
    1320
    1321
    1322
    1323
    1324
    1325
    1326
    1327
    1328
    1329
    1330
    1331
    1332
    1333
    1334
    1335
    1336
    1337
    1338
    1339
    1340
    1341
    1342
    1343
    1344
    1345
    1346
    1347
    1348
    1349
    1350
    1351
    1352
    1353
    1354
    1355
    1356
    1357
    1358
    1359
    1360
    1361
    1362
    1363
    1364
    1365
    1366
    1367
    1368
    1369
    1370
    1371
    1372
    1373
    1374
    1375
    1376
    1377
    1378
    1379
    1380
    1381
    1382
    1383
    1384
    1385
    1386
    1387
    1388
    1389
    1390
    1391
    1392
    1393
    1394
    1395
    1396
    1397
    1398
    1399
    1400
    1401
    1402
    1403
    1404
    1405
    1406
    1407
    1408
    1409
    1410
    1411
    1412
    1413
    1414
    1415
    1416
    /* 
      Uses code snippets from the libquicktime library:
       http://libquicktime.sourceforge.net
     
      The QuickTime File Format PDF from Apple:
        http://developer.apple.com/techpubs/quicktime/qtdevdocs/PDF/QTFileFormat.pdf
    */
     
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <limits.h>
    #include <stdint.h>
     
    #ifdef HAVE_CONFIG_H
    #include "config.h"
    #endif
     
    #ifdef HAVE_STDINT_H
    # include <stdint.h>
    #endif
    #ifdef HAVE_INTTYPES_H
    # include <inttypes.h>
    #endif
    #ifdef HAVE_ZLIB_H
    // Compress the MOOV atom. Turn this off for Flash as it doesn't support it.
    // # define COMPRESS_MOOV_ATOM
    # include <zlib.h>
    #endif
     
    static int read_char(unsigned char const* buffer)
    {
      return buffer[0];
    }
     
    static unsigned int read_int32(void const* buffer)
    {
      unsigned char* p = (unsigned char*)buffer;
      return (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3];
    }
     
    static void write_int32(void* outbuffer, uint32_t value)
    {
      unsigned char* p = (unsigned char*)outbuffer;
      p[0] = (unsigned char)((value >> 24) & 0xff);
      p[1] = (unsigned char)((value >> 16) & 0xff);
      p[2] = (unsigned char)((value >> 8) & 0xff);
      p[3] = (unsigned char)((value >> 0) & 0xff);
    }
     
    static void write_int64(void* outbuffer, uint64_t value)
    {
      unsigned char* p = (unsigned char*)outbuffer;
      write_int32(p + 0, (uint32_t)(value >> 32));
      write_int32(p + 4, (uint32_t)(value >>  0));
    }
     
    struct atom_t
    {
      unsigned char type_[4];
      unsigned int size_;
      unsigned char* start_;
      unsigned char* end_;
    };
     
    #define ATOM_PREAMBLE_SIZE 8
     
    static unsigned int atom_header_size(unsigned char* atom_bytes)
    {
      return (atom_bytes[0] << 24) +
             (atom_bytes[1] << 16) +
             (atom_bytes[2] << 8) +
             (atom_bytes[3]);
    }
     
    static unsigned char* atom_read_header(unsigned char* buffer, struct atom_t* atom)
    {
      atom->start_ = buffer;
      memcpy(&atom->type_[0], &buffer[4], 4);
      atom->size_ = atom_header_size(buffer);
      atom->end_ = atom->start_ + atom->size_;
     
      return buffer + ATOM_PREAMBLE_SIZE;
    }
     
    static unsigned char* atom_skip(unsigned char* buffer, struct atom_t const* atom)
    {
      return atom->end_;
    }
     
    static int atom_is(struct atom_t const* atom, const char* type)
    {
      return (atom->type_[0] == type[0] &&
              atom->type_[1] == type[1] &&
              atom->type_[2] == type[2] &&
              atom->type_[3] == type[3])
             ;
    }
     
    static void atom_print(struct atom_t const* atom)
    {
      printf("Atom(%c%c%c%c,%d)\n", atom->type_[0], atom->type_[1],
              atom->type_[2], atom->type_[3], atom->size_);
    }
     
    #define MAX_TRACKS 8
     
    unsigned int stts_get_entries(unsigned char const* stts)
    {
      return read_int32(stts + 4);
    }
     
    void stts_get_sample_count_and_duration(unsigned char const* stts,
      unsigned int idx, unsigned int* sample_count, unsigned int* sample_duration)
    {
      unsigned char const* table = stts + 8 + idx * 8;
      *sample_count = read_int32(table);
      *sample_duration = read_int32(table + 4);
    }
     
    struct stts_table_t
    {
      uint32_t sample_count_;
      uint32_t sample_duration_;
    };
     
    unsigned int ctts_get_entries(unsigned char const* ctts)
    {
      return read_int32(ctts + 4);
    }
     
    void ctts_get_sample_count_and_offset(unsigned char const* ctts,
      unsigned int idx, unsigned int* sample_count, unsigned int* sample_offset)
    {
      unsigned char const* table = ctts + 8 + idx * 8;
      *sample_count = read_int32(table);
      *sample_offset = read_int32(table + 4);
    }
     
    unsigned int ctts_get_samples(unsigned char const* ctts)
    {
      unsigned int samples = 0;
      unsigned int entries = ctts_get_entries(ctts);
      unsigned int i;
      for(i = 0; i != entries; ++i)
      {
        unsigned int sample_count;
        unsigned int sample_offset;
        ctts_get_sample_count_and_offset(ctts, i, &sample_count, &sample_offset);
        samples += sample_count;
      }
     
      return samples;
    }
     
    struct ctts_table_t
    {
      uint32_t sample_count_;
      uint32_t sample_offset_;
    };
     
    struct stsc_table_t
    {
      uint32_t chunk_;
      uint32_t samples_;
      uint32_t id_;
    };
     
    unsigned int stsc_get_entries(unsigned char const* stsc)
    {
      return read_int32(stsc + 4);
    }
     
    void stsc_get_table(unsigned char const* stsc, unsigned int i, struct stsc_table_t *stsc_table)
    {
      struct stsc_table_t* table = (struct stsc_table_t*)(stsc + 8);
      stsc_table->chunk_ = read_int32(&table[i].chunk_) - 1;
      stsc_table->samples_ = read_int32(&table[i].samples_);
      stsc_table->id_ = read_int32(&table[i].id_);
    }
     
    unsigned int stsc_get_chunk(unsigned char* stsc, unsigned int sample)
    {
      unsigned int entries = read_int32(stsc + 4);
      struct stsc_table_t* table = (struct stsc_table_t*)(stsc + 8);
     
      if(entries == 0)
      {
        return 0;
      }
      else
    //  if(entries == 1)
    //  {
    //    unsigned int table_samples = read_int32(&table[0].samples_);
    //    unsigned int chunk = (sample + 1) / table_samples;
    //    return chunk - 1;
    //  }
    //  else
      {
        unsigned int total = 0;
        unsigned int chunk1 = 1;
        unsigned int chunk1samples = 0;
        unsigned int chunk2entry = 0;
        unsigned int chunk, chunk_sample;
     
        do
        {
          unsigned int range_samples;
          unsigned int chunk2 = read_int32(&table[chunk2entry].chunk_);
          chunk = chunk2 - chunk1;
          range_samples = chunk * chunk1samples;
     
          if(sample < total + range_samples)
            break;
     
          chunk1samples = read_int32(&table[chunk2entry].samples_);
          chunk1 = chunk2;
     
          if(chunk2entry < entries)
          {
            chunk2entry++;
            total += range_samples;
          }
        } while(chunk2entry < entries);
     
        if(chunk1samples)
        {
          unsigned int sample_in_chunk = (sample - total) % chunk1samples;
          if(sample_in_chunk != 0)
          {
            printf("ERROR: sample must be chunk aligned: %d\n", sample_in_chunk);
          }
          chunk = (sample - total) / chunk1samples + chunk1;
        }
        else
          chunk = 1;
     
        chunk_sample = total + (chunk - chunk1) * chunk1samples;
     
        return chunk;
      }
    }
     
    unsigned int stsc_get_samples(unsigned char* stsc)
    {
      unsigned int entries = read_int32(stsc + 4);
      struct stsc_table_t* table = (struct stsc_table_t*)(stsc + 8);
      unsigned int samples = 0;
      unsigned int i;
      for(i = 0; i != entries; ++i)
      {
        samples += read_int32(&table[i].samples_);
      }
      return samples;
    }
     
    unsigned int stco_get_entries(unsigned char const* stco)
    {
      return read_int32(stco + 4);
    }
     
    uint32_t stco_get_offset(unsigned char const* stco, int idx)
    {
      uint32_t const* table = (uint32_t const*)(stco + 8);
      return read_int32(&table[idx]);
    }
     
    #if 0
    void stco_erase(unsigned char* stco, unsigned int chunks_to_delete)
    {
      int entries = read_int32(stco + 4);
      long* table = (long*)(stco + 8);
    //  unsigned int bytes_to_skip;
     
    //  // TODO: remove up to the last chunk. This is problematic as we don't know
    //  // the size of the last chunk. For now, we leave the last chunk in 'mdat'.
    //  unsigned int stco_end = chunks_to_delete;
    //  if(stco_end == entries && entries)
    //    --stco_end;
     
    //  bytes_to_skip = read_int32(&table[stco_end]) - read_int32(&table[0]);
     
      memmove(&table[0], &table[chunks_to_delete],
              (entries - chunks_to_delete) * sizeof(long));
     
      write_int32(stco + 4, entries - chunks_to_delete);
     
    //  return bytes_to_skip;
    }
    #endif
     
    unsigned int stsz_get_sample_size(unsigned char const* stsz)
    {
      return read_int32(stsz + 4);
    }
     
    unsigned int stsz_get_entries(unsigned char const* stsz)
    {
      return read_int32(stsz + 8);
    }
     
    unsigned int stsz_get_size(unsigned char const* stsz, unsigned int idx)
    {
      uint32_t const* table = (uint32_t const*)(stsz + 12);
      return read_int32(&table[idx]);
    }
     
    uint64_t stts_get_duration(unsigned char const* stts)
    {
      long duration = 0;
      unsigned int entries = stts_get_entries(stts);
      unsigned int i;
      for(i = 0; i != entries; ++i)
      {
        unsigned int sample_count;
        unsigned int sample_duration;
        stts_get_sample_count_and_duration(stts, i,
                                           &sample_count, &sample_duration);
        duration += sample_duration * sample_count;
      }
     
      return duration;
    }
     
    unsigned int stts_get_samples(unsigned char const* stts)
    {
      unsigned int samples = 0;
      unsigned int entries = stts_get_entries(stts);
      unsigned int i;
      for(i = 0; i != entries; ++i)
      {
        unsigned int sample_count;
        unsigned int sample_duration;
        stts_get_sample_count_and_duration(stts, i,
                                           &sample_count, &sample_duration);
        samples += sample_count;
      }
     
      return samples;
    }
     
    unsigned int stts_get_sample(unsigned char const* stts, unsigned int time)
    {
      unsigned int stts_index = 0;
      unsigned int stts_count;
     
      unsigned int ret = 0;
      unsigned int time_count = 0;
     
      unsigned int entries = stts_get_entries(stts);
      for(; stts_index != entries; ++stts_index)
      {
        unsigned int sample_count;
        unsigned int sample_duration;
        stts_get_sample_count_and_duration(stts, stts_index,
                                           &sample_count, &sample_duration);
        if(time_count + sample_duration * sample_count >= time)
        {
          stts_count = (time - time_count) / sample_duration;
          time_count += stts_count * sample_duration;
          ret += stts_count;
          break;
        }
        else
        {
          time_count += sample_duration * sample_count;
          ret += sample_count;
    //      stts_index++;
        }
    //    if(stts_index >= table_.size())
    //      break;
      }
    //  *time = time_count;
      return ret;
    }
     
    unsigned int stts_get_time(unsigned char const* stts, unsigned int sample)
    {
      unsigned int ret = 0;
      unsigned int stts_index = 0;
      unsigned int sample_count = 0;
     
      for(;;)
      {
        unsigned int table_sample_count;
        unsigned int table_sample_duration;
        stts_get_sample_count_and_duration(stts, stts_index,
                                           &table_sample_count, &table_sample_duration);
     
        if(sample_count + table_sample_count > sample)
        {
          unsigned int stts_count = (sample - sample_count);
          ret += stts_count * table_sample_duration;
          break;
        }
        else
        {
          sample_count += table_sample_count;
          ret += table_sample_count * table_sample_duration;
          stts_index++;
        }
      }
      return ret;
    }
     
     
    struct stbl_t
    {
      unsigned char* start_;
    //stsd stsd_;               // sample description
      unsigned char* stts_;     // decoding time-to-sample
      unsigned char* stss_;     // sync sample
      unsigned char* stsc_;     // sample-to-chunk
      unsigned char* stsz_;     // sample size
      unsigned char* stco_;     // chunk offset
      unsigned char* ctts_;     // composition time-to-sample
    };
     
    void stbl_parse(struct stbl_t* stbl, unsigned char* buffer, unsigned int size)
    {
      struct atom_t leaf_atom;
      unsigned char* buffer_start = buffer;
      stbl->stss_ = 0;
      stbl->ctts_ = 0;
     
      stbl->start_ = buffer;
     
      while(buffer < buffer_start + size)
      {
        buffer = atom_read_header(buffer, &leaf_atom);
     
        atom_print(&leaf_atom);
     
        if(atom_is(&leaf_atom, "stts"))
        {
          stbl->stts_ = buffer;
        }
        else
        if(atom_is(&leaf_atom, "stss"))
        {
          stbl->stss_ = buffer;
        }
        else
        if(atom_is(&leaf_atom, "stsc"))
        {
          stbl->stsc_ = buffer;
        }
        else
        if(atom_is(&leaf_atom, "stsz"))
        {
          stbl->stsz_ = buffer;
        }
        else
        if(atom_is(&leaf_atom, "stco"))
        {
          stbl->stco_ = buffer;
        }
        else
        if(atom_is(&leaf_atom, "co64"))
        {
          perror("TODO: co64");
        }
        else
        if(atom_is(&leaf_atom, "ctts"))
        {
          stbl->ctts_ = buffer;
        }
     
        buffer = atom_skip(buffer, &leaf_atom);
      }
    }
     
    struct minf_t
    {
      unsigned char* start_;
      struct stbl_t stbl_;
    };
     
    void minf_parse(struct minf_t* minf, unsigned char* buffer, unsigned int size)
    {
      struct atom_t leaf_atom;
      unsigned char* buffer_start = buffer;
     
      minf->start_ = buffer;
     
      while(buffer < buffer_start + size)
      {
        buffer = atom_read_header(buffer, &leaf_atom);
     
        atom_print(&leaf_atom);
     
        if(atom_is(&leaf_atom, "stbl"))
        {
          stbl_parse(&minf->stbl_, buffer, leaf_atom.size_ - ATOM_PREAMBLE_SIZE);
        }
     
        buffer = atom_skip(buffer, &leaf_atom);
      }
    }
     
    struct mdia_t
    {
      unsigned char* start_;
      unsigned char* mdhd_;
      struct minf_t minf_;
    //  hdlr hdlr_;
    };
     
    void mdia_parse(struct mdia_t* mdia, unsigned char* buffer, unsigned int size)
    {
      struct atom_t leaf_atom;
      unsigned char* buffer_start = buffer;
     
      mdia->start_ = buffer;
     
      while(buffer < buffer_start + size)
      {
        buffer = atom_read_header(buffer, &leaf_atom);
     
        atom_print(&leaf_atom);
     
        if(atom_is(&leaf_atom, "mdhd"))
        {
          mdia->mdhd_ = buffer;
        }
        else
        if(atom_is(&leaf_atom, "minf"))
        {
          minf_parse(&mdia->minf_, buffer, leaf_atom.size_ - ATOM_PREAMBLE_SIZE);
        }
     
        buffer = atom_skip(buffer, &leaf_atom);
      }
    }
     
    struct chunks_t
    {
      unsigned int sample_;   // number of the first sample in the chunk
      unsigned int size_;     // number of samples in the chunk
      int id_;                // for multiple codecs mode - not used
      uint64_t pos_;          // start byte position of chunk
    };
     
    struct samples_t
    {
      unsigned int pts_;      // decoding/presentation time
      unsigned int size_;     // size in bytes
      uint64_t pos_;          // byte offset
      unsigned int cto_;      // composition time offset
    };
     
    struct trak_t
    {
      unsigned char* start_;
      unsigned char* tkhd_;
      struct mdia_t mdia_;
     
      /* temporary indices */
      unsigned int chunks_size_;
      struct chunks_t* chunks_;
     
      unsigned int samples_size_;
      struct samples_t* samples_;
    };
     
    void trak_init(struct trak_t* trak)
    {
      trak->chunks_ = 0;
      trak->samples_ = 0;
    }
     
    void trak_exit(struct trak_t* trak)
    {
      if(trak->chunks_)
          free(trak->chunks_);
      if(trak->samples_)
        free(trak->samples_);
    }
     
    void trak_parse(struct trak_t* trak, unsigned char* buffer, unsigned int size)
    {
      struct atom_t leaf_atom;
      unsigned char* buffer_start = buffer;
     
      trak->start_ = buffer;
     
      while(buffer < buffer_start + size)
      {
        buffer = atom_read_header(buffer, &leaf_atom);
     
        atom_print(&leaf_atom);
     
        if(atom_is(&leaf_atom, "tkhd"))
        {
          trak->tkhd_ = buffer;
        }
        else
        if(atom_is(&leaf_atom, "mdia"))
        {
          mdia_parse(&trak->mdia_, buffer, leaf_atom.size_ - ATOM_PREAMBLE_SIZE);
        }
     
        buffer = atom_skip(buffer, &leaf_atom);
      }
    }
     
    struct moov_t
    {
      unsigned char* start_;
      unsigned int tracks_;
      unsigned char* mvhd_;
      struct trak_t traks_[MAX_TRACKS];
    };
     
    void moov_init(struct moov_t* moov)
    {
      moov->tracks_ = 0;
    }
     
    void moov_exit(struct moov_t* moov)
    {
      unsigned int i;
      for(i = 0; i != moov->tracks_; ++i)
      {
        trak_exit(&moov->traks_[i]);
      }
    }
     
    void trak_build_index(struct trak_t* trak)
    {
      void const* stco = trak->mdia_.minf_.stbl_.stco_;
     
      trak->chunks_size_ = stco_get_entries(stco);
      trak->chunks_ = malloc(trak->chunks_size_ * sizeof(struct chunks_t));
     
      {
        unsigned int i;
        for(i = 0; i != trak->chunks_size_; ++i)
        {
          trak->chunks_[i].pos_ = stco_get_offset(stco, i);
        }
      }
     
      // process chunkmap:
      {
        void const* stsc = trak->mdia_.minf_.stbl_.stsc_;
        unsigned int last = trak->chunks_size_;
        unsigned int i = stsc_get_entries(stsc);
        while(i > 0)
        {
          struct stsc_table_t stsc_table;
          unsigned int j;
     
          --i;
     
          stsc_get_table(stsc, i, &stsc_table);
          for(j = stsc_table.chunk_; j < last; j++)
          {
            trak->chunks_[j].id_ = stsc_table.id_;
            trak->chunks_[j].size_ = stsc_table.samples_;
          }
          last = stsc_table.chunk_;
        }
      }
     
      // calc pts of chunks:
      {
        void const* stsz = trak->mdia_.minf_.stbl_.stsz_;
        unsigned int sample_size = stsz_get_sample_size(stsz);
        unsigned int s = 0;
        {
          unsigned int j;
          for(j = 0; j < trak->chunks_size_; j++)
          {
            trak->chunks_[j].sample_ = s;
            s += trak->chunks_[j].size_;
          }
        }
     
        if(sample_size == 0)
        {
          trak->samples_size_ = stsz_get_entries(stsz);
        }
        else
        {
          trak->samples_size_ = s;
        }
     
        trak->samples_ = malloc(trak->samples_size_ * sizeof(struct samples_t));
     
        if(sample_size == 0)
        {
          unsigned int i;
          for(i = 0; i != trak->samples_size_ ; ++i)
            trak->samples_[i].size_ = stsz_get_size(stsz, i);
        }
        else
        {
          unsigned int i;
          for(i = 0; i != trak->samples_size_ ; ++i)
            trak->samples_[i].size_ = sample_size;
        }
      }
     
    //  i = 0;
    //  for (j = 0; j < trak->durmap_size; j++)
    //    i += trak->durmap[j].num;
    //  if (i != s) {
    //    mp_msg(MSGT_DEMUX, MSGL_WARN,
    //           "MOV: durmap and chunkmap sample count differ (%i vs %i)\n", i, s);
    //    if (i > s) s = i;
    //  }
     
      // calc pts:
      {
        void const* stts = trak->mdia_.minf_.stbl_.stts_;
        unsigned int s = 0;
        unsigned int pts = 0;
        unsigned int entries = stts_get_entries(stts);
        unsigned int j;
        for(j = 0; j < entries; j++)
        {
          unsigned int i;
          unsigned int sample_count;
          unsigned int sample_duration;
          stts_get_sample_count_and_duration(stts, j,
                                             &sample_count, &sample_duration);
          for(i = 0; i < sample_count; i++)
          {
            trak->samples_[s].pts_ = pts;
            ++s;
            pts += sample_duration;
          }
        }
      }
     
      // calc composition times:
      {
        void const* ctts = trak->mdia_.minf_.stbl_.ctts_;
        if(ctts)
        {
          unsigned int s = 0;
          unsigned int entries = ctts_get_entries(ctts);
          unsigned int j;
          for(j = 0; j < entries; j++)
          {
            unsigned int i;
            unsigned int sample_count;
            unsigned int sample_offset;
            ctts_get_sample_count_and_offset(ctts, j, &sample_count, &sample_offset);
            for(i = 0; i < sample_count; i++)
            {
              trak->samples_[s].cto_ = sample_offset;
              ++s;
            }
          }
        }
      }
     
      // calc sample offsets
      {
        unsigned int s = 0;
        unsigned int j;
        for(j = 0; j != trak->chunks_size_; j++)
        {
          uint64_t pos = trak->chunks_[j].pos_;
          unsigned int i;
          for(i = 0; i != trak->chunks_[j].size_; i++)
          {
            trak->samples_[s].pos_ = pos;
            pos += trak->samples_[s].size_;
            ++s;
          }
        }
      }
    }
     
    void trak_write_index(struct trak_t* trak, unsigned int start, unsigned int end)
    {
      // write samples [start,end>
     
      // stts = [entries * [sample_count, sample_duration]
      {
        unsigned char* stts = trak->mdia_.minf_.stbl_.stts_;
        unsigned int entries = 0;
        struct stts_table_t* table = (struct stts_table_t*)(stts + 8);
        unsigned int s;
        for(s = start; s != end; ++s)
        {
          unsigned int sample_count = 1;
          unsigned int sample_duration =
            trak->samples_[s + 1].pts_ - trak->samples_[s].pts_;
          while(s != end - 1)
          {
            if((trak->samples_[s + 1].pts_ - trak->samples_[s].pts_) != sample_duration)
              break;
            ++sample_count;
            ++s;
          }
          // write entry
          write_int32(&table[entries].sample_count_, sample_count);
          write_int32(&table[entries].sample_duration_, sample_duration);
          ++entries;
        }
        write_int32(stts + 4, entries);
        if(stts_get_samples(stts) != end - start)
        {
          printf("ERROR: stts_get_samples=%d, should be %d\n",
                 stts_get_samples(stts), end - start);
        }
      }
     
      // ctts = [entries * [sample_count, sample_offset]
      {
        unsigned char* ctts = trak->mdia_.minf_.stbl_.ctts_;
        if(ctts)
        {
          unsigned int entries = 0;
          struct ctts_table_t* table = (struct ctts_table_t*)(ctts + 8);
          unsigned int s;
          for(s = start; s != end; ++s)
          {
            unsigned int sample_count = 1;
            unsigned int sample_offset = trak->samples_[s].cto_;
            while(s != end - 1)
            {
              if(trak->samples_[s + 1].cto_ != sample_offset)
                break;
              ++sample_count;
              ++s;
            }
            // write entry
            write_int32(&table[entries].sample_count_, sample_count);
            write_int32(&table[entries].sample_offset_, sample_offset);
            ++entries;
          }
          write_int32(ctts + 4, entries);
          if(ctts_get_samples(ctts) != end - start)
          {
            printf("ERROR: ctts_get_samples=%d, should be %d\n",
                   ctts_get_samples(ctts), end - start);
          }
        }
      }
     
      // process chunkmap:
      {
        unsigned char* stsc = trak->mdia_.minf_.stbl_.stsc_;
        struct stsc_table_t* stsc_table = (struct stsc_table_t*)(stsc + 8);
        unsigned int i;
        for(i = 0; i != trak->chunks_size_; ++i)
        {
          if(trak->chunks_[i].sample_ + trak->chunks_[i].size_ > start)
            break;
        }
     
        {
          unsigned int stsc_entries = 0;
          unsigned int chunk_start = i;
          unsigned int chunk_end;
          unsigned int samples =
            trak->chunks_[i].sample_ + trak->chunks_[i].size_ - start;
          unsigned int id = trak->chunks_[i].id_;
     
          // write entry [chunk,samples,id]
          write_int32(&stsc_table[stsc_entries].chunk_, 1);
          write_int32(&stsc_table[stsc_entries].samples_, samples);
          write_int32(&stsc_table[stsc_entries].id_, id);
          ++stsc_entries;
          if(i != trak->chunks_size_)
          {
            for(i += 1; i != trak->chunks_size_; ++i)
            {
              if(trak->chunks_[i].sample_ >= end)
                break;
     
              if(trak->chunks_[i].size_ != samples)
              {
                samples = trak->chunks_[i].size_;
                id = trak->chunks_[i].id_;
                write_int32(&stsc_table[stsc_entries].chunk_, i - chunk_start + 1);
                write_int32(&stsc_table[stsc_entries].samples_, samples);
                write_int32(&stsc_table[stsc_entries].id_, id);
                ++stsc_entries;
              }
            }
          }
          chunk_end = i;
          write_int32(stsc + 4, stsc_entries);
          {
            unsigned char* stco = trak->mdia_.minf_.stbl_.stco_;
    //        stco_erase(stco, chunk_start);
    //        unsigned int entries = read_int32(stco + 4);
            unsigned int entries = chunk_end;
            uint32_t* stco_table = (uint32_t*)(stco + 8);
            memmove(stco_table, &stco_table[chunk_start],
                    (entries - chunk_start) * sizeof(uint32_t));
     
    memset(&stco_table[entries - chunk_start], 0,
           (read_int32(stco + 4) - (entries - chunk_start)) * sizeof(uint32_t));
     
            write_int32(stco + 4, entries - chunk_start);
     
            // patch first chunk with correct sample offset
    //        uint32_t* stco_table = (uint32_t*)(stco + 8);
            write_int32(stco_table, (uint32_t)trak->samples_[start].pos_);
          }
        }
      }
     
      // process sync samples:
      if(trak->mdia_.minf_.stbl_.stss_)
      {
        unsigned char* stss = trak->mdia_.minf_.stbl_.stss_;
        unsigned int entries = read_int32(stss + 4);
        uint32_t* table = (uint32_t*)(stss + 8);
        unsigned int stss_start;
        unsigned int i;
        for(i = 0; i != entries; ++i)
        {
          if(read_int32(&table[i]) >= start + 1)
            break;
        }
        stss_start = i;
        for(; i != entries; ++i)
        {
          unsigned int sync_sample = read_int32(&table[i]);
          if(sync_sample >= end + 1)
            break;
          write_int32(&table[i - stss_start], sync_sample - start);
     
        }
    //    memmove(table, table + stss_start, (i - stss_start) * sizeof(uint32_t));
        write_int32(stss + 4, i - stss_start);
      }
     
      // process sample sizes
      {
        unsigned char* stsz = trak->mdia_.minf_.stbl_.stsz_;
        if(stsz_get_sample_size(stsz) == 0)
        {
          uint32_t* table = (uint32_t*)(stsz + 12);
          memmove(table, &table[start], (end - start) * sizeof(uint32_t));
    memset(&table[end - start], 0,
           (read_int32(stsz + 8) - (end - start)) * sizeof(uint32_t));
          write_int32(stsz + 8, end - start);
        }
      }
    }
     
    int moov_parse(struct moov_t* moov, unsigned char* buffer, uint64_t size)
    {
      struct atom_t leaf_atom;
      unsigned char* buffer_start = buffer;
     
      moov->start_ = buffer;
     
      while(buffer < buffer_start + size)
      {
        buffer = atom_read_header(buffer, &leaf_atom);
     
        atom_print(&leaf_atom);
     
        if(atom_is(&leaf_atom, "cmov"))
        {
          return 0;
        }
        else
        if(atom_is(&leaf_atom, "mvhd"))
        {
          moov->mvhd_ = buffer;
        }
        else
        if(atom_is(&leaf_atom, "trak"))
        {
          if(moov->tracks_ == MAX_TRACKS)
            return 0;
          else
          {
            struct trak_t* trak = &moov->traks_[moov->tracks_];
            trak_init(trak);
            trak_parse(trak, buffer, leaf_atom.size_ - ATOM_PREAMBLE_SIZE);
            ++moov->tracks_;
          }
        }
        buffer = atom_skip(buffer, &leaf_atom);
      }
     
      // build the indexing tables
      {
        unsigned int i;
        for(i = 0; i != moov->tracks_; ++i)
        {
          trak_build_index(&moov->traks_[i]);
        }
      }
     
      return 1;
    }
     
    void stco_shift_offsets(unsigned char* stco, int offset)
    {
      unsigned int entries = read_int32(stco + 4);
      unsigned int* table = (unsigned int*)(stco + 8);
      unsigned int i;
      for(i = 0; i != entries; ++i)
        write_int32(&table[i], (read_int32(&table[i]) + offset));
    }
     
    void trak_shift_offsets(struct trak_t* trak, int64_t offset)
    {
      unsigned char* stco = trak->mdia_.minf_.stbl_.stco_;
      stco_shift_offsets(stco, (int32_t)offset);
    }
     
    void moov_shift_offsets(struct moov_t* moov, int64_t offset)
    {
      unsigned int i;
      for(i = 0; i != moov->tracks_; ++i)
      {
        trak_shift_offsets(&moov->traks_[i], offset);
      }
    }
     
    long mvhd_get_time_scale(unsigned char* mvhd)
    {
      int version = read_char(mvhd);
      unsigned char* p = mvhd + (version == 0 ? 12 : 20);
      return read_int32(p);
    }
     
    void mvhd_set_duration(unsigned char* mvhd, uint64_t duration)
    {
      int version = read_char(mvhd);
      if(version == 0)
      {
        write_int32(mvhd + 16, (uint32_t)duration);
      }
      else
      {
        write_int64(mvhd + 24, duration);
      }
    }
     
    long mdhd_get_time_scale(unsigned char* mdhd)
    {
      int version = read_char(mdhd);
      unsigned char* p = mdhd + (version == 0 ? 12 : 20);
     
      return read_int32(p);
    }
     
    void mdhd_set_duration(unsigned char* mdhd, uint64_t duration)
    {
      int version = read_char(mdhd);
      if(version == 0)
      {
        write_int32(mdhd + 16, (uint32_t)duration);
      }
      else
      {
        write_int64(mdhd + 24, duration);
      }
    }
     
    void tkhd_set_duration(unsigned char* tkhd, uint64_t duration)
    {
      int version = read_char(tkhd);
      if(version == 0)
      {
        write_int32(tkhd + 20, (uint32_t)duration);
      }
      else
      {
        write_int64(tkhd + 28, duration);
      }
    }
     
    unsigned int stss_get_entries(unsigned char const* stss)
    {
      return read_int32(stss + 4);
    }
     
    long stss_get_sample(unsigned char const* stss, unsigned int idx)
    {
      unsigned char const* p = stss + 8 + idx * 4;
      return read_int32(p);
    }
     
    unsigned int stss_get_nearest_keyframe(unsigned char const* stss, unsigned int sample)
    {
      // scan the sync samples to find the key frame that precedes the sample number
      unsigned int i;
      unsigned int entries = stss_get_entries(stss);
      unsigned int table_sample = 0;
      for(i = 0; i != entries; ++i)
      {
        table_sample = stss_get_sample(stss, i);
        if(table_sample >= sample)
          break;
      }
      if(table_sample == sample)
        return table_sample;
      else
        return stss_get_sample(stss, i - 1);
    }
     
    unsigned int stbl_get_nearest_keyframe(struct stbl_t const* stbl, unsigned int sample)
    {
      // If the sync atom is not present, all samples are implicit sync samples.
      if(!stbl->stss_)
        return sample;
     
      return stss_get_nearest_keyframe(stbl->stss_, sample);
    }
     
    unsigned int moov_seek(unsigned char* moov_data,
                           uint64_t* moov_size,
                           float start_time,
                           float end_time,
                           uint64_t* mdat_start,
                           uint64_t* mdat_size,
                           uint64_t offset)
    {
      struct moov_t* moov = malloc(sizeof(struct moov_t));
      moov_init(moov);
      if(!moov_parse(moov, moov_data + ATOM_PREAMBLE_SIZE, *moov_size - ATOM_PREAMBLE_SIZE))
      {
        moov_exit(moov);
        free(moov);
        return 0;
      }
     
      {
        long moov_time_scale = mvhd_get_time_scale(moov->mvhd_);
        unsigned int start = (unsigned int)(start_time * moov_time_scale);
        unsigned int end = (unsigned int)(end_time * moov_time_scale);
        uint64_t skip_from_start = UINT64_MAX;
        uint64_t end_offset = 0;
        unsigned int i;
     
        // for every trak, convert seconds to sample (time-to-sample).
        // adjust sample to keyframe
        unsigned int trak_sample_start[MAX_TRACKS];
        unsigned int trak_sample_end[MAX_TRACKS];
     
        uint64_t moov_duration = 0;
     
        // clayton.mp4 has a third track with one sample that lasts the whole clip.
        // Assuming the first two tracks are the audio and video track, we patch
        // the remaining tracks to 'free' atoms.
        if(moov->tracks_ > 2)
        {
          for(i = 2; i != moov->tracks_; ++i)
          {
            // patch 'trak' to 'free'
            unsigned char* p = moov->traks_[i].start_ - 4;
            p[0] = 'f';
            p[1] = 'r';
            p[2] = 'e';
            p[3] = 'e';
          }
          moov->tracks_ = 2;
        }
     
        for(i = 0; i != moov->tracks_; ++i)
        {
          struct trak_t* trak = &moov->traks_[i];
          struct stbl_t* stbl = &trak->mdia_.minf_.stbl_;
          long trak_time_scale = mdhd_get_time_scale(trak->mdia_.mdhd_);
          float moov_to_trak_time = (float)trak_time_scale / (float)moov_time_scale;
          float trak_to_moov_time = (float)moov_time_scale / (float)trak_time_scale;
     
          // DEBUG
    #if 0
          {
            uint64_t trak_duration = stts_get_duration(stbl->stts_);
          }
    #endif
     
          // get start
          if(start == 0)
          {
            trak_sample_start[i] = start;
          }
          else
          {
            start = stts_get_sample(stbl->stts_, (unsigned int)(start * moov_to_trak_time));
            start = stbl_get_nearest_keyframe(stbl, start + 1) - 1;
            trak_sample_start[i] = start;
            start = (unsigned int)(stts_get_time(stbl->stts_, start) * trak_to_moov_time);
          }
     
          // get end
          if(end == 0)
          {
            trak_sample_end[i] = trak->samples_size_;
          }
          else
          {
            end = stts_get_sample(stbl->stts_, (unsigned int)(end * moov_to_trak_time));
            if(end >= trak->samples_size_)
            {
              end = trak->samples_size_;
            }
            else
            {
              end = stbl_get_nearest_keyframe(stbl, end + 1) - 1;
            }
            trak_sample_end[i] = end;
            printf("endframe=%u, samples_size_=%u\n", end, trak->samples_size_);
            end = (unsigned int)(stts_get_time(stbl->stts_, end) * trak_to_moov_time);
          }
        }
     
        printf("start=%u\n", start);
        printf("end=%u\n", end);
     
        if(end && start >= end)
          return 0;
     
        for(i = 0; i != moov->tracks_; ++i)
        {
          struct trak_t* trak = &moov->traks_[i];
          struct stbl_t* stbl = &trak->mdia_.minf_.stbl_;
     
          unsigned int start_sample = trak_sample_start[i];
          unsigned int end_sample = trak_sample_end[i];
     
          trak_write_index(trak, start_sample, end_sample);
     
          {
            uint64_t skip =
              trak->samples_[start_sample].pos_ - trak->samples_[0].pos_;
            if(skip < skip_from_start)
              skip_from_start = skip;
            printf("Trak can skip %llu bytes\n", skip);
     
            if(end_sample != trak->samples_size_)
            {
              uint64_t end_pos = trak->samples_[end_sample].pos_;
              if(end_pos > end_offset)
                end_offset = end_pos;
              printf("New endpos=%llu\n", end_pos);
              printf("Trak can skip %llu bytes at end\n",
                     *mdat_start + *mdat_size - end_offset);
            }
          }
     
          {
            // fixup trak (duration)
            uint64_t trak_duration = stts_get_duration(stbl->stts_);
            long trak_time_scale = mdhd_get_time_scale(trak->mdia_.mdhd_);
            float trak_to_moov_time = (float)moov_time_scale / (float)trak_time_scale;
            uint64_t duration = (long)((float)trak_duration * trak_to_moov_time);
            mdhd_set_duration(trak->mdia_.mdhd_, trak_duration);
            tkhd_set_duration(trak->tkhd_, duration);
            printf("trak: new_duration=%lld\n", duration);
     
            if(duration > moov_duration)
              moov_duration = duration;
          }
     
          printf("stco.size=%d, ", read_int32(stbl->stco_ + 4));
          printf("stts.size=%d samples=%d\n", read_int32(stbl->stts_ + 4), stts_get_samples(stbl->stts_));
          printf("stsz.size=%d\n", read_int32(stbl->stsz_ + 8));
          printf("stsc.samples=%d\n", stsc_get_samples(stbl->stsc_));
        }
        mvhd_set_duration(moov->mvhd_, moov_duration);
     
        offset -= skip_from_start;
     
        printf("shifting offsets by %lld\n", offset);
        moov_shift_offsets(moov, offset);
     
    #ifdef COMPRESS_MOOV_ATOM
        {
          uLong sourceLen = *moov_size - ATOM_PREAMBLE_SIZE;
          uLong destLen = compressBound(sourceLen);
          unsigned char* cmov = malloc(destLen);
          int zstatus = compress(cmov, &destLen, moov_data, sourceLen);
          if(zstatus == Z_OK)
          {
            printf("cmov size = %lu (%ld%%)\n", destLen, 100 * destLen / sourceLen);
          }
     
          {
            const int extra_space = 4096;
            if(destLen + extra_space < sourceLen)
            {
              const int bytes_saved = sourceLen - destLen;
              uLong destLen2;
              int extra = 0;
              printf("shifting offsets by %d\n", -bytes_saved);
              moov_shift_offsets(moov, -bytes_saved);
     
              extra += ATOM_PREAMBLE_SIZE + 4;            // dcom
              extra += ATOM_PREAMBLE_SIZE + 4;            // cmvd
              extra += ATOM_PREAMBLE_SIZE;                // cmov
              extra += ATOM_PREAMBLE_SIZE + extra_space;  // free
     
              printf("shifting offsets by %d\n", extra);
              moov_shift_offsets(moov, extra);
     
              // recompress
              destLen2 = compressBound(sourceLen);
              zstatus = compress(cmov, &destLen2, moov_data, sourceLen);
              if(zstatus == Z_OK)
              {
                printf("cmov size = %lu (%ld%%)\n", destLen2, 100 * destLen2 / sourceLen);
     
                if(destLen2 < destLen + extra_space)
                {
                  // copy compressed movie atom
                  unsigned char* outbuffer = moov_data;
     
                  uint32_t dcom_size = ATOM_PREAMBLE_SIZE + 4;
                  uint32_t cmvd_size = ATOM_PREAMBLE_SIZE + 4 + destLen2;
                  uint32_t cmov_size = ATOM_PREAMBLE_SIZE + dcom_size + cmvd_size;
                  uint32_t free_size = ATOM_PREAMBLE_SIZE + extra_space + destLen - destLen2;
                  *moov_size = ATOM_PREAMBLE_SIZE + cmov_size + free_size;
     
                  write_int32(outbuffer, (uint32_t)*moov_size);
                  outbuffer += 4;
     
                  // skip 'moov'
                  outbuffer += 4;
     
                  write_int32(outbuffer, cmov_size);
                  outbuffer += 4;
                  {
                    outbuffer[0] = 'c';
                    outbuffer[1] = 'm';
                    outbuffer[2] = 'o';
                    outbuffer[3] = 'v';
                    outbuffer += 4;
     
                    write_int32(outbuffer, dcom_size);
                    outbuffer += 4;
                    {
                      outbuffer[0] = 'd';
                      outbuffer[1] = 'c';
                      outbuffer[2] = 'o';
                      outbuffer[3] = 'm';
                      outbuffer += 4;
     
                      outbuffer[0] = 'z';
                      outbuffer[1] = 'l';
                      outbuffer[2] = 'i';
                      outbuffer[3] = 'b';
                      outbuffer += 4;
                    }
     
                    write_int32(outbuffer, cmvd_size);
                    outbuffer += 4;
                    {
                      outbuffer[0] = 'c';
                      outbuffer[1] = 'm';
                      outbuffer[2] = 'v';
                      outbuffer[3] = 'd';
                      outbuffer += 4;
     
                      write_int32(outbuffer, sourceLen);
                      outbuffer += 4;
     
                      memcpy(outbuffer, cmov, destLen2);
                      outbuffer += destLen2;
                    }
                  }
     
                  // add final padding
                  write_int32(outbuffer, free_size);
                  outbuffer += 4;
     
                  outbuffer[0] = 'f';
                  outbuffer[1] = 'r';
                  outbuffer[2] = 'e';
                  outbuffer[3] = 'e';
                  outbuffer += 4;
     
                  {
                    const char free_bytes[8] =
                    {
                      'C', 'o', 'd', 'e','S','h', 'o', 'p'
                    };
                    uint32_t padding_index;
                    for(padding_index = ATOM_PREAMBLE_SIZE; padding_index != free_size; ++padding_index)
                    {
                      outbuffer[padding_index] = free_bytes[padding_index % 8];
                    }
                  }
                }
                else
                {
                  printf("2nd pass compress overflow\n");
                }
              }
            }
          }
          free(cmov);
        }
    #endif
     
        *mdat_start += skip_from_start;
        if(end_offset != 0)
        {
          *mdat_size = end_offset;
        }
        *mdat_size -= skip_from_start;
      }
     
      moov_exit(moov);
      free(moov);
     
      return 1;
    }
    Merci pour votre aide...

  4. #4
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    34
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2009
    Messages : 34
    Points : 27
    Points
    27
    Par défaut
    Ou alors pouvez me compiler cette extensio au format .so et .dll ?
    Sources : http://codeblog.palos.ro/2008/11/13/...ideo-from-php/
    Merci.

Discussions similaires

  1. Appel fonction php dans code javascript
    Par licorne dans le forum Général JavaScript
    Réponses: 7
    Dernier message: 05/03/2008, 11h55
  2. [PHP-JS] Code postal et ville
    Par lodan dans le forum Langage
    Réponses: 7
    Dernier message: 18/08/2006, 00h00
  3. Réponses: 4
    Dernier message: 21/06/2006, 17h09
  4. equivalent chr et ord de PHP (caractere<-> code ASCII)
    Par wamania dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 24/05/2006, 12h51
  5. Réponses: 23
    Dernier message: 26/04/2006, 20h58

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo