libmp4.c 75 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * libmp4.c : LibMP4 library for mp4 module for vlc
 *****************************************************************************
 * Copyright (C) 2001 VideoLAN
5
 * $Id: libmp4.c,v 1.7 2002/10/10 22:46:20 massiot Exp $
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 * 
 * This program 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 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */
23
#include <stdarg.h>
24 25 26 27 28 29 30
#include <string.h>                                              /* strdup() */
#include <errno.h>
#include <sys/types.h>

#include <vlc/vlc.h>
#include <vlc/input.h>

31 32 33 34
#ifdef HAVE_ZLIB_H
#   include <zlib.h>                                     /* for compressed moov */
#endif

35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include "libmp4.h"

/*****************************************************************************
 * Here are defined some macro to make life simpler but before using it 
 *  *look* at the code. 
 *
 *  XXX: All macro are written in capital letters
 *
 *****************************************************************************/
#define MP4_BOX_HEADERSIZE( p_box ) \
  ( 8 + ( p_box->i_shortsize == 1 ? 8 : 0 ) \
      + ( p_box->i_type == FOURCC_uuid ? 16 : 0 ) )
    
#define MP4_BOX_DESCEND( p_box ) \
49
    MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) )
50 51 52 53 54 55 56 57 58 59 60 61 62 63

#define MP4_GET1BYTE( dst ) \
    dst = *p_peek; p_peek++; i_read--

#define MP4_GET2BYTES( dst ) \
    dst = GetWBE( p_peek ); p_peek += 2; i_read -= 2

#define MP4_GET3BYTES( dst ) \
    dst = Get24bBE( p_peek ); p_peek += 3; i_read -= 3

#define MP4_GET4BYTES( dst ) \
    dst = GetDWBE( p_peek ); p_peek += 4; i_read -= 4
    
#define MP4_GETFOURCC( dst ) \
64 65
    dst = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] ); \
    p_peek += 4; i_read -= 4
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 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431

#define MP4_GET8BYTES( dst ) \
    dst = GetQWBE( p_peek ); p_peek += 8; i_read -= 8
        
#define MP4_GETVERSIONFLAGS( p_void ) \
    MP4_GET1BYTE( p_void->i_version ); \
    MP4_GET3BYTES( p_void->i_flags )
    
#define MP4_GETSTRINGZ( p_str ) \
    if( ( i_read > 0 )&&(p_peek[0] ) ) \
    { \
        p_str = calloc( sizeof( char ), __MIN( strlen( p_peek ), i_read )+1);\
        memcpy( p_str, p_peek, __MIN( strlen( p_peek ), i_read ) ); \
        p_str[__MIN( strlen( p_peek ), i_read )] = 0; \
        p_peek += strlen( p_str ) + 1; \
        i_read -= strlen( p_str ) + 1; \
    } \
    else \
    { \
        p_str = NULL; \
    }
    

#define MP4_READBOX_ENTER( MP4_Box_data_TYPE_t ) \
    s64 i_read = p_box->i_size; \
    u8 *p_peek, *p_buff; \
    i_read = p_box->i_size; \
    if( !( p_peek = p_buff = malloc( i_read ) ) ) \
    { \
        return( 0 ); \
    } \
    if( !MP4_ReadStream( p_stream, p_peek, i_read ) )\
    { \
        free( p_buff ); \
        return( 0 ); \
    } \
    p_peek += MP4_BOX_HEADERSIZE( p_box ); \
    i_read -= MP4_BOX_HEADERSIZE( p_box ); \
    if( !( p_box->data.p_data = malloc( sizeof( MP4_Box_data_TYPE_t ) ) ) ) \
    { \
      free( p_buff ); \
      return( 0 ); \
    } 
                
#define MP4_READBOX_EXIT( i_code ) \
    free( p_buff ); \
    if( i_read < 0 ) \
    { \
        msg_Warn( p_stream->p_input, "Not enougth data" ); \
    } \
    return( i_code )

#define FREE( p ) \
    if( p ) {free( p ); p = NULL; }
   

    
/* Some assumptions:
        * The input method HAVE to be seekable 
 
*/

/* Some functions to manipulate memory */
static u16 GetWLE( u8 *p_buff )
{
    return( (p_buff[0]) + ( p_buff[1] <<8 ) );
}

static u32 GetDWLE( u8 *p_buff )
{
    return( p_buff[0] + ( p_buff[1] <<8 ) +
            ( p_buff[2] <<16 ) + ( p_buff[3] <<24 ) );
}

static u16 GetWBE( u8 *p_buff )
{
    return( (p_buff[0]<<8) + p_buff[1] );
}

static u32 Get24bBE( u8 *p_buff )
{
    return( ( p_buff[0] <<16 ) + ( p_buff[1] <<8 ) + p_buff[2] );
}


static u32 GetDWBE( u8 *p_buff )
{
    return( (p_buff[0] << 24) + ( p_buff[1] <<16 ) +
            ( p_buff[2] <<8 ) + p_buff[3] );
}

static u64 GetQWBE( u8 *p_buff )
{
    return( ( (u64)GetDWBE( p_buff ) << 32 )|( (u64)GetDWBE( p_buff + 4 ) ) );
}


static void GetUUID( UUID_t *p_uuid, u8 *p_buff )
{
    memcpy( p_uuid, 
            p_buff,
            16 );
}

static void CreateUUID( UUID_t *p_uuid, u32 i_fourcc )
{
    /* made by 0xXXXXXXXX-0011-0010-8000-00aa00389b71 
            where XXXXXXXX is the fourcc */
    /* FIXME implement this */
}

/* some functions for mp4 encoding of variables */

void MP4_ConvertDate2Str( char *psz, u64 i_date )
{
    int i_day;
    int i_hour;
    int i_min;
    int i_sec;

    i_day = i_date / ( 60*60*24);
    i_hour = ( i_date /( 60*60 ) ) % 60;
    i_min  = ( i_date / 60 ) % 60;
    i_sec =  i_date % 60;
    /* FIXME do it correctly, date begin at 1 jan 1904 */ 
    sprintf( psz, "%dd-%2.2dh:%2.2dm:%2.2ds",
                   i_day, i_hour, i_min, i_sec );
}



/*****************************************************************************
 * Some basic functions to manipulate stream more easily in vlc
 *
 * MP4_TellAbsolute get file position
 * 
 * MP4_SeekAbsolute seek in the file
 *
 * MP4_ReadData read data from the file in a buffer
 *
 *****************************************************************************/
off_t MP4_TellAbsolute( input_thread_t *p_input )
{
    off_t i_pos;
    
    vlc_mutex_lock( &p_input->stream.stream_lock );
    
    i_pos= p_input->stream.p_selected_area->i_tell -
            ( p_input->p_last_data - p_input->p_current_data  );

    vlc_mutex_unlock( &p_input->stream.stream_lock );

    return( i_pos );
}
 
int MP4_SeekAbsolute( input_thread_t *p_input,
                       off_t i_pos)
{
    off_t i_filepos;

    if( i_pos >= p_input->stream.p_selected_area->i_size )
    {
        return( 0 );
    }
            
    i_filepos = MP4_TellAbsolute( p_input );
    if( i_pos != i_filepos )
    {
        p_input->pf_seek( p_input, i_pos );
        input_AccessReinit( p_input );
    }
    return( 1 );
}

/* return 1 if success, 0 if fail */
int MP4_ReadData( input_thread_t *p_input, u8 *p_buff, int i_size )
{
    data_packet_t *p_data;

    int i_read;

                
    if( !i_size )
    {
        return( 1 );
    }

    do
    {
        i_read = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
        if( i_read <= 0 )
        {
            return( 0 );
        }
        memcpy( p_buff, p_data->p_payload_start, i_read );
        input_DeletePacket( p_input->p_method_data, p_data );
        
        p_buff += i_read;
        i_size -= i_read;
                
    } while( i_size );
    
    return( 1 );
}

/*****************************************************************************
 * Some basic functions to manipulate MP4_Stream_t, an abstraction o p_input
 *  in the way that you can read from a memory buffer or from an input
 *
 *****************************************************************************/

/****  ------- First some function to make abstract from input --------  */

/****************************************************************************
 * MP4_InputStream create an stram with an input
 *
 ****************************************************************************/
MP4_Stream_t *MP4_InputStream( input_thread_t *p_input )
{
    MP4_Stream_t *p_stream;

    if( !( p_stream = malloc( sizeof( MP4_Stream_t ) ) ) )
    {
        return( NULL );
    }
    p_stream->b_memory = 0;
    p_stream->p_input = p_input;
    p_stream->i_start = 0;
    p_stream->i_stop = 0;
    p_stream->p_buffer = NULL;
    return( p_stream );
}


/****************************************************************************
 * MP4_MemoryStream create a memory stream
 * if p_buffer == NULL, will allocate a buffer of i_size, else 
 *     it uses p_buffer XXX you have to unallocate it yourself !
 *
 ****************************************************************************/
MP4_Stream_t *MP4_MemoryStream( input_thread_t *p_input,
                                int i_size, u8 *p_buffer )
{
    MP4_Stream_t *p_stream;

    if( !( p_stream = malloc( sizeof( MP4_Stream_t ) ) ) )
    {
        return( NULL );
    }
    p_stream->b_memory = 1;
    p_stream->p_input = p_input;
    p_stream->i_start = 0;
    p_stream->i_stop = i_size;
    if( !p_stream->p_buffer )
    {
        if( !( p_stream->p_buffer = malloc( i_size ) ) )
        {
            free( p_stream );
            return( NULL );
        }
    }
    else
    {
        p_stream->p_buffer = p_buffer;
    }

    return( p_stream );
}
/****************************************************************************
 * MP4_ReadStream read from a MP4_Stream_t
 *
 ****************************************************************************/
int MP4_ReadStream( MP4_Stream_t *p_stream, u8 *p_buff, int i_size )
{
    if( p_stream->b_memory )
    {
        if( i_size > p_stream->i_stop - p_stream->i_start )
        {
            return( 0 );
        }
        memcpy( p_buff, 
                p_stream->p_buffer + p_stream->i_start,
                i_size );
        p_stream->i_start += i_size;
        return( 1 );
    }
    else
    {
        return( MP4_ReadData( p_stream->p_input, p_buff, i_size ) );
    }
}

/****************************************************************************
 * MP4_PeekStream peek from a MP4_Stream_t
 *
 ****************************************************************************/
int MP4_PeekStream( MP4_Stream_t *p_stream, u8 **pp_peek, int i_size )
{
    if( p_stream->b_memory )
    {
        *pp_peek = p_stream->p_buffer + p_stream->i_start;

        return( __MIN(i_size,p_stream->i_stop - p_stream->i_start ));
    }
    else
    {
        return( input_Peek( p_stream->p_input, pp_peek, i_size ) );
    }
}

/****************************************************************************
 * MP4_TellStream give absolute position in the stream
 * XXX for a memory stream give position from begining of the buffer
 ****************************************************************************/
off_t MP4_TellStream( MP4_Stream_t *p_stream )
{
    if( p_stream->b_memory )
    {
        return( p_stream->i_start );
    }
    else
    {
        return( MP4_TellAbsolute( p_stream->p_input ) );
    }
}

/****************************************************************************
 * MP4_SeekStream seek in a MP4_Stream_t
 *
 ****************************************************************************/
int MP4_SeekStream( MP4_Stream_t *p_stream, off_t i_pos)
{
    if( p_stream->b_memory )
    {
        if( i_pos < p_stream->i_stop )
        {
            p_stream->i_start = i_pos;
            return( 1 );
        }
        else
        {
            return( 0 );
        }
    }
    else
    {
        return( MP4_SeekAbsolute( p_stream->p_input, i_pos ) );
    }
}



/*****************************************************************************
 * MP4_ReadBoxCommon : Load only common parameters for all boxes 
 *****************************************************************************
 * p_box need to be an already allocated MP4_Box_t, and all data 
 *  will only be peek not read
 *
 * RETURN : 0 if it fail, 1 otherwise
 *****************************************************************************/
int MP4_ReadBoxCommon( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    int i_read;
    u8  *p_peek;
    
    if( ( ( i_read = MP4_PeekStream( p_stream, &p_peek, 32 ) ) < 8 ) )
    {
        return( 0 );
    }
    p_box->i_pos = MP4_TellStream( p_stream );

    p_box->data.p_data = NULL;
    p_box->p_father = NULL;
    p_box->p_first  = NULL;
    p_box->p_last  = NULL;
    p_box->p_next   = NULL;
    
    MP4_GET4BYTES( p_box->i_shortsize );
    MP4_GETFOURCC( p_box->i_type );

    /* Now special case */

    if( p_box->i_shortsize == 1 )
    {
        /* get the true size on 64 bits */
        MP4_GET8BYTES( p_box->i_size );
    }
    else
    {
        p_box->i_size = p_box->i_shortsize;
        /* XXX size of 0 means that the box extends to end of file */
    }
    
    if( p_box->i_type == FOURCC_uuid )
    {
        /* get extented type on 16 bytes */
        GetUUID( &p_box->i_uuid, p_peek );
        p_peek += 16; i_read -= 16;
    }
    else
    {
        CreateUUID( &p_box->i_uuid, p_box->i_type );
    }
#ifdef MP4_VERBOSE
    /* FIXME how to write u64 ??? */
    if( p_box->i_size )
    {
        msg_Dbg( p_stream->p_input, "Found Box: %c%c%c%c size %d",
                 (p_box->i_type)&0xff, (p_box->i_type>>8)&0xff, 
                     (p_box->i_type>>16)&0xff, (p_box->i_type>>24)&0xff,
                 (u32)p_box->i_size );
    }
#endif

    return( 1 ); 
}


/*****************************************************************************
 * MP4_MP4_NextBox : Go to the next box 
 *****************************************************************************
 * if p_box == NULL, go to the next box in witch we are( at the begining ).
 *****************************************************************************/
int MP4_NextBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_Box_t box;

    if( !p_box )
    {
        MP4_ReadBoxCommon( p_stream, &box );
        p_box = &box;
    }

    if( !p_box->i_size )
    {
        return( 2 ); /* Box with infinite size */
    }

    if( p_box->p_father )
    {
        /* check if it's within p-father */
        if( p_box->i_size + p_box->i_pos >= 
                    p_box->p_father->i_size + p_box->p_father->i_pos )
        {
            return( 0 ); /* out of bound */
        }
    }
    return( MP4_SeekStream( p_stream, p_box->i_size + p_box->i_pos ) );
}
/*****************************************************************************
 * MP4_MP4_GotoBox : Go to this particular box 
 *****************************************************************************
 * RETURN : 0 if it fail, 1 otherwise
 *****************************************************************************/
int MP4_GotoBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    return( MP4_SeekStream( p_stream, p_box->i_pos ) );
}


/*****************************************************************************
 * For all known box a loader is given, 
 *  XXX: all common struct have to be already read by MP4_ReadBoxCommon
 *       after called one of theses functions, file position is unknown
 *       you need to call MP4_GotoBox to go where you want 
 *****************************************************************************/
int MP4_ReadBoxContainerRaw( MP4_Stream_t *p_stream, MP4_Box_t *p_container )
{
    MP4_Box_t *p_box;
    
    if( MP4_TellStream( p_stream ) + 8 > 
                        p_container->i_pos + p_container->i_size  )
    {
        /* there is no box to load */
        return( 0 );
    }
    
    do
    {
        p_box = malloc( sizeof( MP4_Box_t ) );

        if( MP4_ReadBox( p_stream, p_box , p_container ) )
        {
            /* chain this box with the father and the other at same level */
            if( !p_container->p_first )
            {
                p_container->p_first = p_box;
            }
            else
            {
                p_container->p_last->p_next = p_box;
            }
            p_container->p_last = p_box;
        }
        else
        {
            /* free memory */
            free( p_box );
            break;
        }
        
    }while( MP4_NextBox( p_stream, p_box ) == 1 );
    
    return( 1 );
}


int MP4_ReadBoxContainer( MP4_Stream_t *p_stream, MP4_Box_t *p_container )
{
    
    if( p_container->i_size <= MP4_BOX_HEADERSIZE(p_container ) + 8 )
    {
        /* container is empty, 8 stand for the first header in this box */
        return( 1 );
    }
    
    /* enter box */
    MP4_BOX_DESCEND( p_container );

    return( MP4_ReadBoxContainerRaw( p_stream, p_container ) );
}

void MP4_FreeBox_Common( input_thread_t *p_input, MP4_Box_t *p_box )
{
    /* Up to now do nothing */
}

int MP4_ReadBoxSkip( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    /* Nothing to do */
#ifdef MP4_VERBOSE
    msg_Dbg( p_stream->p_input, "Skip box: \"%c%c%c%c\"", 
            (p_box->i_type)&0xff, 
            (p_box->i_type>>8)&0xff,
            (p_box->i_type>>16)&0xff, 
            (p_box->i_type>>24)&0xff );
#endif
    return( 1 );
}

int MP4_ReadBox_ftyp( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_ftyp_t );
    
    MP4_GETFOURCC( p_box->data.p_ftyp->i_major_brand );
    MP4_GET4BYTES( p_box->data.p_ftyp->i_minor_version );
    
    if( ( p_box->data.p_ftyp->i_compatible_brands_count = i_read / 4 ) )
    {
        int i;
        p_box->data.p_ftyp->i_compatible_brands = 
            calloc( p_box->data.p_ftyp->i_compatible_brands_count, sizeof(u32));

        for( i =0; i < p_box->data.p_ftyp->i_compatible_brands_count; i++ )
        {
            MP4_GETFOURCC( p_box->data.p_ftyp->i_compatible_brands[i] );
        }
    }
    else
    {
        p_box->data.p_ftyp->i_compatible_brands = NULL;
    }

    MP4_READBOX_EXIT( 1 );
}

void MP4_FreeBox_ftyp( input_thread_t *p_input, MP4_Box_t *p_box )
{
    FREE( p_box->data.p_ftyp->i_compatible_brands );
}


int MP4_ReadBox_mvhd(  MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    int i;
#ifdef MP4_VERBOSE
    char s_creation_time[128];
    char s_modification_time[128];
    char s_duration[128];
#endif
    MP4_READBOX_ENTER( MP4_Box_data_mvhd_t );

    MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );

    if( p_box->data.p_mvhd->i_version )
    {
        MP4_GET8BYTES( p_box->data.p_mvhd->i_creation_time );
        MP4_GET8BYTES( p_box->data.p_mvhd->i_modification_time );
        MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
        MP4_GET8BYTES( p_box->data.p_mvhd->i_duration );
    }
    else
    {
        MP4_GET4BYTES( p_box->data.p_mvhd->i_creation_time );
        MP4_GET4BYTES( p_box->data.p_mvhd->i_modification_time );
        MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
        MP4_GET4BYTES( p_box->data.p_mvhd->i_duration );
    }
    MP4_GET4BYTES( p_box->data.p_mvhd->i_rate ); 
    MP4_GET2BYTES( p_box->data.p_mvhd->i_volume ); 
    MP4_GET2BYTES( p_box->data.p_mvhd->i_reserved1 );

  
    for( i = 0; i < 2; i++ )
    {
        MP4_GET4BYTES( p_box->data.p_mvhd->i_reserved2[i] );
    }
    for( i = 0; i < 9; i++ )
    {
        MP4_GET4BYTES( p_box->data.p_mvhd->i_matrix[i] );
    }
    for( i = 0; i < 6; i++ )
    {
        MP4_GET4BYTES( p_box->data.p_mvhd->i_predefined[i] );
    }
    
    MP4_GET4BYTES( p_box->data.p_mvhd->i_next_track_id );
    
            
#ifdef MP4_VERBOSE
    MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time );
    MP4_ConvertDate2Str( s_modification_time, 
                         p_box->data.p_mvhd->i_modification_time );
    if( p_box->data.p_mvhd->i_rate )
    {
        MP4_ConvertDate2Str( s_duration, 
                 p_box->data.p_mvhd->i_duration / p_box->data.p_mvhd->i_rate );
    }
    else
    {
        s_duration[0] = 0;
    }    
    msg_Dbg( p_stream->p_input, "Read Box: \"mvhd\" creation %s modification %s time scale %d duration %s rate %f volume %f next track id %d",
                  s_creation_time,
                  s_modification_time,
                  (u32)p_box->data.p_mvhd->i_timescale,
                  s_duration,
                  (float)p_box->data.p_mvhd->i_rate / (1<<16 ),
                  (float)p_box->data.p_mvhd->i_volume / 256 ,
                  (u32)p_box->data.p_mvhd->i_next_track_id );
#endif
    MP4_READBOX_EXIT( 1 );
}

int MP4_ReadBox_tkhd(  MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    int i;
#ifdef MP4_VERBOSE
    char s_creation_time[128];
    char s_modification_time[128];
    char s_duration[128];
#endif
    MP4_READBOX_ENTER( MP4_Box_data_tkhd_t );
    
    MP4_GETVERSIONFLAGS( p_box->data.p_tkhd );

    if( p_box->data.p_tkhd->i_version )
    {
        MP4_GET8BYTES( p_box->data.p_tkhd->i_creation_time );
        MP4_GET8BYTES( p_box->data.p_tkhd->i_modification_time );
        MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
        MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
        MP4_GET8BYTES( p_box->data.p_tkhd->i_duration );
    }
    else
    {
        MP4_GET4BYTES( p_box->data.p_tkhd->i_creation_time );
        MP4_GET4BYTES( p_box->data.p_tkhd->i_modification_time );
        MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
        MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
        MP4_GET4BYTES( p_box->data.p_tkhd->i_duration );
    }
    
    for( i = 0; i < 2; i++ )
    {
        MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved2[i] );
    }
    MP4_GET2BYTES( p_box->data.p_tkhd->i_layer );
    MP4_GET2BYTES( p_box->data.p_tkhd->i_predefined );
    MP4_GET2BYTES( p_box->data.p_tkhd->i_volume );
    MP4_GET2BYTES( p_box->data.p_tkhd->i_reserved3 );

    for( i = 0; i < 9; i++ )
    {
        MP4_GET4BYTES( p_box->data.p_tkhd->i_matrix[i] );
    }
    MP4_GET4BYTES( p_box->data.p_tkhd->i_width );
    MP4_GET4BYTES( p_box->data.p_tkhd->i_height );
            
#ifdef MP4_VERBOSE
    MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time );
    MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mvhd->i_modification_time );
    MP4_ConvertDate2Str( s_duration, p_box->data.p_mvhd->i_duration );
    
    msg_Dbg( p_stream->p_input, "Read Box: \"tkhd\" creation %s modification %s duration %s track ID %d layer %d volume %f width %f height %f",
                  s_creation_time,
                  s_modification_time,
                  s_duration,
                  p_box->data.p_tkhd->i_track_ID,
                  p_box->data.p_tkhd->i_layer,
                  (float)p_box->data.p_tkhd->i_volume / 256 ,
                  (float)p_box->data.p_tkhd->i_width / 65536,
                  (float)p_box->data.p_tkhd->i_height / 65536 );
#endif
    MP4_READBOX_EXIT( 1 );
}

int MP4_ReadBox_tref( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{

    if( p_box->i_size < MP4_BOX_HEADERSIZE(p_box ) + 8 )
    {
        /* container is empty, 8 stand for the first header in this box */
        return( 1 );
    }
   
    if( !MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) ) )
    {
        return( 0 );
    }

    MP4_ReadBoxContainerRaw( p_stream, p_box );

#ifdef MP4_VERBOSE
    msg_Dbg( p_stream->p_input, "Read Box: \"tref\" " );

#endif
    return( 1 );
}



int MP4_ReadBox_mdhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    int i;
    u16 i_language;
#ifdef MP4_VERBOSE
    char s_creation_time[128];
    char s_modification_time[128];
    char s_duration[128];
#endif
    MP4_READBOX_ENTER( MP4_Box_data_mdhd_t );

    MP4_GETVERSIONFLAGS( p_box->data.p_mdhd );

    if( p_box->data.p_mdhd->i_version )
    {
        MP4_GET8BYTES( p_box->data.p_mdhd->i_creation_time );
        MP4_GET8BYTES( p_box->data.p_mdhd->i_modification_time );
        MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
        MP4_GET8BYTES( p_box->data.p_mdhd->i_duration );
    }
    else
    {
        MP4_GET4BYTES( p_box->data.p_mdhd->i_creation_time );
        MP4_GET4BYTES( p_box->data.p_mdhd->i_modification_time );
        MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
        MP4_GET4BYTES( p_box->data.p_mdhd->i_duration );
    }
    i_language = GetWBE( p_peek ); 
    for( i = 0; i < 3; i++ )
    {
        p_box->data.p_mdhd->i_language[i] = 
                    ( ( i_language >> ( (2-i)*5 ) )&0x1f ) + 0x60;
    }

    MP4_GET2BYTES( p_box->data.p_mdhd->i_predefined );
  
#ifdef MP4_VERBOSE
    MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mdhd->i_creation_time );
    MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mdhd->i_modification_time );
    MP4_ConvertDate2Str( s_duration, p_box->data.p_mdhd->i_duration );
    msg_Dbg( p_stream->p_input, "Read Box: \"mdhd\" creation %s modification %s time scale %d duration %s language %c%c%c",
                  s_creation_time,
                  s_modification_time,
                  (u32)p_box->data.p_mdhd->i_timescale,
                  s_duration,
                  p_box->data.p_mdhd->i_language[0],
                  p_box->data.p_mdhd->i_language[1],
                  p_box->data.p_mdhd->i_language[2] );
#endif
    MP4_READBOX_EXIT( 1 );
}


int MP4_ReadBox_hdlr( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_hdlr_t ); 

    MP4_GETVERSIONFLAGS( p_box->data.p_hdlr );
 
    MP4_GET4BYTES( p_box->data.p_hdlr->i_predefined );
    MP4_GETFOURCC( p_box->data.p_hdlr->i_handler_type );

    p_box->data.p_hdlr->psz_name = calloc( sizeof( char ), i_read + 1 );
    memcpy( p_box->data.p_hdlr->psz_name, p_peek, i_read );

#ifdef MP4_VERBOSE
    msg_Dbg( p_stream->p_input, "Read Box: \"hdlr\" hanler type %c%c%c%c name %s",
                       ( p_box->data.p_hdlr->i_handler_type )&0xff,
                       ( p_box->data.p_hdlr->i_handler_type >>  8 )&0xff,
                       ( p_box->data.p_hdlr->i_handler_type >> 16 )&0xff,
                       ( p_box->data.p_hdlr->i_handler_type >> 24 )&0xff,
                       p_box->data.p_hdlr->psz_name );

#endif
    MP4_READBOX_EXIT( 1 );
}

void MP4_FreeBox_hdlr( input_thread_t *p_input, MP4_Box_t *p_box )
{
    FREE( p_box->data.p_hdlr->psz_name );
}

int MP4_ReadBox_vmhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    int i;
   
    MP4_READBOX_ENTER( MP4_Box_data_vmhd_t ); 

    MP4_GETVERSIONFLAGS( p_box->data.p_vmhd );
    
    MP4_GET2BYTES( p_box->data.p_vmhd->i_graphics_mode );
    for( i = 0; i < 3; i++ )
    {
        MP4_GET2BYTES( p_box->data.p_vmhd->i_opcolor[i] );
    }
    
#ifdef MP4_VERBOSE
    msg_Dbg( p_stream->p_input, "Read Box: \"vmhd\" graphics-mode %d opcolor (%d, %d, %d)",
                      p_box->data.p_vmhd->i_graphics_mode,
                      p_box->data.p_vmhd->i_opcolor[0],
                      p_box->data.p_vmhd->i_opcolor[1],
                      p_box->data.p_vmhd->i_opcolor[2] );
                      
#endif
    MP4_READBOX_EXIT( 1 );
}

int MP4_ReadBox_smhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_smhd_t ); 

    MP4_GETVERSIONFLAGS( p_box->data.p_smhd );



    MP4_GET2BYTES( p_box->data.p_smhd->i_balance );

    MP4_GET2BYTES( p_box->data.p_smhd->i_reserved );
    
#ifdef MP4_VERBOSE
    msg_Dbg( p_stream->p_input, "Read Box: \"smhd\" balance %f",
                      (float)p_box->data.p_smhd->i_balance / 256 );
                      
#endif
    MP4_READBOX_EXIT( 1 );
}


int MP4_ReadBox_hmhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_hmhd_t ); 

    MP4_GETVERSIONFLAGS( p_box->data.p_hmhd );

    MP4_GET2BYTES( p_box->data.p_hmhd->i_max_PDU_size );
    MP4_GET2BYTES( p_box->data.p_hmhd->i_avg_PDU_size );

    MP4_GET4BYTES( p_box->data.p_hmhd->i_max_bitrate );
    MP4_GET4BYTES( p_box->data.p_hmhd->i_avg_bitrate );

    MP4_GET4BYTES( p_box->data.p_hmhd->i_reserved );

#ifdef MP4_VERBOSE
    msg_Dbg( p_stream->p_input, "Read Box: \"hmhd\" maxPDU-size %d avgPDU-size %d max-bitrate %d avg-bitrate %d",
                      p_box->data.p_hmhd->i_max_PDU_size,
                      p_box->data.p_hmhd->i_avg_PDU_size,
                      p_box->data.p_hmhd->i_max_bitrate,
                      p_box->data.p_hmhd->i_avg_bitrate );
                      
#endif
    MP4_READBOX_EXIT( 1 );
}

int MP4_ReadBox_url( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_url_t ); 

    MP4_GETVERSIONFLAGS( p_box->data.p_url );
    MP4_GETSTRINGZ( p_box->data.p_url->psz_location );

#ifdef MP4_VERBOSE
    msg_Dbg( p_stream->p_input, "Read Box: \"url\" url: %s",
                       p_box->data.p_url->psz_location );

#endif
    MP4_READBOX_EXIT( 1 );
}


void MP4_FreeBox_url( input_thread_t *p_input, MP4_Box_t *p_box )
{
    FREE( p_box->data.p_url->psz_location )
}

int MP4_ReadBox_urn( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_urn_t );

    MP4_GETVERSIONFLAGS( p_box->data.p_urn );

    MP4_GETSTRINGZ( p_box->data.p_urn->psz_name );
    MP4_GETSTRINGZ( p_box->data.p_urn->psz_location ); 
   
#ifdef MP4_VERBOSE
    msg_Dbg( p_stream->p_input, "Read Box: \"urn\" name %s location %s",
                      p_box->data.p_urn->psz_name,
                      p_box->data.p_urn->psz_location );
#endif
    MP4_READBOX_EXIT( 1 );
}
void MP4_FreeBox_urn( input_thread_t *p_input, MP4_Box_t *p_box )
{
    FREE( p_box->data.p_urn->psz_name );
    FREE( p_box->data.p_urn->psz_location );
}


int MP4_ReadBox_dref( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_dref_t );
    
    MP4_GETVERSIONFLAGS( p_box->data.p_dref );

    MP4_GET4BYTES( p_box->data.p_dref->i_entry_count );
   
    MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 8 );
    MP4_ReadBoxContainerRaw( p_stream, p_box );

#ifdef MP4_VERBOSE
    msg_Dbg( p_stream->p_input, "Read Box: \"dref\" entry-count %d",
                      p_box->data.p_dref->i_entry_count );

#endif
    MP4_READBOX_EXIT( 1 );
}


int MP4_ReadBox_stts( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    int i;
    MP4_READBOX_ENTER( MP4_Box_data_stts_t ); 

    MP4_GETVERSIONFLAGS( p_box->data.p_stts );
    MP4_GET4BYTES( p_box->data.p_stts->i_entry_count );

    p_box->data.p_stts->i_sample_count = 
        calloc( sizeof( u32 ), p_box->data.p_stts->i_entry_count );
    p_box->data.p_stts->i_sample_delta =
        calloc( sizeof( u32 ), p_box->data.p_stts->i_entry_count );
    
    for( i = 0; (i < p_box->data.p_stts->i_entry_count )&&( i_read >=8 ); i++ )
    {
        MP4_GET4BYTES( p_box->data.p_stts->i_sample_count[i] );
        MP4_GET4BYTES( p_box->data.p_stts->i_sample_delta[i] );
    }
    
    
#ifdef MP4_VERBOSE
    msg_Dbg( p_stream->p_input, "Read Box: \"stts\" entry-count %d",
                      p_box->data.p_stts->i_entry_count );

#endif
    MP4_READBOX_EXIT( 1 );
}

void MP4_FreeBox_stts( input_thread_t *p_input, MP4_Box_t *p_box )
{
    FREE( p_box->data.p_stts->i_sample_count );
    FREE( p_box->data.p_stts->i_sample_delta );
            
}

int MP4_ReadBox_ctts( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    int i;
    MP4_READBOX_ENTER( MP4_Box_data_ctts_t );
    
    MP4_GETVERSIONFLAGS( p_box->data.p_ctts );
 
    MP4_GET4BYTES( p_box->data.p_ctts->i_entry_count );

    p_box->data.p_ctts->i_sample_count = 
        calloc( sizeof( u32 ), p_box->data.p_ctts->i_entry_count );
    p_box->data.p_ctts->i_sample_offset =
        calloc( sizeof( u32 ), p_box->data.p_ctts->i_entry_count );
    
    for( i = 0; (i < p_box->data.p_ctts->i_entry_count )&&( i_read >=8 ); i++ )
    {
        MP4_GET4BYTES( p_box->data.p_ctts->i_sample_count[i] );
        MP4_GET4BYTES( p_box->data.p_ctts->i_sample_offset[i] );
    }
    
    
#ifdef MP4_VERBOSE
    msg_Dbg( p_stream->p_input, "Read Box: \"ctts\" entry-count %d",
                      p_box->data.p_ctts->i_entry_count );

#endif
    MP4_READBOX_EXIT( 1 );
}

void MP4_FreeBox_ctts( input_thread_t *p_input, MP4_Box_t *p_box )
{
    FREE( p_box->data.p_ctts->i_sample_count );
    FREE( p_box->data.p_ctts->i_sample_offset );
}

static int MP4_ReadLengthDescriptor( u8 **pp_peek, s64 *i_read )
{
    int i_b;
    int i_len = 0;
    do
    {
        i_b = **pp_peek;

        (*pp_peek)++;
        (*i_read)--;
        i_len = ( i_len << 7 ) + ( i_b&0x7f );
    } while( i_b&0x80 );
    return( i_len );    
}

int MP4_ReadBox_esds( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
#define es_descriptor p_box->data.p_esds->es_descriptor
    int i_len;
    int i_flags;
    int i_type;

    MP4_READBOX_ENTER( MP4_Box_data_esds_t );

    MP4_GETVERSIONFLAGS( p_box->data.p_esds );


    MP4_GET1BYTE( i_type );
    if( i_type == 0x03 ) /* MP4ESDescrTag */
    {
        i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );

        MP4_GET2BYTES( es_descriptor.i_ES_ID );
        MP4_GET1BYTE( i_flags );
        es_descriptor.b_stream_dependence = ( (i_flags&0x80) != 0);
        es_descriptor.b_url = ( (i_flags&0x40) != 0);
        es_descriptor.b_OCRstream = ( (i_flags&0x20) != 0);

        es_descriptor.i_stream_priority = i_flags&0x1f;
        if( es_descriptor.b_stream_dependence )
        {
            MP4_GET2BYTES( es_descriptor.i_depend_on_ES_ID );
        }
        if( es_descriptor.b_url )
        {
            int i_len;
        
            MP4_GET1BYTE( i_len );
            es_descriptor.psz_URL = calloc( sizeof(char), i_len + 1 );
            memcpy( es_descriptor.psz_URL, p_peek, i_len );
            es_descriptor.psz_URL[i_len] = 0;
            p_peek += i_len;
            i_read -= i_len;
        }
        else
        {
            es_descriptor.psz_URL = NULL;
        }
        if( es_descriptor.b_OCRstream )
        {
            MP4_GET2BYTES( es_descriptor.i_OCR_ES_ID );
        }
        MP4_GET1BYTE( i_type ); /* get next type */
    }

    if( i_type != 0x04)/* MP4DecConfigDescrTag */
    {
        MP4_READBOX_EXIT( 1 ); /* rest isn't interesting up to now */
    }

    i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
    es_descriptor.p_decConfigDescr = 
            malloc( sizeof( MP4_descriptor_decoder_config_t ));

    MP4_GET1BYTE( es_descriptor.p_decConfigDescr->i_objectTypeIndication );
    MP4_GET1BYTE( i_flags );
    es_descriptor.p_decConfigDescr->i_streamType = i_flags >> 2;
    es_descriptor.p_decConfigDescr->b_upStream = ( i_flags >> 1 )&0x01;
    MP4_GET3BYTES( es_descriptor.p_decConfigDescr->i_buffer_sizeDB );
    MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_max_bitrate );
    MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_avg_bitrate );
    MP4_GET1BYTE( i_type );
    if( i_type !=  0x05 )/* MP4DecSpecificDescrTag */
    {
        MP4_READBOX_EXIT( 1 );
    }
   
    i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
    es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = i_len;
    es_descriptor.p_decConfigDescr->p_decoder_specific_info = malloc( i_len );
    memcpy( es_descriptor.p_decConfigDescr->p_decoder_specific_info, 
            p_peek, i_len );

    MP4_READBOX_EXIT( 1 );

#undef es_descriptor
}

void MP4_FreeBox_esds( input_thread_t *p_input, MP4_Box_t *p_box )
{
    FREE( p_box->data.p_esds->es_descriptor.psz_URL );
    FREE( p_box->data.p_esds->es_descriptor.p_decConfigDescr );
}

int MP4_ReadBox_sample_soun( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    int i;    
    
    MP4_READBOX_ENTER( MP4_Box_data_sample_soun_t );

    for( i = 0; i < 6 ; i++ )
    {
        MP4_GET1BYTE( p_box->data.p_sample_soun->i_reserved1[i] );
    }

    MP4_GET2BYTES( p_box->data.p_sample_soun->i_data_reference_index );
    
    for( i = 0; i < 2 ; i++ )
    {
        MP4_GET4BYTES( p_box->data.p_sample_soun->i_reserved2[i] );
    }
   
    MP4_GET2BYTES( p_box->data.p_sample_soun->i_channelcount );
    MP4_GET2BYTES( p_box->data.p_sample_soun->i_samplesize );
    MP4_GET2BYTES( p_box->data.p_sample_soun->i_predefined );
    MP4_GET2BYTES( p_box->data.p_sample_soun->i_reserved3 );
    MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratehi );
    MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratelo );
    
    MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 28 );
    MP4_ReadBoxContainerRaw( p_stream, p_box ); /* esds */
   
#ifdef MP4_VERBOSE
    msg_Dbg( p_stream->p_input, "Read Box: \"soun\" in stsd channel %d sample size %d sampl rate %f",
                      p_box->data.p_sample_soun->i_channelcount,
                      p_box->data.p_sample_soun->i_samplesize,
                      (float)p_box->data.p_sample_soun->i_sampleratehi + 
                    (float)p_box->data.p_sample_soun->i_sampleratelo / 65536 );

#endif
    MP4_READBOX_EXIT( 1 );
}

#if 0
int MP4_ReadBox_sample_mp4a( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    int i;    

    MP4_READBOX_ENTER( MP4_Box_data_sample_mp4a_t );
    
    for( i = 0; i < 6 ; i++ )
    {
        MP4_GET1BYTE( p_box->data.p_sample_mp4a->i_reserved1[i] );
    }

    MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_data_reference_index );
    
    for( i = 0; i < 2 ; i++ )
    {
        MP4_GET4BYTES( p_box->data.p_sample_mp4a->i_reserved2[i] );
    }
   
    MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_channelcount );
    MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_samplesize );
    MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_predefined );
    MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_reserved3 );
    MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_sampleratehi );
    MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_sampleratelo );
    
    MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 28 );
    MP4_ReadBoxContainerRaw( p_stream, p_box ); /* esds */

#ifdef MP4_VERBOSE
    msg_Dbg( p_stream->p_input, "Read Box: \"mp4a\" in stsd channel %d sample size %d sampl rate %f",
                      p_box->data.p_sample_mp4a->i_channelcount,
                      p_box->data.p_sample_mp4a->i_samplesize,
                      (float)p_box->data.p_sample_mp4a->i_sampleratehi + 
                        (float)p_box->data.p_sample_mp4a->i_sampleratelo / 65536 );

#endif
    MP4_READBOX_EXIT( 1 );
}
#endif

int MP4_ReadBox_sample_vide( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    int i;    

    MP4_READBOX_ENTER( MP4_Box_data_sample_vide_t );

    for( i = 0; i < 6 ; i++ )
    {
        MP4_GET1BYTE( p_box->data.p_sample_vide->i_reserved1[i] );
    }

    MP4_GET2BYTES( p_box->data.p_sample_vide->i_data_reference_index );

    MP4_GET2BYTES( p_box->data.p_sample_vide->i_predefined1 );
    MP4_GET2BYTES( p_box->data.p_sample_vide->i_reserved2 );

    for( i = 0; i < 3 ; i++ )
    {
        MP4_GET4BYTES( p_box->data.p_sample_vide->i_predefined2[i] );
    }

    MP4_GET2BYTES( p_box->data.p_sample_vide->i_width );
    MP4_GET2BYTES( p_box->data.p_sample_vide->i_height );
    
    MP4_GET4BYTES( p_box->data.p_sample_vide->i_horizresolution );
    MP4_GET4BYTES( p_box->data.p_sample_vide->i_vertresolution );

    MP4_GET4BYTES( p_box->data.p_sample_vide->i_reserved3 );
    MP4_GET2BYTES( p_box->data.p_sample_vide->i_predefined3 );

    memcpy( &p_box->data.p_sample_vide->i_compressorname, p_peek, 32 );
    p_peek += 32; i_read -= 32;

    MP4_GET2BYTES( p_box->data.p_sample_vide->i_depth );
    MP4_GET2BYTES( p_box->data.p_sample_vide->i_predefined4 );
    
    MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 78);
    MP4_ReadBoxContainerRaw( p_stream, p_box );
   
#ifdef MP4_VERBOSE
    msg_Dbg( p_stream->p_input, "Read Box: \"vide\" in stsd %dx%d depth %d",
                      p_box->data.p_sample_vide->i_width,
                      p_box->data.p_sample_vide->i_height,
                      p_box->data.p_sample_vide->i_depth );

#endif
    MP4_READBOX_EXIT( 1 );
}


int MP4_ReadBox_stsd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{

    MP4_READBOX_ENTER( MP4_Box_data_stsd_t );

    MP4_GETVERSIONFLAGS( p_box->data.p_stsd );

    MP4_GET4BYTES( p_box->data.p_stsd->i_entry_count );

    MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 8 ); 

    MP4_ReadBoxContainerRaw( p_stream, p_box );

#ifdef MP4_VERBOSE
    msg_Dbg( p_stream->p_input, "Read Box: \"stsd\" entry-count %d",
                      p_box->data.p_stsd->i_entry_count );

#endif
    MP4_READBOX_EXIT( 1 );
}


int MP4_ReadBox_stsz( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    int i;

    MP4_READBOX_ENTER( MP4_Box_data_stsz_t );
    
    MP4_GETVERSIONFLAGS( p_box->data.p_stsz );
    
    MP4_GET4BYTES( p_box->data.p_stsz->i_sample_size );

    MP4_GET4BYTES( p_box->data.p_stsz->i_sample_count );

    p_box->data.p_stsz->i_entry_size = 
        calloc( sizeof( u32 ), p_box->data.p_stsz->i_sample_count );
    
    if( !p_box->data.p_stsz->i_sample_size )
    {
        for( i=0; (i<p_box->data.p_stsz->i_sample_count)&&(i_read >= 4 ); i++ )
        {
            MP4_GET4BYTES( p_box->data.p_stsz->i_entry_size[i] );
        }
    }
    
    
#ifdef MP4_VERBOSE 
    msg_Dbg( p_stream->p_input, "Read Box: \"stsz\" sample-size %d sample-count %d",
                      p_box->data.p_stsz->i_sample_size,
                      p_box->data.p_stsz->i_sample_count );

#endif
    MP4_READBOX_EXIT( 1 );
}

void MP4_FreeBox_stsz( input_thread_t *p_input, MP4_Box_t *p_box )
{
    FREE( p_box->data.p_stsz->i_entry_size );
}

int MP4_ReadBox_stsc( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    int i;
    
    MP4_READBOX_ENTER( MP4_Box_data_stsc_t );

    MP4_GETVERSIONFLAGS( p_box->data.p_stsc );
    
    MP4_GET4BYTES( p_box->data.p_stsc->i_entry_count );

    p_box->data.p_stsc->i_first_chunk = 
        calloc( sizeof( u32 ), p_box->data.p_stsc->i_entry_count );
    p_box->data.p_stsc->i_samples_per_chunk = 
        calloc( sizeof( u32 ), p_box->data.p_stsc->i_entry_count );
    p_box->data.p_stsc->i_sample_description_index = 
        calloc( sizeof( u32 ), p_box->data.p_stsc->i_entry_count );

    for( i = 0; (i < p_box->data.p_stsc->i_entry_count )&&( i_read >= 12 );i++ )
    {
        MP4_GET4BYTES( p_box->data.p_stsc->i_first_chunk[i] );
        MP4_GET4BYTES( p_box->data.p_stsc->i_samples_per_chunk[i] );
        MP4_GET4BYTES( p_box->data.p_stsc->i_sample_description_index[i] );
    }
    
    
#ifdef MP4_VERBOSE 
    msg_Dbg( p_stream->p_input, "Read Box: \"stsc\" entry-count %d",
                      p_box->data.p_stsc->i_entry_count );

#endif
    MP4_READBOX_EXIT( 1 );
}

void MP4_FreeBox_stsc( input_thread_t *p_input, MP4_Box_t *p_box )
{
    FREE( p_box->data.p_stsc->i_first_chunk );
    FREE( p_box->data.p_stsc->i_samples_per_chunk );
    FREE( p_box->data.p_stsc->i_sample_description_index );
}

int MP4_ReadBox_stco_co64( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    int i;
    
    MP4_READBOX_ENTER( MP4_Box_data_co64_t );

    MP4_GETVERSIONFLAGS( p_box->data.p_co64 );

    MP4_GET4BYTES( p_box->data.p_co64->i_entry_count );

    p_box->data.p_co64->i_chunk_offset = 
        calloc( sizeof( u64 ), p_box->data.p_co64->i_entry_count );

1432
    for( i = 0; i < p_box->data.p_co64->i_entry_count; i++ )
1433 1434 1435
    {
        if( p_box->i_type == FOURCC_stco )
        {
1436 1437 1438 1439
            if( i_read < 4 )
            {
                break;
            }
1440 1441 1442 1443
            MP4_GET4BYTES( p_box->data.p_co64->i_chunk_offset[i] );
        }
        else
        {
1444 1445 1446 1447
            if( i_read < 8 )
            {
                break;
            }
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
            MP4_GET8BYTES( p_box->data.p_co64->i_chunk_offset[i] );
        }
    }
    
    
#ifdef MP4_VERBOSE 
    msg_Dbg( p_stream->p_input, "Read Box: \"co64\" entry-count %d",
                      p_box->data.p_co64->i_entry_count );

#endif
    MP4_READBOX_EXIT( 1 );
}

void MP4_FreeBox_stco_co64( input_thread_t *p_input, MP4_Box_t *p_box )
{
    FREE( p_box->data.p_co64->i_chunk_offset );
}

int MP4_ReadBox_stss( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    int i;
    
    MP4_READBOX_ENTER( MP4_Box_data_stss_t ); 

    MP4_GETVERSIONFLAGS( p_box->data.p_stss );

    MP4_GET4BYTES( p_box->data.p_stss->i_entry_count );

    p_box->data.p_stss->i_sample_number = 
        calloc( sizeof( u32 ), p_box->data.p_stss->i_entry_count );

    for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 4 ); i++ )
    {

        MP4_GET4BYTES( p_box->data.p_stss->i_sample_number[i] );
1483 1484
        /* XXX in libmp4 sample begin at 0 */
        p_box->data.p_stss->i_sample_number[i]--;
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
    }
    
    
#ifdef MP4_VERBOSE 
    msg_Dbg( p_stream->p_input, "Read Box: \"stss\" entry-count %d",
                      p_box->data.p_stss->i_entry_count );

#endif
    MP4_READBOX_EXIT( 1 );
}

void MP4_FreeBox_stss( input_thread_t *p_input, MP4_Box_t *p_box )
{
    FREE( p_box->data.p_stss->i_sample_number )
}

int MP4_ReadBox_stsh( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    int i;
    
    MP4_READBOX_ENTER( MP4_Box_data_stsh_t );

    MP4_GETVERSIONFLAGS( p_box->data.p_stsh );


    MP4_GET4BYTES( p_box->data.p_stsh->i_entry_count );

    p_box->data.p_stsh->i_shadowed_sample_number = 
        calloc( sizeof( u32 ), p_box->data.p_stsh->i_entry_count );

    p_box->data.p_stsh->i_sync_sample_number = 
        calloc( sizeof( u32 ), p_box->data.p_stsh->i_entry_count );


    for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 8 ); i++ )
    {

        MP4_GET4BYTES( p_box->data.p_stsh->i_shadowed_sample_number[i] );
        MP4_GET4BYTES( p_box->data.p_stsh->i_sync_sample_number[i] );
    }
    
    
#ifdef MP4_VERBOSE 
    msg_Dbg( p_stream->p_input, "Read Box: \"stsh\" entry-count %d",
                      p_box->data.p_stsh->i_entry_count );
#endif
    MP4_READBOX_EXIT( 1 );
}

void MP4_FreeBox_stsh( input_thread_t *p_input, MP4_Box_t *p_box )
{
    FREE( p_box->data.p_stsh->i_shadowed_sample_number )
    FREE( p_box->data.p_stsh->i_sync_sample_number )
}


int MP4_ReadBox_stdp( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    int i;
   
    MP4_READBOX_ENTER( MP4_Box_data_stdp_t ); 

    MP4_GETVERSIONFLAGS( p_box->data.p_stdp );

    p_box->data.p_stdp->i_priority = 
        calloc( sizeof( u16 ), i_read / 2 );

    for( i = 0; i < i_read / 2 ; i++ )
    {

        MP4_GET2BYTES( p_box->data.p_stdp->i_priority[i] );
    }
    
    
#ifdef MP4_VERBOSE 
    msg_Dbg( p_stream->p_input, "Read Box: \"stdp\" entry-count %d",
                      i_read / 2 );

#endif
    MP4_READBOX_EXIT( 1 );
}

void MP4_FreeBox_stdp( input_thread_t *p_input, MP4_Box_t *p_box )
{
    FREE( p_box->data.p_stdp->i_priority )
}

int MP4_ReadBox_padb( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    int i;
    
    MP4_READBOX_ENTER( MP4_Box_data_padb_t );

    MP4_GETVERSIONFLAGS( p_box->data.p_padb );


    MP4_GET4BYTES( p_box->data.p_padb->i_sample_count );

    p_box->data.p_padb->i_reserved1 = 
        calloc( sizeof( u16 ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
    p_box->data.p_padb->i_pad2 = 
        calloc( sizeof( u16 ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
    p_box->data.p_padb->i_reserved2 = 
        calloc( sizeof( u16 ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
    p_box->data.p_padb->i_pad1 = 
        calloc( sizeof( u16 ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );


    for( i = 0; i < i_read / 2 ; i++ )
    {
        p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 7 )&0x01;
        p_box->data.p_padb->i_pad2[i] = ( (*p_peek) >> 4 )&0x07;
        p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 3 )&0x01;
        p_box->data.p_padb->i_pad1[i] = ( (*p_peek) )&0x07;

        p_peek += 1; i_read -= 1;
    }
    
    
#ifdef MP4_VERBOSE 
    msg_Dbg( p_stream->p_input, "Read Box: \"stdp\" entry-count %d",
                      i_read / 2 );

#endif
    MP4_READBOX_EXIT( 1 );
}

void MP4_FreeBox_padb( input_thread_t *p_input, MP4_Box_t *p_box )
{
    FREE( p_box->data.p_padb->i_reserved1 );
    FREE( p_box->data.p_padb->i_pad2 );
    FREE( p_box->data.p_padb->i_reserved2 );
    FREE( p_box->data.p_padb->i_pad1 );
}

int MP4_ReadBox_elst( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    int i;
    
    MP4_READBOX_ENTER( MP4_Box_data_padb_t );

    MP4_GETVERSIONFLAGS( p_box->data.p_elst );


    MP4_GET4BYTES( p_box->data.p_elst->i_entry_count );

    p_box->data.p_elst->i_segment_duration =
        calloc( sizeof( u64 ), p_box->data.p_elst->i_entry_count );
    p_box->data.p_elst->i_media_time =
        calloc( sizeof( u64 ), p_box->data.p_elst->i_entry_count );
    p_box->data.p_elst->i_media_rate_integer =
        calloc( sizeof( u16 ), p_box->data.p_elst->i_entry_count );
    p_box->data.p_elst->i_media_rate_fraction= 
        calloc( sizeof( u16 ), p_box->data.p_elst->i_entry_count );


    for( i = 0; i < p_box->data.p_elst->i_entry_count; i++ )
    {
        if( p_box->data.p_elst->i_version == 1 )
        {

            MP4_GET8BYTES( p_box->data.p_elst->i_segment_duration[i] );

            MP4_GET8BYTES( p_box->data.p_elst->i_media_time[i] );
        }
        else
        {

            MP4_GET4BYTES( p_box->data.p_elst->i_segment_duration[i] );

            MP4_GET4BYTES( p_box->data.p_elst->i_media_time[i] );
        }

        MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_integer[i] );
        MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_fraction[i] );
    }
    
    
#ifdef MP4_VERBOSE 
    msg_Dbg( p_stream->p_input, "Read Box: \"elst\" entry-count %d",
                      i_read / 2 );

#endif
    MP4_READBOX_EXIT( 1 );
}

void MP4_FreeBox_elst( input_thread_t *p_input, MP4_Box_t *p_box )
{
    FREE( p_box->data.p_elst->i_segment_duration );
    FREE( p_box->data.p_elst->i_media_time );
    FREE( p_box->data.p_elst->i_media_rate_integer );
    FREE( p_box->data.p_elst->i_media_rate_fraction );
}

int MP4_ReadBox_cprt( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    int i_language;
    int i;
    
    MP4_READBOX_ENTER( MP4_Box_data_cprt_t );

    MP4_GETVERSIONFLAGS( p_box->data.p_cprt );

    i_language = GetWBE( p_peek ); 
    for( i = 0; i < 3; i++ )
    {
        p_box->data.p_cprt->i_language[i] = 
            ( ( i_language >> ( (2-i)*5 ) )&0x1f ) + 0x60;
    }
    p_peek += 2; i_read -= 2;  
    MP4_GETSTRINGZ( p_box->data.p_cprt->psz_notice );

#ifdef MP4_VERBOSE
    msg_Dbg( p_stream->p_input, "Read Box: \"cprt\" language %c%c%c notice %s",
                      p_box->data.p_cprt->i_language[0],
                      p_box->data.p_cprt->i_language[1],
                      p_box->data.p_cprt->i_language[2],
                      p_box->data.p_cprt->psz_notice );

#endif
    MP4_READBOX_EXIT( 1 );
}

void MP4_FreeBox_cprt( input_thread_t *p_input, MP4_Box_t *p_box )
{
    FREE( p_box->data.p_cprt->psz_notice );
}


int MP4_ReadBox_dcom( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_dcom_t );
    
    MP4_GETFOURCC( p_box->data.p_dcom->i_algorithm );
#ifdef MP4_VERBOSE
    msg_Dbg( p_stream->p_input, 
             "Read Box: \"dcom\" compression algorithm : %c%c%c%c",
                      ( p_box->data.p_dcom->i_algorithm )&0xff,
                      ( p_box->data.p_dcom->i_algorithm >> 8 )&0xff,
                      ( p_box->data.p_dcom->i_algorithm >> 16 )&0xff,
                      ( p_box->data.p_dcom->i_algorithm >> 24 )&0xff );
#endif 
    
    MP4_READBOX_EXIT( 1 );
}

int MP4_ReadBox_cmvd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_cmvd_t );

    
    MP4_GET4BYTES( p_box->data.p_cmvd->i_uncompressed_size );

    p_box->data.p_cmvd->i_compressed_size = i_read;
    
    if( !( p_box->data.p_cmvd->p_data = malloc( i_read ) ) )
    {
        msg_Dbg( p_stream->p_input, "Read Box: \"cmvd\" not enough memory to load data" );
        return( 1 );
    }
    
    /* now copy compressed data */
    memcpy( p_box->data.p_cmvd->p_data,
            p_peek,
            i_read);
    
    p_box->data.p_cmvd->b_compressed = 1;
     
#ifdef MP4_VERBOSE
    msg_Dbg( p_stream->p_input, "Read Box: \"cmvd\" compressed data size %d",
                      p_box->data.p_cmvd->i_compressed_size );
#endif 
    
    MP4_READBOX_EXIT( 1 );
}
void MP4_FreeBox_cmvd( input_thread_t *p_input, MP4_Box_t *p_box )
{
    FREE( p_box->data.p_cmvd->p_data );
}

1765

1766 1767 1768 1769 1770 1771 1772
int MP4_ReadBox_cmov( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_Stream_t *p_stream_memory;
    MP4_Box_t *p_umov;
    
    MP4_Box_t *p_dcom;
    MP4_Box_t *p_cmvd;
Gildas Bazin's avatar
 
Gildas Bazin committed
1773
#ifdef HAVE_ZLIB_H
1774
    z_stream  z_data;
Gildas Bazin's avatar
 
Gildas Bazin committed
1775
#endif
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
    u8 *p_data;
    
    int i_result;

    if( !( p_box->data.p_cmov = malloc( sizeof( MP4_Box_data_cmov_t ) ) ) )
    {
        msg_Err( p_stream->p_input, "out of memory" );
        return( 0 );
    }
    memset( p_box->data.p_cmov, 0, sizeof( MP4_Box_data_cmov_t ) );
    
    if( !( p_box->p_father )||
        ( p_box->p_father->i_type != FOURCC_moov ) )
    {
        msg_Warn( p_stream->p_input, "Read box: \"cmov\" box alone" );
        return( 1 );
    }

    if( !(i_result = MP4_ReadBoxContainer( p_stream, p_box ) ) )
    {
        return( 0 );
    }
    
    if( !( p_dcom = MP4_FindBox( p_box, FOURCC_dcom ) )||
        !( p_cmvd = MP4_FindBox( p_box, FOURCC_cmvd ) )||
        !( p_cmvd->data.p_cmvd->p_data ) )
    {
        msg_Warn( p_stream->p_input, "Read Box: \"cmov\" incomplete" );
        return( 1 );
    }

    if( p_dcom->data.p_dcom->i_algorithm != FOURCC_zlib )
    {
        msg_Dbg( p_stream->p_input, "Read Box: \"cmov\" compression algorithm : %c%c%c%c not supported",
                    ( p_dcom->data.p_dcom->i_algorithm )&0xff,
                    ( p_dcom->data.p_dcom->i_algorithm >> 8 )&0xff,
                    ( p_dcom->data.p_dcom->i_algorithm >> 16 )&0xff,
                    ( p_dcom->data.p_dcom->i_algorithm >> 24 )&0xff );
        return( 1 );
    }
1816 1817 1818 1819 1820 1821

#ifndef HAVE_ZLIB_H
    msg_Dbg( p_stream->p_input,
             "Read Box: \"cmov\" zlib unsupported" );
    return( 1 );
#else 
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
    /* decompress data */
    /* allocate a new buffer */
    if( !( p_data = malloc( p_cmvd->data.p_cmvd->i_uncompressed_size ) ) )
    {
        msg_Err( p_stream->p_input, 
                 "Read Box: \"cmov\" not enough memory to uncompress data" );
        return( 1 );
    }
    /* init default structures */
    z_data.next_in   = p_cmvd->data.p_cmvd->p_data;
    z_data.avail_in  = p_cmvd->data.p_cmvd->i_compressed_size;
    z_data.next_out  = p_data;
    z_data.avail_out = p_cmvd->data.p_cmvd->i_uncompressed_size;
    z_data.zalloc    = (alloc_func)Z_NULL;
    z_data.zfree     = (free_func)Z_NULL;
    z_data.opaque    = (voidpf)Z_NULL;

    /* init zlib */
    if( ( i_result = inflateInit( &z_data ) ) != Z_OK )
    {
        msg_Err( p_stream->p_input, 
                 "Read Box: \"cmov\" error while uncompressing data" );
        free( p_data );
        return( 1 );
    }

    /* uncompress */
    i_result = inflate( &z_data, Z_NO_FLUSH );
    if( ( i_result != Z_OK )&&( i_result != Z_STREAM_END ) )
    {
        msg_Err( p_stream->p_input, 
                 "Read Box: \"cmov\" error while uncompressing data" );
        free( p_data );
        return( 1 );
    }

    if( p_cmvd->data.p_cmvd->i_uncompressed_size != z_data.total_out )
    {
        msg_Warn( p_stream->p_input, 
                  "Read Box: \"cmov\" uncompressing data size mismatch" );
    }
    p_cmvd->data.p_cmvd->i_uncompressed_size = z_data.total_out;

    /* close zlib */ 
    i_result = inflateEnd( &z_data );
    if( i_result != Z_OK )
    {
        msg_Warn( p_stream->p_input, 
           "Read Box: \"cmov\" error while uncompressing data (ignored)" );
    }


    free( p_cmvd->data.p_cmvd->p_data );
    p_cmvd->data.p_cmvd->p_data = p_data;
    p_cmvd->data.p_cmvd->b_compressed = 0;

    msg_Dbg( p_stream->p_input, 
             "Read Box: \"cmov\" box succesfully uncompressed" );

    /* now create a memory stream */
    p_stream_memory = MP4_MemoryStream( p_stream->p_input, 
                                        p_cmvd->data.p_cmvd->i_uncompressed_size,
                                        p_cmvd->data.p_cmvd->p_data );

    /* and read uncompressd moov */
    p_umov = malloc( sizeof( MP4_Box_t ) );
    
    i_result = MP4_ReadBox( p_stream_memory, p_umov, NULL );
  
    p_box->data.p_cmov->p_moov = p_umov;
    free( p_stream_memory );

#ifdef MP4_VERBOSE
    msg_Dbg( p_stream->p_input, 
             "Read Box: \"cmov\" compressed movie header completed" );
#endif
    return( i_result );
1899
#endif /* HAVE_ZLIB_H */
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
}


/**** ------------------------------------------------------------------- ****/
/****                   "Higher level" Functions                          ****/
/**** ------------------------------------------------------------------- ****/

static struct 
{
    u32 i_type;
    int  (*MP4_ReadBox_function )( MP4_Stream_t *p_stream, MP4_Box_t *p_box ); 
    void (*MP4_FreeBox_function )( input_thread_t *p_input, MP4_Box_t *p_box );
} MP4_Box_Function [] =
{
    /* Containers */
    { FOURCC_moov,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
    { FOURCC_trak,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
    { FOURCC_mdia,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
    { FOURCC_moof,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
    { FOURCC_minf,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
    { FOURCC_stbl,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
    { FOURCC_dinf,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
    { FOURCC_edts,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
    { FOURCC_udta,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
    { FOURCC_nmhd,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
    { FOURCC_hnti,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },

    /* specific box */
    { FOURCC_ftyp,  MP4_ReadBox_ftyp,       MP4_FreeBox_ftyp },
    { FOURCC_cmov,  MP4_ReadBox_cmov,       MP4_FreeBox_Common },
    { FOURCC_mvhd,  MP4_ReadBox_mvhd,       MP4_FreeBox_Common },
    { FOURCC_tkhd,  MP4_ReadBox_tkhd,       MP4_FreeBox_Common },
    { FOURCC_tref,  MP4_ReadBox_tref,       MP4_FreeBox_Common },
    { FOURCC_mdhd,  MP4_ReadBox_mdhd,       MP4_FreeBox_Common },
    { FOURCC_hdlr,  MP4_ReadBox_hdlr,       MP4_FreeBox_hdlr },
    { FOURCC_vmhd,  MP4_ReadBox_vmhd,       MP4_FreeBox_Common },
    { FOURCC_smhd,  MP4_ReadBox_smhd,       MP4_FreeBox_Common },
    { FOURCC_hmhd,  MP4_ReadBox_hmhd,       MP4_FreeBox_Common },
    { FOURCC_url,   MP4_ReadBox_url,        MP4_FreeBox_url },
    { FOURCC_urn,   MP4_ReadBox_urn,        MP4_FreeBox_urn },
    { FOURCC_dref,  MP4_ReadBox_dref,       MP4_FreeBox_Common },
    { FOURCC_stts,  MP4_ReadBox_stts,       MP4_FreeBox_stts },
    { FOURCC_ctts,  MP4_ReadBox_ctts,       MP4_FreeBox_ctts },
    { FOURCC_stsd,  MP4_ReadBox_stsd,       MP4_FreeBox_Common },
    { FOURCC_stsz,  MP4_ReadBox_stsz,       MP4_FreeBox_stsz },
    { FOURCC_stsc,  MP4_ReadBox_stsc,       MP4_FreeBox_stsc },
    { FOURCC_stco,  MP4_ReadBox_stco_co64,  MP4_FreeBox_stco_co64 },
    { FOURCC_co64,  MP4_ReadBox_stco_co64,  MP4_FreeBox_stco_co64 },
    { FOURCC_stss,  MP4_ReadBox_stss,       MP4_FreeBox_stss },
    { FOURCC_stsh,  MP4_ReadBox_stsh,       MP4_FreeBox_stsh },
    { FOURCC_stdp,  MP4_ReadBox_stdp,       MP4_FreeBox_stdp },
    { FOURCC_padb,  MP4_ReadBox_padb,       MP4_FreeBox_padb },
    { FOURCC_elst,  MP4_ReadBox_elst,       MP4_FreeBox_elst },
    { FOURCC_cprt,  MP4_ReadBox_cprt,       MP4_FreeBox_cprt },
    { FOURCC_esds,  MP4_ReadBox_esds,       MP4_FreeBox_esds },
    { FOURCC_dcom,  MP4_ReadBox_dcom,       MP4_FreeBox_Common },
    { FOURCC_cmvd,  MP4_ReadBox_cmvd,       MP4_FreeBox_cmvd },

    /* Nothing to do with this box */
    { FOURCC_mdat,  MP4_ReadBoxSkip,        MP4_FreeBox_Common },
    { FOURCC_skip,  MP4_ReadBoxSkip,        MP4_FreeBox_Common },
    { FOURCC_free,  MP4_ReadBoxSkip,        MP4_FreeBox_Common },
    { FOURCC_wide,  MP4_ReadBoxSkip,        MP4_FreeBox_Common },

    /* for codecs */
    { FOURCC_soun,  MP4_ReadBox_sample_soun,    MP4_FreeBox_Common },
    { FOURCC__mp3,  MP4_ReadBox_sample_soun,    MP4_FreeBox_Common },
    { FOURCC_ms55,  MP4_ReadBox_sample_soun,    MP4_FreeBox_Common },
    { FOURCC_mp4a,  MP4_ReadBox_sample_soun,    MP4_FreeBox_Common },

    { FOURCC_vide,  MP4_ReadBox_sample_vide,    MP4_FreeBox_Common },
    { FOURCC_mp4v,  MP4_ReadBox_sample_vide,    MP4_FreeBox_Common },
    { FOURCC_SVQ1,  MP4_ReadBox_sample_vide,    MP4_FreeBox_Common },
    { FOURCC_DIVX,  MP4_ReadBox_sample_vide,    MP4_FreeBox_Common },
    { FOURCC_h263,  MP4_ReadBox_sample_vide,    MP4_FreeBox_Common },
    { FOURCC_cvid,  MP4_ReadBox_sample_vide,    MP4_FreeBox_Common },
    { FOURCC_3IV1,  MP4_ReadBox_sample_vide,    MP4_FreeBox_Common },
    { FOURCC_3IV2,  MP4_ReadBox_sample_vide,    MP4_FreeBox_Common },
    { FOURCC_mjpa,  MP4_ReadBox_sample_vide,    MP4_FreeBox_Common },
    { FOURCC_mjpb,  MP4_ReadBox_sample_vide,    MP4_FreeBox_Common },
    { FOURCC_mjqt,  NULL,                       NULL }, /* found in mjpa/b */
    { FOURCC_mjht,  NULL,                       NULL },
 
    { FOURCC_jpeg,  MP4_ReadBox_sample_vide,    MP4_FreeBox_Common },

    { FOURCC_mp4s,  NULL,                       MP4_FreeBox_Common },

    /* XXX there is 2 box where we could find this entry stbl and tref*/
    { FOURCC_hint,  NULL,                       MP4_FreeBox_Common },

    /* found in tref box */
    { FOURCC_dpnd,  NULL,   NULL },
    { FOURCC_ipir,  NULL,   NULL },
    { FOURCC_mpod,  NULL,   NULL },
    
    /* found in hnti */
    { FOURCC_rtp,   NULL,   NULL },
    
    /* Last entry */
    
    { 0,            NULL,                   NULL }
};



/*****************************************************************************
 * MP4_ReadBox : parse the actual box and the children 
 *  XXX : Do not go to the next box
 *****************************************************************************/
int MP4_ReadBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father )
{
    int i_result;
    int i_index;
    
    if( !MP4_ReadBoxCommon( p_stream, p_box ) )
    {
        msg_Warn( p_stream->p_input, "Cannot read one box" );
        return( 0 );
    }
    if( !p_box->i_size )
    {
        msg_Dbg( p_stream->p_input, "Found an empty box (null size)" );
        return( 0 );
    }
    p_box->p_father = p_father;

    /* Now search function to call */
    for( i_index = 0; ; i_index++ )
    {
        if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
            ( MP4_Box_Function[i_index].i_type == 0 ) )
        {
            break;
        }
    }
    if( MP4_Box_Function[i_index].MP4_ReadBox_function == NULL )
    {
        msg_Warn( p_stream->p_input,
                  "Unknown box type %c%c%c%c (uncompletetly loaded)",
                  (p_box->i_type)&0xff, 
                  (p_box->i_type>>8)&0xff,
                  (p_box->i_type>>16)&0xff,
                  (p_box->i_type>>24)&0xff );
        return( 1 );
    }
    else
    {
        i_result = 
           (MP4_Box_Function[i_index].MP4_ReadBox_function)( p_stream, p_box );
    }

    if( !MP4_GotoBox( p_stream, p_box ) )
    {
        return( 0 );
    }
    return( i_result );
}
2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162
#if 0
/*****************************************************************************
 * MP4_CountBox: given a box, count how many child have the requested type 
 * FIXME : support GUUID 
 *****************************************************************************/
int MP4_CountBox( MP4_Box_t *p_box, u32 i_type )
{
    int i_count;
    MP4_Box_t *p_child;
    
    if( !p_box )
    {
        return( 0 );
    }

    i_count = 0;
    p_child = p_box->p_first;
    while( p_child )
    {
        if( p_child->i_type == i_type )
        {   
            i_count++;
        }
        p_child = p_child->p_next;
    }
    
    return( i_count );
}
#endif

/*****************************************************************************
 * MP4_FindBox:  find first box with i_type child of p_box
 *      return NULL if not found
 *****************************************************************************/

MP4_Box_t *MP4_FindBox( MP4_Box_t *p_box, u32 i_type )
{
    MP4_Box_t *p_child;
    
    if( !p_box )
    {
        return( NULL );
    }

    p_child = p_box->p_first;
    while( p_child )
    {
        if( p_child->i_type == i_type )
        {   
            return( p_child );
        }
        p_child = p_child->p_next;
    }
    
    return( NULL );
}


#if 0
/*****************************************************************************
 * MP4_FindNextBox:  find next box with thesame type and at the same level 
 *                  than p_box
 *****************************************************************************/
MP4_Box_t *MP4_FindNextBox( MP4_Box_t *p_box )
{
    MP4_Box_t *p_next;
    
    if( !p_box )
    {
        return( NULL );
    }

    p_next = p_box->p_next;
    while( p_next )
    {
        if( p_next->i_type == p_box->i_type )
        {
            return( p_next );
        }
        p_next = p_next->p_next;
    }
    return( NULL );
}
/*****************************************************************************
 * MP4_FindNbBox:  find the box i_number
 *****************************************************************************/
MP4_Box_t *MP4_FindNbBox( MP4_Box_t *p_box, u32 i_number )
{
    MP4_Box_t *p_child = p_box->p_first;
    
    if( !p_child )
    {
        return( NULL );
    }

    while( i_number )
    {
        if( !( p_child = p_child->p_next ) )
        {
            return( NULL );
        }
        i_number--;
    }
    return( p_child );
}
#endif
2163 2164 2165 2166 2167

/*****************************************************************************
 * MP4_FreeBox : free memory after read with MP4_ReadBox and all 
 * the children
 *****************************************************************************/
2168
void MP4_BoxFree( input_thread_t *p_input, MP4_Box_t *p_box )
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182
{
    int i_index;

    MP4_Box_t *p_child;
    MP4_Box_t *p_next;

    if( !p_box )
    {
        return; /* hehe */
    }
    p_child = p_box->p_first;
    while( p_child )
    {
        p_next = p_child->p_next;
2183
        MP4_BoxFree( p_input, p_child );
2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
        /* MP4_FreeBoxChildren have free all data expect p_child itself */
        free( p_child );
        p_child = p_next; 
    }

    /* Now search function to call */
    if( p_box->data.p_data )
    {
        for( i_index = 0; ; i_index++ )
        {
            if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
                ( MP4_Box_Function[i_index].i_type == 0 ) )
            {
                break;
            }
        }
        if( MP4_Box_Function[i_index].MP4_FreeBox_function == NULL )
        {
            /* Should not happen */
            msg_Warn( p_input, 
                      "cannot free box %c%c%c%c, type unknown",
                      (p_box->i_type)&0xff,
                      (p_box->i_type >> 8)&0xff, 
                      (p_box->i_type >> 16)&0xff, 
                      (p_box->i_type >> 24)&0xff );
        }
        else
        {
            MP4_Box_Function[i_index].MP4_FreeBox_function( p_input, p_box );
        }

        free( p_box->data.p_data );
        p_box->data.p_data = NULL;
    }

    p_box->p_first = NULL;
    p_box->p_last = NULL;
    
}

/*****************************************************************************
2225
 * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
2226 2227 2228 2229
 *****************************************************************************
 *  The first box is a virtual box "root" and is the father for all first 
 *  level boxes for the file, a sort of virtual contener
 *****************************************************************************/
2230
int MP4_BoxGetRoot( input_thread_t *p_input, MP4_Box_t *p_root )
2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
{
    
    MP4_Stream_t *p_stream;
    int i_result;
    
    MP4_SeekAbsolute( p_input, 0 );     /* Go to the begining */
    p_root->i_pos = 0;
    p_root->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
    p_root->i_shortsize = 1;
    p_root->i_size = p_input->stream.p_selected_area->i_size;
    CreateUUID( &p_root->i_uuid, p_root->i_type );

    p_root->data.p_data = NULL;
    p_root->p_father = NULL;
    p_root->p_first  = NULL;
    p_root->p_last  = NULL;
    p_root->p_next   = NULL;
    
    p_stream = MP4_InputStream( p_input );
    
    i_result = MP4_ReadBoxContainerRaw( p_stream, p_root );

    free( p_stream );
    
    if( i_result )
    {
        MP4_Box_t *p_child; 
        MP4_Box_t *p_moov;
        MP4_Box_t *p_cmov;

        /* check if there is a cmov, if so replace 
          compressed moov by  uncompressed one */
        if( ( p_moov = MP4_FindBox( p_root, FOURCC_moov ) )&&
            ( p_cmov = MP4_FindBox( p_moov, FOURCC_cmov ) ) )
        {
            /* rename the compressed moov as a box to skip */
            p_moov->i_type = FOURCC_skip;
                
            /* get uncompressed p_moov */
            p_moov = p_cmov->data.p_cmov->p_moov;
            p_cmov->data.p_cmov->p_moov = NULL;

            /* make p_root father of this new moov */
            p_moov->p_father = p_root;

            /* insert this new moov box as first child of p_root */
            p_moov->p_next = p_child = p_root->p_first;
            p_root->p_first = p_moov;
        }
    }
    return( i_result );
}


2285
static void __MP4_BoxDumpStructure( input_thread_t *p_input,
2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318
                                    MP4_Box_t *p_box, int i_level )
{
    MP4_Box_t *p_child;

    if( !i_level )
    {
        msg_Dbg( p_input, "Dumping root Box \"%c%c%c%c \"",
                          (p_box->i_type ) &0xff,
                          (p_box->i_type >>8 ) &0xff,
                          (p_box->i_type >>16 ) &0xff,
                          (p_box->i_type >>24) &0xff );
    }
    else
    {
        char str[512];
        int i;
        memset( str, (u8)' ', 512 );
        for( i = 0; i < i_level; i++ )
        {
            str[i*5] = '|';
        }
        sprintf( str + i_level * 5, "+ %c%c%c%c size %d",
                      (p_box->i_type ) &0xff,
                      (p_box->i_type>>8 ) &0xff,
                      (p_box->i_type>>16 ) &0xff,
                      (p_box->i_type>>24 ) &0xff,
                      (u32)p_box->i_size );
        
        msg_Dbg( p_input, "%s", str );
    }
    p_child = p_box->p_first;
    while( p_child )
    {
2319
        __MP4_BoxDumpStructure( p_input, p_child, i_level + 1 );
2320 2321 2322 2323 2324
        p_child = p_child->p_next;
    }
    
}

2325
void MP4_BoxDumpStructure( input_thread_t *p_input, MP4_Box_t *p_box )
2326
{
2327
    __MP4_BoxDumpStructure( p_input, p_box, 0 );
2328 2329 2330
}


2331

2332
/*****************************************************************************
2333 2334 2335 2336 2337
 *****************************************************************************
 **
 **  High level methods to acces an MP4 file
 **
 *****************************************************************************
2338
 *****************************************************************************/
2339
static void __get_token( char **ppsz_path, char **ppsz_token, int *pi_number )
2340
{
2341 2342
    int i_len ;
    if( !*ppsz_path[0] )
2343
    {
2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356
        *ppsz_token = NULL;
        *pi_number = 0;
        return;
    }
    i_len = 0;
    while(  (*ppsz_path)[i_len] && 
            (*ppsz_path)[i_len] != '/' && (*ppsz_path)[i_len] != '[' )
    {
        i_len++;
    }
    if( !i_len && **ppsz_path == '/' )
    {
        i_len = 1;
2357
    }
2358 2359 2360
    *ppsz_token = malloc( i_len + 1 );
        
    memcpy( *ppsz_token, *ppsz_path, i_len );
2361

2362 2363 2364 2365 2366
    (*ppsz_token)[i_len] = '\0';

    *ppsz_path += i_len;
        
    if( **ppsz_path == '[' )
2367
    {
2368 2369 2370 2371 2372 2373 2374 2375 2376
        (*ppsz_path)++;
        *pi_number = strtol( *ppsz_path, NULL, 10 );
        while( **ppsz_path && **ppsz_path != ']' )
        {
            (*ppsz_path)++;
        }
        if( **ppsz_path == ']' )
        {
            (*ppsz_path)++;
2377 2378
        }
    }
2379 2380 2381 2382 2383 2384 2385 2386
    else
    {
        *pi_number = 0;
    }
    while( **ppsz_path == '/' )
    {
        (*ppsz_path)++;
    }
2387 2388
}

2389 2390
static void __MP4_BoxGet( MP4_Box_t **pp_result,
                          MP4_Box_t *p_box, char *psz_fmt, va_list args)
2391
{
2392
    char    *psz_path;
2393
#if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN)
2394 2395 2396
    int     i_size;
#endif

2397 2398 2399
    
    if( !p_box )
    {
2400 2401
        *pp_result = NULL;
        return;
2402 2403
    }

2404
#if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN)
2405 2406 2407 2408 2409 2410 2411 2412 2413
    vasprintf( &psz_path, psz_fmt, args );
#else
    i_size = strlen( psz_fmt ) + 1024;
    psz_path = calloc( i_size, sizeof( char ) );
    vsnprintf( psz_path, i_size, psz_fmt, args );
    psz_path[i_size - 1] = 0;
#endif

    if( !psz_path || !psz_path[0] )
2414
    {
2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435
        FREE( psz_path );
        *pp_result = NULL;
        return;
    }

//    fprintf( stderr, "path:'%s'\n", psz_path );
    psz_fmt = psz_path; /* keep this pointer, as it need to be unallocated */
    for( ; ; )
    {
        char *psz_token;
        int i_number;

        __get_token( &psz_path, &psz_token, &i_number );
//        fprintf( stderr, "path:'%s', token:'%s' n:%d\n", 
//                 psz_path,psz_token,i_number );
        if( !psz_token )
        {
            FREE( psz_token );
            free( psz_fmt );
            *pp_result = p_box;
            return;
2436
        }
2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527
        else
        if( !strcmp( psz_token, "/" ) )
        {
            /* Find root box */
            while( p_box && p_box->i_type != VLC_FOURCC( 'r', 'o', 'o', 't' ) )
            {
                p_box = p_box->p_father;
            }
            if( !p_box )
            {
                free( psz_token );
                free( psz_fmt );
                *pp_result = NULL;
                return;
            }
        }
        else
        if( !strcmp( psz_token, "." ) )
        {
            /* Do nothing */
        }
        else
        if( !strcmp( psz_token, ".." ) )
        {
            p_box = p_box->p_father;
            if( !p_box )
            {
                free( psz_token );
                free( psz_fmt );
                *pp_result = NULL;
                return;
            }
        }
        else
        if( strlen( psz_token ) == 4 )
        {
            u32 i_fourcc;
            i_fourcc = VLC_FOURCC( psz_token[0], psz_token[1],
                                   psz_token[2], psz_token[3] );
            p_box = p_box->p_first;
            for( ; ; )
            {
                if( !p_box )
                {
                    free( psz_token );
                    free( psz_fmt );
                    *pp_result = NULL;
                    return;
                }
                if( p_box->i_type == i_fourcc )
                {
                    if( !i_number )
                    {
                        break;
                    }
                    i_number--;
                }
                p_box = p_box->p_next;
            }
        }
        else
        if( strlen( psz_token ) == 0 )
        {
            p_box = p_box->p_first;
            for( ; ; )
            {
                if( !p_box )
                {
                    free( psz_token );
                    free( psz_fmt );
                    *pp_result = NULL;
                    return;
                }
                if( !i_number )
                {
                    break;
                }
                i_number--;
                p_box = p_box->p_next;
            }
        }
        else
        {
//            fprintf( stderr, "Argg malformed token \"%s\"",psz_token );
            FREE( psz_token );
            free( psz_fmt );
            *pp_result = NULL;
            return;
        }
        
        free( psz_token );
2528 2529 2530 2531 2532
    }
    
}

/*****************************************************************************
2533 2534 2535 2536 2537 2538 2539
 * MP4_BoxGet: find a box given a path relative to p_box
 *****************************************************************************
 * Path Format: . .. / as usual
 *              [number] to specifie box number ex: trak[12]
 *              
 * ex: /moov/trak[12]
 *     ../mdia
2540
 *****************************************************************************/
2541
MP4_Box_t *MP4_BoxGet( MP4_Box_t *p_box, char *psz_fmt, ... )
2542
{
2543 2544
    va_list args;
    MP4_Box_t *p_result;
2545

2546 2547 2548 2549 2550
    va_start( args, psz_fmt ); 
    __MP4_BoxGet( &p_result, p_box, psz_fmt, args );
    va_end( args );

    return( p_result );
2551 2552 2553
}

/*****************************************************************************
2554 2555 2556 2557 2558 2559 2560
 * MP4_BoxCount: count box given a path relative to p_box
 *****************************************************************************
 * Path Format: . .. / as usual
 *              [number] to specifie box number ex: trak[12]
 *              
 * ex: /moov/trak[12]
 *     ../mdia
2561
 *****************************************************************************/
2562
int MP4_BoxCount( MP4_Box_t *p_box, char *psz_fmt, ... )
2563
{
2564 2565 2566 2567 2568 2569 2570 2571
    va_list args;
    int     i_count;
    MP4_Box_t *p_result, *p_next;

    va_start( args, psz_fmt ); 
    __MP4_BoxGet( &p_result, p_box, psz_fmt, args );
    va_end( args );
    if( !p_result )
2572
    {
2573
        return( 0 );
2574
    }
2575 2576 2577
    
    i_count = 1;
    for( p_next = p_result->p_next; p_next != NULL; p_next = p_next->p_next)
2578
    {
2579
        if( p_next->i_type == p_result->i_type)
2580
        {
2581
            i_count++;
2582 2583
        }
    }
2584
    return( i_count );
2585 2586
}