1 /*****************************************************************************
2 * libmp4.h : LibMP4 library for mp4 module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: libmp4.h,v 1.1 2002/07/17 21:37:27 fenrir Exp $
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
25 /* XXX It's not the same than VLC_FOURCC */
26 #define MP4_FOURCC( a, b, c, d ) \
27 ( ((u32)d) | ( ((u32)c) << 8 ) | ( ((u32)b) << 16 ) | ( ((u32)a) << 24 ) )
30 #define FOURCC_uuid MP4_FOURCC( 'u', 'u', 'i', 'd' )
32 #define FOURCC_ftyp MP4_FOURCC( 'f', 't', 'y', 'p' )
33 #define FOURCC_moov MP4_FOURCC( 'm', 'o', 'o', 'v' )
34 #define FOURCC_cmov MP4_FOURCC( 'c', 'm', 'o', 'v' )
35 #define FOURCC_dcom MP4_FOURCC( 'd', 'c', 'o', 'm' )
36 #define FOURCC_cmvd MP4_FOURCC( 'c', 'm', 'v', 'd' )
38 #define FOURCC_moof MP4_FOURCC( 'm', 'o', 'o', 'f' )
39 #define FOURCC_mdat MP4_FOURCC( 'm', 'd', 'a', 't' )
40 #define FOURCC_skip MP4_FOURCC( 's', 'k', 'i', 'p' )
41 #define FOURCC_free MP4_FOURCC( 'f', 'r', 'e', 'e' )
42 #define FOURCC_udta MP4_FOURCC( 'u', 'd', 't', 'a' )
43 #define FOURCC_wide MP4_FOURCC( 'w', 'i', 'd', 'e' )
45 #define FOURCC_trak MP4_FOURCC( 't', 'r', 'a', 'k' )
46 #define FOURCC_mvhd MP4_FOURCC( 'm', 'v', 'h', 'd' )
47 #define FOURCC_tkhd MP4_FOURCC( 't', 'k', 'h', 'd' )
48 #define FOURCC_tref MP4_FOURCC( 't', 'r', 'e', 'f' )
49 #define FOURCC_mdia MP4_FOURCC( 'm', 'd', 'i', 'a' )
50 #define FOURCC_mdhd MP4_FOURCC( 'm', 'd', 'h', 'd' )
51 #define FOURCC_hdlr MP4_FOURCC( 'h', 'd', 'l', 'r' )
52 #define FOURCC_minf MP4_FOURCC( 'm', 'i', 'n', 'f' )
53 #define FOURCC_vmhd MP4_FOURCC( 'v', 'm', 'h', 'd' )
54 #define FOURCC_smhd MP4_FOURCC( 's', 'm', 'h', 'd' )
55 #define FOURCC_hmhd MP4_FOURCC( 'h', 'm', 'h', 'd' )
56 #define FOURCC_dinf MP4_FOURCC( 'd', 'i', 'n', 'f' )
57 #define FOURCC_url MP4_FOURCC( 'u', 'r', 'l', ' ' )
58 #define FOURCC_urn MP4_FOURCC( 'u', 'r', 'n', ' ' )
59 #define FOURCC_dref MP4_FOURCC( 'd', 'r', 'e', 'f' )
60 #define FOURCC_stbl MP4_FOURCC( 's', 't', 'b', 'l' )
61 #define FOURCC_stts MP4_FOURCC( 's', 't', 't', 's' )
62 #define FOURCC_ctts MP4_FOURCC( 'c', 't', 't', 's' )
63 #define FOURCC_stsd MP4_FOURCC( 's', 't', 's', 'd' )
64 #define FOURCC_stsz MP4_FOURCC( 's', 't', 's', 'z' )
65 #define FOURCC_stz2 MP4_FOURCC( 's', 't', 'z', '2' )
66 #define FOURCC_stsc MP4_FOURCC( 's', 't', 's', 'c' )
67 #define FOURCC_stco MP4_FOURCC( 's', 't', 'c', 'o' )
68 #define FOURCC_co64 MP4_FOURCC( 'c', 'o', '6', '4' )
69 #define FOURCC_stss MP4_FOURCC( 's', 't', 's', 's' )
70 #define FOURCC_stsh MP4_FOURCC( 's', 't', 's', 'h' )
71 #define FOURCC_stdp MP4_FOURCC( 's', 't', 'd', 'p' )
72 #define FOURCC_padb MP4_FOURCC( 'p', 'a', 'd', 'b' )
73 #define FOURCC_edts MP4_FOURCC( 'e', 'd', 't', 's' )
74 #define FOURCC_elst MP4_FOURCC( 'e', 'l', 's', 't' )
75 #define FOURCC_udta MP4_FOURCC( 'u', 'd', 't', 'a' )
76 #define FOURCC_mvex MP4_FOURCC( 'm', 'v', 'e', 'x' )
77 #define FOURCC_trex MP4_FOURCC( 't', 'r', 'e', 'x' )
78 #define FOURCC_mfhd MP4_FOURCC( 'm', 'f', 'h', 'd' )
79 #define FOURCC_traf MP4_FOURCC( 't', 'r', 'a', 'f' )
80 #define FOURCC_tfhd MP4_FOURCC( 't', 'f', 'h', 'd' )
81 #define FOURCC_trun MP4_FOURCC( 't', 'r', 'u', 'n' )
82 #define FOURCC_cprt MP4_FOURCC( 'c', 'p', 'r', 't' )
83 #define FOURCC_iods MP4_FOURCC( 'i', 'o', 'd', 's' )
85 #define FOURCC_nmhd MP4_FOURCC( 'n', 'm', 'h', 'd' )
86 #define FOURCC_mp4v MP4_FOURCC( 'm', 'p', '4', 'v' )
87 #define FOURCC_mp4a MP4_FOURCC( 'm', 'p', '4', 'a' )
88 #define FOURCC_mp4s MP4_FOURCC( 'm', 'p', '4', 's' )
89 #define FOURCC_vide MP4_FOURCC( 'v', 'i', 'd', 'e' )
90 #define FOURCC_soun MP4_FOURCC( 's', 'o', 'u', 'n' )
91 #define FOURCC_hint MP4_FOURCC( 'h', 'i', 'n', 't' )
93 #define FOURCC_dpnd MP4_FOURCC( 'd', 'p', 'n', 'd' )
94 #define FOURCC_ipir MP4_FOURCC( 'i', 'p', 'i', 'r' )
95 #define FOURCC_mpod MP4_FOURCC( 'm', 'p', 'o', 'd' )
96 #define FOURCC_hnti MP4_FOURCC( 'h', 'n', 't', 'i' )
97 #define FOURCC_rtp MP4_FOURCC( 'r', 't', 'p', ' ' )
99 #define FOURCC_isom MP4_FOURCC( 'i', 's', 'o', 'm' )
100 #define FOURCC_esds MP4_FOURCC( 'e', 's', 'd', 's' )
102 #define FOURCC_zlib MP4_FOURCC( 'z', 'l', 'i', 'b' )
103 #define FOURCC_SVQ1 MP4_FOURCC( 'S', 'V', 'Q', '1' )
104 #define FOURCC__mp3 MP4_FOURCC( '.', 'm', 'p', '3' )
105 #define FOURCC_3IV1 MP4_FOURCC( '3', 'I', 'V', '1' )
106 #define FOURCC_h263 MP4_FOURCC( 'h', '2', '6', '3' )
107 #define FOURCC_DIVX MP4_FOURCC( 'D', 'I', 'V', 'X' )
111 #define FOURCC_ MP4_FOURCC( '', '', '', '' )
113 /* Do you want some debug information on all read boxes ? */
114 #define MP4_VERBOSE 1
116 /* memory stream and file stream object */
118 typedef struct MP4_Stream_s
120 int b_memory; /* do we uses a memory buffer */
122 input_thread_t *p_input;
124 off_t i_start; /* in the buffer position for memory stream */
133 /* uuid Universal Unique IDentifiers */
134 typedef struct UUID_s
139 /* specific structure for all boxes */
141 typedef struct MP4_Box_data_ftyp_s
146 u32 i_compatible_brands_count;
147 u32 *i_compatible_brands;
149 } MP4_Box_data_ftyp_t;
151 typedef struct MP4_Box_data_mvhd_s
157 u64 i_modification_time;
169 } MP4_Box_data_mvhd_t;
171 #define MP4_TRACK_ENABLED 0x000001
172 #define MP4_TRACK_IN_MOVIE 0x000002
173 #define MP4_TRACK_IN_PREVIEW 0x000004
174 typedef struct MP4_Box_data_tkhd_s
180 u64 i_modification_time;
195 } MP4_Box_data_tkhd_t;
197 typedef struct MP4_Box_data_mdhd_s
203 u64 i_modification_time;
207 /* one bit for pad */
208 /* unsigned int(5)[3] language difference with 0x60*/
209 unsigned char i_language[3];
212 } MP4_Box_data_mdhd_t;
214 typedef struct MP4_Box_data_hdlr_s
220 u32 i_handler_type; /* "vide" "soun" "hint" "odsm"
221 "crsm" "sdsm" "m7sm" "ocsm"
224 unsigned char *psz_name; /* in UTF-8 */
226 } MP4_Box_data_hdlr_t;
228 typedef struct MP4_Box_data_vmhd_s
236 } MP4_Box_data_vmhd_t;
238 typedef struct MP4_Box_data_smhd_s
246 } MP4_Box_data_smhd_t;
248 typedef struct MP4_Box_data_hmhd_s
259 } MP4_Box_data_hmhd_t;
261 typedef struct MP4_Box_data_url_s
266 unsigned char *psz_location;
268 } MP4_Box_data_url_t;
270 typedef struct MP4_Box_data_urn_s
275 unsigned char *psz_name;
276 unsigned char *psz_location;
278 } MP4_Box_data_urn_t;
280 typedef struct MP4_Box_data_dref_s
286 /* XXX it's also a container with i_entry_count entry */
287 } MP4_Box_data_dref_t;
289 typedef struct MP4_Box_data_stts_s
295 u32 *i_sample_count; /* these are array */
298 } MP4_Box_data_stts_t;
300 typedef struct MP4_Box_data_ctts_s
307 u32 *i_sample_count; /* these are array */
308 s32 *i_sample_offset;
310 } MP4_Box_data_ctts_t;
313 typedef struct MP4_Box_data_sample_soun_s
316 u16 i_data_reference_index;
323 u16 i_sampleratehi; /* timescale of track */
326 } MP4_Box_data_sample_soun_t;
328 typedef struct MP4_Box_data_sample_mp4a_s
331 u16 i_data_reference_index;
338 u16 i_sampleratehi; /* timescale of track */
341 } MP4_Box_data_sample_mp4a_t;
344 typedef struct MP4_Box_data_sample_vide_s
347 u16 i_data_reference_index;
351 u32 i_predefined2[3];
356 u32 i_horizresolution;
357 u32 i_vertresolution;
362 u8 i_compressorname[32];
367 } MP4_Box_data_sample_vide_t;
369 typedef struct MP4_Box_data_sample_mp4v_s
372 u16 i_data_reference_index;
376 u32 i_predefined2[3];
381 u32 i_horizresolution;
382 u32 i_vertresolution;
387 u8 i_compressorname[32];
392 } MP4_Box_data_sample_mp4v_t;
396 typedef struct MP4_Box_data_sample_hint_s
399 u16 i_data_reference_index;
403 } MP4_Box_data_sample_hint_t;
405 typedef struct MP4_Box_data_moviehintinformation_rtp_s
407 u32 i_description_format;
408 unsigned char *psz_text;
410 } MP4_Box_data_moviehintinformation_rtp_t;
414 typedef struct MP4_Box_data_stsd_s
421 /* it contains SampleEntry handled as if it was Box */
423 } MP4_Box_data_stsd_t;
426 typedef struct MP4_Box_data_stsz_s
434 u32 *i_entry_size; /* array , empty if i_sample_size != 0 */
436 } MP4_Box_data_stsz_t;
438 typedef struct MP4_Box_data_stz2_s
443 u32 i_sample_size; /* 24 bits */
447 u32 *i_entry_size; /* array: unsigned int(i_field_size) entry_size */
449 } MP4_Box_data_stz2_t;
451 typedef struct MP4_Box_data_stsc_s
458 u32 *i_first_chunk; /* theses are arrays */
459 u32 *i_samples_per_chunk;
460 u32 *i_sample_description_index;
462 } MP4_Box_data_stsc_t;
465 typedef struct MP4_Box_data_co64_s
474 } MP4_Box_data_co64_t;
477 typedef struct MP4_Box_data_stss_s
484 u32 *i_sample_number;
486 } MP4_Box_data_stss_t;
488 typedef struct MP4_Box_data_stsh_s
495 u32 *i_shadowed_sample_number;
496 u32 *i_sync_sample_number;
498 } MP4_Box_data_stsh_t;
500 typedef struct MP4_Box_data_stdp_s
507 } MP4_Box_data_stdp_t;
509 typedef struct MP4_Box_data_padb_s
516 u16 *i_reserved1; /* 1bit */
517 u16 *i_pad2; /* 3bits */
518 u16 *i_reserved2; /* 1bit */
519 u16 *i_pad1; /* 3bits */
522 } MP4_Box_data_padb_t;
525 typedef struct MP4_Box_data_elst_s
532 u64 *i_segment_duration;
534 u16 *i_media_rate_integer;
535 u16 *i_media_rate_fraction;
538 } MP4_Box_data_elst_t;
540 typedef struct MP4_Box_data_cprt_s
545 unsigned char i_language[3];
547 unsigned char *psz_notice;
548 } MP4_Box_data_cprt_t;
551 /* DecoderConfigDescriptor */
552 typedef struct MP4_descriptor_decoder_config_s
554 u8 i_objectTypeIndication;
561 int i_decoder_specific_info_len;
562 u8 *p_decoder_specific_info;
563 /* some other stuff */
565 } MP4_descriptor_decoder_config_t;
567 typedef struct MP4_descriptor_SL_config_s
570 } MP4_descriptor_SL_config_t;
573 typedef struct MP4_descriptor_ES_s
576 int b_stream_dependence;
579 int i_stream_priority;
581 int i_depend_on_ES_ID; /* if b_stream_dependence set */
583 unsigned char *psz_URL;
585 u16 i_OCR_ES_ID; /* if b_OCRstream */
586 MP4_descriptor_decoder_config_t *p_decConfigDescr;
588 MP4_descriptor_SL_config_t *p_slConfigDescr;
590 /* some other stuff ... */
592 } MP4_descriptor_ES_t;
595 typedef struct MP4_Box_data_esds_s
600 MP4_descriptor_ES_t es_descriptor;
602 } MP4_Box_data_esds_t;
605 typedef struct MP4_Box_data_dcom_s
607 u32 i_algorithm; /* fourcc */
609 } MP4_Box_data_dcom_t;
611 typedef struct MP4_Box_data_cmvd_s
613 u32 i_uncompressed_size;
614 u32 i_compressed_size;
616 int b_compressed; /* Set to 1 if compressed data, 0 if uncompressed */
619 } MP4_Box_data_cmvd_t;
621 typedef struct MP4_Box_data_cmov_s
623 struct MP4_Box_s *p_moov; /* uncompressed moov */
625 } MP4_Box_data_cmov_t;
628 typedef struct MP4_Box_data_cmov_s
637 typedef union MP4_Box_data_s
639 MP4_Box_data_ftyp_t *p_ftyp;
640 MP4_Box_data_mvhd_t *p_mvhd;
641 MP4_Box_data_tkhd_t *p_tkhd;
642 MP4_Box_data_mdhd_t *p_mdhd;
643 MP4_Box_data_hdlr_t *p_hdlr;
644 MP4_Box_data_vmhd_t *p_vmhd;
645 MP4_Box_data_smhd_t *p_smhd;
646 MP4_Box_data_hmhd_t *p_hmhd;
647 MP4_Box_data_url_t *p_url;
648 MP4_Box_data_urn_t *p_urn;
649 MP4_Box_data_dref_t *p_dref;
650 MP4_Box_data_stts_t *p_stts;
651 MP4_Box_data_ctts_t *p_ctts;
652 MP4_Box_data_stsd_t *p_stsd;
653 MP4_Box_data_sample_vide_t *p_sample_vide;
654 MP4_Box_data_sample_vide_t *p_sample_mp4v;
655 MP4_Box_data_sample_soun_t *p_sample_soun;
656 MP4_Box_data_sample_soun_t *p_sample_mp4a;
657 MP4_Box_data_sample_hint_t *p_sample_hint;
659 MP4_Box_data_esds_t *p_esds;
661 MP4_Box_data_stsz_t *p_stsz;
662 MP4_Box_data_stz2_t *p_stz2;
663 MP4_Box_data_stsc_t *p_stsc;
664 MP4_Box_data_co64_t *p_co64;
665 MP4_Box_data_stss_t *p_stss;
666 MP4_Box_data_stsh_t *p_stsh;
667 MP4_Box_data_stdp_t *p_stdp;
668 MP4_Box_data_padb_t *p_padb;
669 MP4_Box_data_elst_t *p_elst;
670 MP4_Box_data_cprt_t *p_cprt;
672 MP4_Box_data_dcom_t *p_dcom;
673 MP4_Box_data_cmvd_t *p_cmvd;
674 MP4_Box_data_cmov_t *p_cmov;
676 MP4_Box_data_moviehintinformation_rtp_t p_moviehintinformation_rtp;
678 void *p_data; /* for unknow type */
683 /* the most basic structure */
684 typedef struct MP4_Box_s
686 off_t i_pos; /* absolute position */
691 UUID_t i_uuid; /* Set if i_type == "uuid" */
693 u64 i_size; /* always set so use it */
695 MP4_Box_data_t data; /* union of pointers on extended data depending on i_type (or i_usertype) */
697 struct MP4_Box_s *p_father; /* pointer on the father Box */
699 struct MP4_Box_s *p_first; /* pointer on the first child Box */
700 struct MP4_Box_s *p_last;
702 struct MP4_Box_s *p_next; /* pointer on the next boxes at the same level */
706 /*---------------------------------------------------------------------------*/
707 /****----------------------- High level functions ------------------------****/
708 /*---------------------------------------------------------------------------*/
710 /*****************************************************************************
711 * Function for manipulate stream easily
712 *****************************************************************************/
714 off_t MP4_TellAbsolute( input_thread_t *p_input );
715 int MP4_SeekAbsolute( input_thread_t *p_input, off_t i_pos);
716 int MP4_ReadData( input_thread_t *p_input, u8 *p_buff, int i_size );
718 /*****************************************************************************
719 * MP4_ReadRoot : Parse the entire file, and create all boxes in memory
720 *****************************************************************************
721 * The first box is a virtual box "root" and is the father for all first
724 * RETURN : 1 if succes and 0 if it failed
725 *****************************************************************************/
726 int MP4_ReadBoxRoot( input_thread_t *p_input, MP4_Box_t *p_root );
728 /*****************************************************************************
729 * MP4_FreeBox : free memory allocated after read with MP4_ReadBox
730 * this, means also children boxes
731 * XXX : all children have to be allocated by a malloc !! and
732 * p_box isn't freeing
733 *****************************************************************************/
734 void MP4_FreeBox( input_thread_t *p_input, MP4_Box_t *p_box );
736 /*****************************************************************************
737 * MP4_DumpBoxStructure: print the structure of the p_box
738 *****************************************************************************/
739 void MP4_DumpBoxStructure( input_thread_t *p_input, MP4_Box_t *p_box );
741 /*****************************************************************************
742 * MP4_CountBox: given a box, count how many child have the requested type
743 * FIXME : support GUUID
744 *****************************************************************************/
745 int MP4_CountBox( MP4_Box_t *p_box, u32 i_type );
747 /*****************************************************************************
748 * MP4_FindBox: find first box with i_type child of p_box
749 * return NULL if not found
750 *****************************************************************************/
751 MP4_Box_t *MP4_FindBox( MP4_Box_t *p_box, u32 i_type );
753 /*****************************************************************************
754 * MP4_FindNextBox: find next box with thesame type and at the same level
756 *****************************************************************************/
757 MP4_Box_t *MP4_FindNextBox( MP4_Box_t *p_box );
759 /*****************************************************************************
760 * MP4_FindNbBox: find the box i_number
761 *****************************************************************************/
762 MP4_Box_t *MP4_FindNbBox( MP4_Box_t *p_box, u32 i_number );
765 /*---------------------------------------------------------------------------*/
766 /****----------------------- Lower level functions -----------------------****/
768 /****------Use them only when you known what they really do and need------****/
770 /****---------------------------------------------------------------------****/
771 /*---------------------------------------------------------------------------*/
774 /**** ------- First some function to make abstract from input -------- */
776 /****************************************************************************
777 * MP4_InputStream create an stram with an input
779 ****************************************************************************/
780 MP4_Stream_t *MP4_InputStream( input_thread_t *p_input );
782 /****************************************************************************
783 * MP4_MemoryStream create a memory stream
784 * if p_buffer == NULL, will allocate a buffer of i_size, else
785 * it uses p_buffer XXX you have to unallocate yourself !
787 ****************************************************************************/
788 MP4_Stream_t *MP4_MemoryStream( input_thread_t *p_input,
789 int i_size, u8 *p_buffer );
791 /****************************************************************************
792 * MP4_ReadStream read from a MP4_Stream_t
794 ****************************************************************************/
795 int MP4_ReadStream( MP4_Stream_t *p_stream, u8 *p_buff, int i_size );
797 /****************************************************************************
798 * MP4_PeekStream guess it ;)
800 ****************************************************************************/
801 int MP4_PeekStream( MP4_Stream_t *p_stream, u8 **pp_peek, int i_size );
803 /****************************************************************************
804 * MP4_TellStream give absolute position in the stream
805 * XXX for a memory stream give position from begining of the buffer
806 ****************************************************************************/
807 off_t MP4_TellStream( MP4_Stream_t *p_stream );
809 /****************************************************************************
810 * MP4_SeekStream seek in a MP4_Stream_t
812 ****************************************************************************/
813 int MP4_SeekStream( MP4_Stream_t *p_stream, off_t i_pos);
817 /*****************************************************************************
818 * MP4_ReadBox : parse the actual box and the children if they exist
820 * RETURN : 1 if succes and 0 if it failed
821 *****************************************************************************/
822 int MP4_ReadBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father );
825 /*****************************************************************************
826 * MP4_ReadBoxCommon : Load only common parameters for all boxes
827 *****************************************************************************
828 * p_box need to be an already allocated MP4_Box_t, and all data
829 * will only be peek not read
831 * RETURN : 0 if it fail, 1 otherwise
832 *****************************************************************************/
833 int MP4_ReadBoxCommon( MP4_Stream_t *p_stream, MP4_Box_t *p_box );
836 /*****************************************************************************
837 * MP4_MP4_GotoBox : Go to this particular box
838 *****************************************************************************
839 * RETURN : 0 if it fail, 1 otherwise
840 *****************************************************************************/
841 int MP4_GotoBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box );
844 /*****************************************************************************
845 * MP4_MP4_NextBox : Go to the next box wiithin p_box->p_father
846 *****************************************************************************
847 * if p_box == NULL, go to the next box in witch we are( at the begining ).
848 *****************************************************************************/
849 int MP4_NextBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box );