1 /*****************************************************************************
2 * libmp4.h : LibMP4 library for mp4 module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: libmp4.h,v 1.5 2002/07/23 17:19:02 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 *****************************************************************************/
23 #define FOURCC_uuid VLC_FOURCC( 'u', 'u', 'i', 'd' )
25 #define FOURCC_ftyp VLC_FOURCC( 'f', 't', 'y', 'p' )
26 #define FOURCC_moov VLC_FOURCC( 'm', 'o', 'o', 'v' )
27 #define FOURCC_cmov VLC_FOURCC( 'c', 'm', 'o', 'v' )
28 #define FOURCC_dcom VLC_FOURCC( 'd', 'c', 'o', 'm' )
29 #define FOURCC_cmvd VLC_FOURCC( 'c', 'm', 'v', 'd' )
31 #define FOURCC_moof VLC_FOURCC( 'm', 'o', 'o', 'f' )
32 #define FOURCC_mdat VLC_FOURCC( 'm', 'd', 'a', 't' )
33 #define FOURCC_skip VLC_FOURCC( 's', 'k', 'i', 'p' )
34 #define FOURCC_free VLC_FOURCC( 'f', 'r', 'e', 'e' )
35 #define FOURCC_udta VLC_FOURCC( 'u', 'd', 't', 'a' )
36 #define FOURCC_wide VLC_FOURCC( 'w', 'i', 'd', 'e' )
38 #define FOURCC_trak VLC_FOURCC( 't', 'r', 'a', 'k' )
39 #define FOURCC_mvhd VLC_FOURCC( 'm', 'v', 'h', 'd' )
40 #define FOURCC_tkhd VLC_FOURCC( 't', 'k', 'h', 'd' )
41 #define FOURCC_tref VLC_FOURCC( 't', 'r', 'e', 'f' )
42 #define FOURCC_mdia VLC_FOURCC( 'm', 'd', 'i', 'a' )
43 #define FOURCC_mdhd VLC_FOURCC( 'm', 'd', 'h', 'd' )
44 #define FOURCC_hdlr VLC_FOURCC( 'h', 'd', 'l', 'r' )
45 #define FOURCC_minf VLC_FOURCC( 'm', 'i', 'n', 'f' )
46 #define FOURCC_vmhd VLC_FOURCC( 'v', 'm', 'h', 'd' )
47 #define FOURCC_smhd VLC_FOURCC( 's', 'm', 'h', 'd' )
48 #define FOURCC_hmhd VLC_FOURCC( 'h', 'm', 'h', 'd' )
49 #define FOURCC_dinf VLC_FOURCC( 'd', 'i', 'n', 'f' )
50 #define FOURCC_url VLC_FOURCC( 'u', 'r', 'l', ' ' )
51 #define FOURCC_urn VLC_FOURCC( 'u', 'r', 'n', ' ' )
52 #define FOURCC_dref VLC_FOURCC( 'd', 'r', 'e', 'f' )
53 #define FOURCC_stbl VLC_FOURCC( 's', 't', 'b', 'l' )
54 #define FOURCC_stts VLC_FOURCC( 's', 't', 't', 's' )
55 #define FOURCC_ctts VLC_FOURCC( 'c', 't', 't', 's' )
56 #define FOURCC_stsd VLC_FOURCC( 's', 't', 's', 'd' )
57 #define FOURCC_stsz VLC_FOURCC( 's', 't', 's', 'z' )
58 #define FOURCC_stz2 VLC_FOURCC( 's', 't', 'z', '2' )
59 #define FOURCC_stsc VLC_FOURCC( 's', 't', 's', 'c' )
60 #define FOURCC_stco VLC_FOURCC( 's', 't', 'c', 'o' )
61 #define FOURCC_co64 VLC_FOURCC( 'c', 'o', '6', '4' )
62 #define FOURCC_stss VLC_FOURCC( 's', 't', 's', 's' )
63 #define FOURCC_stsh VLC_FOURCC( 's', 't', 's', 'h' )
64 #define FOURCC_stdp VLC_FOURCC( 's', 't', 'd', 'p' )
65 #define FOURCC_padb VLC_FOURCC( 'p', 'a', 'd', 'b' )
66 #define FOURCC_edts VLC_FOURCC( 'e', 'd', 't', 's' )
67 #define FOURCC_elst VLC_FOURCC( 'e', 'l', 's', 't' )
68 #define FOURCC_udta VLC_FOURCC( 'u', 'd', 't', 'a' )
69 #define FOURCC_mvex VLC_FOURCC( 'm', 'v', 'e', 'x' )
70 #define FOURCC_trex VLC_FOURCC( 't', 'r', 'e', 'x' )
71 #define FOURCC_mfhd VLC_FOURCC( 'm', 'f', 'h', 'd' )
72 #define FOURCC_traf VLC_FOURCC( 't', 'r', 'a', 'f' )
73 #define FOURCC_tfhd VLC_FOURCC( 't', 'f', 'h', 'd' )
74 #define FOURCC_trun VLC_FOURCC( 't', 'r', 'u', 'n' )
75 #define FOURCC_cprt VLC_FOURCC( 'c', 'p', 'r', 't' )
76 #define FOURCC_iods VLC_FOURCC( 'i', 'o', 'd', 's' )
78 #define FOURCC_nmhd VLC_FOURCC( 'n', 'm', 'h', 'd' )
79 #define FOURCC_mp4v VLC_FOURCC( 'm', 'p', '4', 'v' )
80 #define FOURCC_mp4a VLC_FOURCC( 'm', 'p', '4', 'a' )
81 #define FOURCC_mp4s VLC_FOURCC( 'm', 'p', '4', 's' )
82 #define FOURCC_vide VLC_FOURCC( 'v', 'i', 'd', 'e' )
83 #define FOURCC_soun VLC_FOURCC( 's', 'o', 'u', 'n' )
84 #define FOURCC_hint VLC_FOURCC( 'h', 'i', 'n', 't' )
86 #define FOURCC_dpnd VLC_FOURCC( 'd', 'p', 'n', 'd' )
87 #define FOURCC_ipir VLC_FOURCC( 'i', 'p', 'i', 'r' )
88 #define FOURCC_mpod VLC_FOURCC( 'm', 'p', 'o', 'd' )
89 #define FOURCC_hnti VLC_FOURCC( 'h', 'n', 't', 'i' )
90 #define FOURCC_rtp VLC_FOURCC( 'r', 't', 'p', ' ' )
92 #define FOURCC_isom VLC_FOURCC( 'i', 's', 'o', 'm' )
93 #define FOURCC_esds VLC_FOURCC( 'e', 's', 'd', 's' )
95 #define FOURCC_zlib VLC_FOURCC( 'z', 'l', 'i', 'b' )
96 #define FOURCC_SVQ1 VLC_FOURCC( 'S', 'V', 'Q', '1' )
97 #define FOURCC__mp3 VLC_FOURCC( '.', 'm', 'p', '3' )
98 #define FOURCC_3IV1 VLC_FOURCC( '3', 'I', 'V', '1' )
99 #define FOURCC_h263 VLC_FOURCC( 'h', '2', '6', '3' )
100 #define FOURCC_DIVX VLC_FOURCC( 'D', 'I', 'V', 'X' )
101 #define FOURCC_cvid VLC_FOURCC( 'c', 'v', 'i', 'd' )
102 #define FOURCC_mjpa VLC_FOURCC( 'm', 'j', 'p', 'a' )
103 #define FOURCC_mjpb VLC_FOURCC( 'm', 'j', 'q', 't' )
104 #define FOURCC_mjqt VLC_FOURCC( 'm', 'j', 'h', 't' )
105 #define FOURCC_mjht VLC_FOURCC( 'm', 'j', 'p', 'b' )
107 #define FOURCC_jpeg VLC_FOURCC( 'j', 'p', 'e', 'g' )
109 /* Do you want some debug information on all read boxes ? */
110 #define MP4_VERBOSE 1
112 /* memory stream and file stream object */
114 typedef struct MP4_Stream_s
116 int b_memory; /* do we uses a memory buffer */
118 input_thread_t *p_input;
120 off_t i_start; /* in the buffer position for memory stream */
129 /* uuid Universal Unique IDentifiers */
130 typedef struct UUID_s
135 /* specific structure for all boxes */
137 typedef struct MP4_Box_data_ftyp_s
142 u32 i_compatible_brands_count;
143 u32 *i_compatible_brands;
145 } MP4_Box_data_ftyp_t;
147 typedef struct MP4_Box_data_mvhd_s
153 u64 i_modification_time;
165 } MP4_Box_data_mvhd_t;
167 #define MP4_TRACK_ENABLED 0x000001
168 #define MP4_TRACK_IN_MOVIE 0x000002
169 #define MP4_TRACK_IN_PREVIEW 0x000004
170 typedef struct MP4_Box_data_tkhd_s
176 u64 i_modification_time;
191 } MP4_Box_data_tkhd_t;
193 typedef struct MP4_Box_data_mdhd_s
199 u64 i_modification_time;
203 /* one bit for pad */
204 /* unsigned int(5)[3] language difference with 0x60*/
205 unsigned char i_language[3];
208 } MP4_Box_data_mdhd_t;
210 typedef struct MP4_Box_data_hdlr_s
216 u32 i_handler_type; /* "vide" "soun" "hint" "odsm"
217 "crsm" "sdsm" "m7sm" "ocsm"
220 unsigned char *psz_name; /* in UTF-8 */
222 } MP4_Box_data_hdlr_t;
224 typedef struct MP4_Box_data_vmhd_s
232 } MP4_Box_data_vmhd_t;
234 typedef struct MP4_Box_data_smhd_s
242 } MP4_Box_data_smhd_t;
244 typedef struct MP4_Box_data_hmhd_s
255 } MP4_Box_data_hmhd_t;
257 typedef struct MP4_Box_data_url_s
262 unsigned char *psz_location;
264 } MP4_Box_data_url_t;
266 typedef struct MP4_Box_data_urn_s
271 unsigned char *psz_name;
272 unsigned char *psz_location;
274 } MP4_Box_data_urn_t;
276 typedef struct MP4_Box_data_dref_s
282 /* XXX it's also a container with i_entry_count entry */
283 } MP4_Box_data_dref_t;
285 typedef struct MP4_Box_data_stts_s
291 u32 *i_sample_count; /* these are array */
294 } MP4_Box_data_stts_t;
296 typedef struct MP4_Box_data_ctts_s
303 u32 *i_sample_count; /* these are array */
304 s32 *i_sample_offset;
306 } MP4_Box_data_ctts_t;
309 typedef struct MP4_Box_data_sample_soun_s
312 u16 i_data_reference_index;
319 u16 i_sampleratehi; /* timescale of track */
322 } MP4_Box_data_sample_soun_t;
324 typedef struct MP4_Box_data_sample_vide_s
327 u16 i_data_reference_index;
331 u32 i_predefined2[3];
336 u32 i_horizresolution;
337 u32 i_vertresolution;
342 u8 i_compressorname[32];
347 } MP4_Box_data_sample_vide_t;
349 typedef struct MP4_Box_data_sample_hint_s
352 u16 i_data_reference_index;
356 } MP4_Box_data_sample_hint_t;
358 typedef struct MP4_Box_data_moviehintinformation_rtp_s
360 u32 i_description_format;
361 unsigned char *psz_text;
363 } MP4_Box_data_moviehintinformation_rtp_t;
367 typedef struct MP4_Box_data_stsd_s
374 /* it contains SampleEntry handled as if it was Box */
376 } MP4_Box_data_stsd_t;
379 typedef struct MP4_Box_data_stsz_s
387 u32 *i_entry_size; /* array , empty if i_sample_size != 0 */
389 } MP4_Box_data_stsz_t;
391 typedef struct MP4_Box_data_stz2_s
396 u32 i_sample_size; /* 24 bits */
400 u32 *i_entry_size; /* array: unsigned int(i_field_size) entry_size */
402 } MP4_Box_data_stz2_t;
404 typedef struct MP4_Box_data_stsc_s
411 u32 *i_first_chunk; /* theses are arrays */
412 u32 *i_samples_per_chunk;
413 u32 *i_sample_description_index;
415 } MP4_Box_data_stsc_t;
418 typedef struct MP4_Box_data_co64_s
427 } MP4_Box_data_co64_t;
430 typedef struct MP4_Box_data_stss_s
437 u32 *i_sample_number;
439 } MP4_Box_data_stss_t;
441 typedef struct MP4_Box_data_stsh_s
448 u32 *i_shadowed_sample_number;
449 u32 *i_sync_sample_number;
451 } MP4_Box_data_stsh_t;
453 typedef struct MP4_Box_data_stdp_s
460 } MP4_Box_data_stdp_t;
462 typedef struct MP4_Box_data_padb_s
469 u16 *i_reserved1; /* 1bit */
470 u16 *i_pad2; /* 3bits */
471 u16 *i_reserved2; /* 1bit */
472 u16 *i_pad1; /* 3bits */
475 } MP4_Box_data_padb_t;
478 typedef struct MP4_Box_data_elst_s
485 u64 *i_segment_duration;
487 u16 *i_media_rate_integer;
488 u16 *i_media_rate_fraction;
491 } MP4_Box_data_elst_t;
493 typedef struct MP4_Box_data_cprt_s
498 unsigned char i_language[3];
500 unsigned char *psz_notice;
501 } MP4_Box_data_cprt_t;
504 /* DecoderConfigDescriptor */
505 typedef struct MP4_descriptor_decoder_config_s
507 u8 i_objectTypeIndication;
514 int i_decoder_specific_info_len;
515 u8 *p_decoder_specific_info;
516 /* some other stuff */
518 } MP4_descriptor_decoder_config_t;
520 typedef struct MP4_descriptor_SL_config_s
523 } MP4_descriptor_SL_config_t;
526 typedef struct MP4_descriptor_ES_s
529 int b_stream_dependence;
532 int i_stream_priority;
534 int i_depend_on_ES_ID; /* if b_stream_dependence set */
536 unsigned char *psz_URL;
538 u16 i_OCR_ES_ID; /* if b_OCRstream */
539 MP4_descriptor_decoder_config_t *p_decConfigDescr;
541 MP4_descriptor_SL_config_t *p_slConfigDescr;
543 /* some other stuff ... */
545 } MP4_descriptor_ES_t;
548 typedef struct MP4_Box_data_esds_s
553 MP4_descriptor_ES_t es_descriptor;
555 } MP4_Box_data_esds_t;
558 typedef struct MP4_Box_data_dcom_s
560 u32 i_algorithm; /* fourcc */
562 } MP4_Box_data_dcom_t;
564 typedef struct MP4_Box_data_cmvd_s
566 u32 i_uncompressed_size;
567 u32 i_compressed_size;
569 int b_compressed; /* Set to 1 if compressed data, 0 if uncompressed */
572 } MP4_Box_data_cmvd_t;
574 typedef struct MP4_Box_data_cmov_s
576 struct MP4_Box_s *p_moov; /* uncompressed moov */
578 } MP4_Box_data_cmov_t;
581 typedef struct MP4_Box_data_cmov_s
590 typedef union MP4_Box_data_s
592 MP4_Box_data_ftyp_t *p_ftyp;
593 MP4_Box_data_mvhd_t *p_mvhd;
594 MP4_Box_data_tkhd_t *p_tkhd;
595 MP4_Box_data_mdhd_t *p_mdhd;
596 MP4_Box_data_hdlr_t *p_hdlr;
597 MP4_Box_data_vmhd_t *p_vmhd;
598 MP4_Box_data_smhd_t *p_smhd;
599 MP4_Box_data_hmhd_t *p_hmhd;
600 MP4_Box_data_url_t *p_url;
601 MP4_Box_data_urn_t *p_urn;
602 MP4_Box_data_dref_t *p_dref;
603 MP4_Box_data_stts_t *p_stts;
604 MP4_Box_data_ctts_t *p_ctts;
605 MP4_Box_data_stsd_t *p_stsd;
606 MP4_Box_data_sample_vide_t *p_sample_vide;
607 MP4_Box_data_sample_soun_t *p_sample_soun;
608 MP4_Box_data_sample_hint_t *p_sample_hint;
610 MP4_Box_data_esds_t *p_esds;
612 MP4_Box_data_stsz_t *p_stsz;
613 MP4_Box_data_stz2_t *p_stz2;
614 MP4_Box_data_stsc_t *p_stsc;
615 MP4_Box_data_co64_t *p_co64;
616 MP4_Box_data_stss_t *p_stss;
617 MP4_Box_data_stsh_t *p_stsh;
618 MP4_Box_data_stdp_t *p_stdp;
619 MP4_Box_data_padb_t *p_padb;
620 MP4_Box_data_elst_t *p_elst;
621 MP4_Box_data_cprt_t *p_cprt;
623 MP4_Box_data_dcom_t *p_dcom;
624 MP4_Box_data_cmvd_t *p_cmvd;
625 MP4_Box_data_cmov_t *p_cmov;
627 MP4_Box_data_moviehintinformation_rtp_t p_moviehintinformation_rtp;
629 void *p_data; /* for unknow type */
634 /* the most basic structure */
635 typedef struct MP4_Box_s
637 off_t i_pos; /* absolute position */
642 UUID_t i_uuid; /* Set if i_type == "uuid" */
644 u64 i_size; /* always set so use it */
646 MP4_Box_data_t data; /* union of pointers on extended data depending on i_type (or i_usertype) */
648 struct MP4_Box_s *p_father; /* pointer on the father Box */
650 struct MP4_Box_s *p_first; /* pointer on the first child Box */
651 struct MP4_Box_s *p_last;
653 struct MP4_Box_s *p_next; /* pointer on the next boxes at the same level */
657 /*---------------------------------------------------------------------------*/
658 /****----------------------- High level functions ------------------------****/
659 /*---------------------------------------------------------------------------*/
661 /*****************************************************************************
662 * Function for manipulate stream easily
663 *****************************************************************************/
665 off_t MP4_TellAbsolute( input_thread_t *p_input );
666 int MP4_SeekAbsolute( input_thread_t *p_input, off_t i_pos);
667 int MP4_ReadData( input_thread_t *p_input, u8 *p_buff, int i_size );
669 /*****************************************************************************
670 * MP4_ReadRoot : Parse the entire file, and create all boxes in memory
671 *****************************************************************************
672 * The first box is a virtual box "root" and is the father for all first
675 * RETURN : 1 if succes and 0 if it failed
676 *****************************************************************************/
677 int MP4_ReadBoxRoot( input_thread_t *p_input, MP4_Box_t *p_root );
679 /*****************************************************************************
680 * MP4_FreeBox : free memory allocated after read with MP4_ReadBox
681 * this, means also children boxes
682 * XXX : all children have to be allocated by a malloc !! and
683 * p_box isn't freeing
684 *****************************************************************************/
685 void MP4_FreeBox( input_thread_t *p_input, MP4_Box_t *p_box );
687 /*****************************************************************************
688 * MP4_DumpBoxStructure: print the structure of the p_box
689 *****************************************************************************/
690 void MP4_DumpBoxStructure( input_thread_t *p_input, MP4_Box_t *p_box );
692 /*****************************************************************************
693 * MP4_CountBox: given a box, count how many child have the requested type
694 * FIXME : support GUUID
695 *****************************************************************************/
696 int MP4_CountBox( MP4_Box_t *p_box, u32 i_type );
698 /*****************************************************************************
699 * MP4_FindBox: find first box with i_type child of p_box
700 * return NULL if not found
701 *****************************************************************************/
702 MP4_Box_t *MP4_FindBox( MP4_Box_t *p_box, u32 i_type );
704 /*****************************************************************************
705 * MP4_FindNextBox: find next box with thesame type and at the same level
707 *****************************************************************************/
708 MP4_Box_t *MP4_FindNextBox( MP4_Box_t *p_box );
710 /*****************************************************************************
711 * MP4_FindNbBox: find the box i_number
712 *****************************************************************************/
713 MP4_Box_t *MP4_FindNbBox( MP4_Box_t *p_box, u32 i_number );
716 /*---------------------------------------------------------------------------*/
717 /****----------------------- Lower level functions -----------------------****/
719 /****------Use them only when you known what they really do and need------****/
721 /****---------------------------------------------------------------------****/
722 /*---------------------------------------------------------------------------*/
725 /**** ------- First some function to make abstract from input -------- */
727 /****************************************************************************
728 * MP4_InputStream create an stram with an input
730 ****************************************************************************/
731 MP4_Stream_t *MP4_InputStream( input_thread_t *p_input );
733 /****************************************************************************
734 * MP4_MemoryStream create a memory stream
735 * if p_buffer == NULL, will allocate a buffer of i_size, else
736 * it uses p_buffer XXX you have to unallocate yourself !
738 ****************************************************************************/
739 MP4_Stream_t *MP4_MemoryStream( input_thread_t *p_input,
740 int i_size, u8 *p_buffer );
742 /****************************************************************************
743 * MP4_ReadStream read from a MP4_Stream_t
745 ****************************************************************************/
746 int MP4_ReadStream( MP4_Stream_t *p_stream, u8 *p_buff, int i_size );
748 /****************************************************************************
749 * MP4_PeekStream guess it ;)
751 ****************************************************************************/
752 int MP4_PeekStream( MP4_Stream_t *p_stream, u8 **pp_peek, int i_size );
754 /****************************************************************************
755 * MP4_TellStream give absolute position in the stream
756 * XXX for a memory stream give position from begining of the buffer
757 ****************************************************************************/
758 off_t MP4_TellStream( MP4_Stream_t *p_stream );
760 /****************************************************************************
761 * MP4_SeekStream seek in a MP4_Stream_t
763 ****************************************************************************/
764 int MP4_SeekStream( MP4_Stream_t *p_stream, off_t i_pos);
768 /*****************************************************************************
769 * MP4_ReadBox : parse the actual box and the children if they exist
771 * RETURN : 1 if succes and 0 if it failed
772 *****************************************************************************/
773 int MP4_ReadBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father );
776 /*****************************************************************************
777 * MP4_ReadBoxCommon : Load only common parameters for all boxes
778 *****************************************************************************
779 * p_box need to be an already allocated MP4_Box_t, and all data
780 * will only be peek not read
782 * RETURN : 0 if it fail, 1 otherwise
783 *****************************************************************************/
784 int MP4_ReadBoxCommon( MP4_Stream_t *p_stream, MP4_Box_t *p_box );
787 /*****************************************************************************
788 * MP4_MP4_GotoBox : Go to this particular box
789 *****************************************************************************
790 * RETURN : 0 if it fail, 1 otherwise
791 *****************************************************************************/
792 int MP4_GotoBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box );
795 /*****************************************************************************
796 * MP4_MP4_NextBox : Go to the next box wiithin p_box->p_father
797 *****************************************************************************
798 * if p_box == NULL, go to the next box in witch we are( at the begining ).
799 *****************************************************************************/
800 int MP4_NextBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box );