]> git.sesse.net Git - vlc/blob - modules/demux/mp4/libmp4.h
7b2cbfc961160eddb019c39221215780b9e4189f
[vlc] / modules / demux / mp4 / libmp4.h
1 /*****************************************************************************
2  * libmp4.h : LibMP4 library for mp4 module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2004, 2010 VLC authors and VideoLAN
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
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 Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #ifndef _VLC_LIBMP4_H
24 #define _VLC_LIBMP4_H 1
25
26 #include <vlc_es.h>
27
28 #define BLOCK16x16 (1<<16)
29
30 #define ATOM_root VLC_FOURCC( 'r', 'o', 'o', 't' )
31 #define ATOM_uuid VLC_FOURCC( 'u', 'u', 'i', 'd' )
32
33 #define ATOM_ftyp VLC_FOURCC( 'f', 't', 'y', 'p' )
34 #define ATOM_moov VLC_FOURCC( 'm', 'o', 'o', 'v' )
35 #define ATOM_foov VLC_FOURCC( 'f', 'o', 'o', 'v' )
36 #define ATOM_cmov VLC_FOURCC( 'c', 'm', 'o', 'v' )
37 #define ATOM_dcom VLC_FOURCC( 'd', 'c', 'o', 'm' )
38 #define ATOM_cmvd VLC_FOURCC( 'c', 'm', 'v', 'd' )
39
40 #define ATOM_moof VLC_FOURCC( 'm', 'o', 'o', 'f' )
41 #define ATOM_mdat VLC_FOURCC( 'm', 'd', 'a', 't' )
42 #define ATOM_skip VLC_FOURCC( 's', 'k', 'i', 'p' )
43 #define ATOM_free VLC_FOURCC( 'f', 'r', 'e', 'e' )
44 #define ATOM_udta VLC_FOURCC( 'u', 'd', 't', 'a' )
45 #define ATOM_wide VLC_FOURCC( 'w', 'i', 'd', 'e' )
46
47 #define ATOM_data VLC_FOURCC( 'd', 'a', 't', 'a' )
48
49 #define ATOM_trak VLC_FOURCC( 't', 'r', 'a', 'k' )
50 #define ATOM_mvhd VLC_FOURCC( 'm', 'v', 'h', 'd' )
51 #define ATOM_tkhd VLC_FOURCC( 't', 'k', 'h', 'd' )
52 #define ATOM_tref VLC_FOURCC( 't', 'r', 'e', 'f' )
53 #define ATOM_mdia VLC_FOURCC( 'm', 'd', 'i', 'a' )
54 #define ATOM_mdhd VLC_FOURCC( 'm', 'd', 'h', 'd' )
55 #define ATOM_hdlr VLC_FOURCC( 'h', 'd', 'l', 'r' )
56 #define ATOM_minf VLC_FOURCC( 'm', 'i', 'n', 'f' )
57 #define ATOM_vmhd VLC_FOURCC( 'v', 'm', 'h', 'd' )
58 #define ATOM_smhd VLC_FOURCC( 's', 'm', 'h', 'd' )
59 #define ATOM_hmhd VLC_FOURCC( 'h', 'm', 'h', 'd' )
60 #define ATOM_dinf VLC_FOURCC( 'd', 'i', 'n', 'f' )
61 #define ATOM_url  VLC_FOURCC( 'u', 'r', 'l', ' ' )
62 #define ATOM_urn  VLC_FOURCC( 'u', 'r', 'n', ' ' )
63 #define ATOM_dref VLC_FOURCC( 'd', 'r', 'e', 'f' )
64 #define ATOM_stbl VLC_FOURCC( 's', 't', 'b', 'l' )
65 #define ATOM_stts VLC_FOURCC( 's', 't', 't', 's' )
66 #define ATOM_ctts VLC_FOURCC( 'c', 't', 't', 's' )
67 #define ATOM_stsd VLC_FOURCC( 's', 't', 's', 'd' )
68 #define ATOM_stsz VLC_FOURCC( 's', 't', 's', 'z' )
69 #define ATOM_stz2 VLC_FOURCC( 's', 't', 'z', '2' )
70 #define ATOM_stsc VLC_FOURCC( 's', 't', 's', 'c' )
71 #define ATOM_stco VLC_FOURCC( 's', 't', 'c', 'o' )
72 #define ATOM_co64 VLC_FOURCC( 'c', 'o', '6', '4' )
73 #define ATOM_stss VLC_FOURCC( 's', 't', 's', 's' )
74 #define ATOM_stsh VLC_FOURCC( 's', 't', 's', 'h' )
75 #define ATOM_stdp VLC_FOURCC( 's', 't', 'd', 'p' )
76 #define ATOM_padb VLC_FOURCC( 'p', 'a', 'd', 'b' )
77 #define ATOM_edts VLC_FOURCC( 'e', 'd', 't', 's' )
78 #define ATOM_elst VLC_FOURCC( 'e', 'l', 's', 't' )
79 #define ATOM_mvex VLC_FOURCC( 'm', 'v', 'e', 'x' )
80 #define ATOM_sdtp VLC_FOURCC( 's', 'd', 't', 'p' )
81 #define ATOM_trex VLC_FOURCC( 't', 'r', 'e', 'x' )
82 #define ATOM_mehd VLC_FOURCC( 'm', 'e', 'h', 'd' )
83 #define ATOM_mfhd VLC_FOURCC( 'm', 'f', 'h', 'd' )
84 #define ATOM_traf VLC_FOURCC( 't', 'r', 'a', 'f' )
85 #define ATOM_sidx VLC_FOURCC( 's', 'i', 'd', 'x' )
86 #define ATOM_tfhd VLC_FOURCC( 't', 'f', 'h', 'd' )
87 #define ATOM_trun VLC_FOURCC( 't', 'r', 'u', 'n' )
88 #define ATOM_cprt VLC_FOURCC( 'c', 'p', 'r', 't' )
89 #define ATOM_iods VLC_FOURCC( 'i', 'o', 'd', 's' )
90 #define ATOM_pasp VLC_FOURCC( 'p', 'a', 's', 'p' )
91 #define ATOM_mfra VLC_FOURCC( 'm', 'f', 'r', 'a' )
92 #define ATOM_mfro VLC_FOURCC( 'm', 'f', 'r', 'o' )
93 #define ATOM_tfra VLC_FOURCC( 't', 'f', 'r', 'a' )
94
95 #define ATOM_nmhd VLC_FOURCC( 'n', 'm', 'h', 'd' )
96 #define ATOM_mp2v VLC_FOURCC( 'm', 'p', '2', 'v' )
97 #define ATOM_mp4v VLC_FOURCC( 'm', 'p', '4', 'v' )
98 #define ATOM_mp4a VLC_FOURCC( 'm', 'p', '4', 'a' )
99 #define ATOM_mp4s VLC_FOURCC( 'm', 'p', '4', 's' )
100 #define ATOM_vide VLC_FOURCC( 'v', 'i', 'd', 'e' )
101 #define ATOM_soun VLC_FOURCC( 's', 'o', 'u', 'n' )
102 #define ATOM_hint VLC_FOURCC( 'h', 'i', 'n', 't' )
103 #define ATOM_hdv2 VLC_FOURCC( 'h', 'd', 'v', '2' )
104
105 #define ATOM_dpnd VLC_FOURCC( 'd', 'p', 'n', 'd' )
106 #define ATOM_ipir VLC_FOURCC( 'i', 'p', 'i', 'r' )
107 #define ATOM_mpod VLC_FOURCC( 'm', 'p', 'o', 'd' )
108 #define ATOM_hnti VLC_FOURCC( 'h', 'n', 't', 'i' )
109 #define ATOM_rtp  VLC_FOURCC( 'r', 't', 'p', ' ' )
110
111 #define ATOM_isom VLC_FOURCC( 'i', 's', 'o', 'm' )
112 #define ATOM_3gp4 VLC_FOURCC( '3', 'g', 'p', '4' )
113 #define ATOM_esds VLC_FOURCC( 'e', 's', 'd', 's' )
114
115 #define ATOM_lpcm VLC_FOURCC( 'l', 'p', 'c', 'm' )
116 #define ATOM__mp3 VLC_FOURCC( '.', 'm', 'p', '3' )
117 #define ATOM_ms02 VLC_FOURCC( 'm', 's', 0x0, 0x02 )
118 #define ATOM_ms11 VLC_FOURCC( 'm', 's', 0x0, 0x11 )
119 #define ATOM_ms55 VLC_FOURCC( 'm', 's', 0x0, 0x55 )
120 #define ATOM_twos VLC_FOURCC( 't', 'w', 'o', 's' )
121 #define ATOM_sowt VLC_FOURCC( 's', 'o', 'w', 't' )
122 #define ATOM_QDMC VLC_FOURCC( 'Q', 'D', 'M', 'C' )
123 #define ATOM_QDM2 VLC_FOURCC( 'Q', 'D', 'M', '2' )
124 #define ATOM_ima4 VLC_FOURCC( 'i', 'm', 'a', '4' )
125 #define ATOM_IMA4 VLC_FOURCC( 'I', 'M', 'A', '4' )
126 #define ATOM_dvi  VLC_FOURCC( 'd', 'v', 'i', ' ' )
127 #define ATOM_MAC3 VLC_FOURCC( 'M', 'A', 'C', '3' )
128 #define ATOM_MAC6 VLC_FOURCC( 'M', 'A', 'C', '6' )
129 #define ATOM_alaw VLC_FOURCC( 'a', 'l', 'a', 'w' )
130 #define ATOM_ulaw VLC_FOURCC( 'u', 'l', 'a', 'w' )
131 #define ATOM_Qclp VLC_FOURCC( 'Q', 'c', 'l', 'p' )
132 #define ATOM_samr VLC_FOURCC( 's', 'a', 'm', 'r' )
133 #define ATOM_sawb VLC_FOURCC( 's', 'a', 'w', 'b' )
134 #define ATOM_OggS VLC_FOURCC( 'O', 'g', 'g', 'S' )
135 #define ATOM_alac VLC_FOURCC( 'a', 'l', 'a', 'c' )
136 #define ATOM_dac3 VLC_FOURCC( 'd', 'a', 'c', '3' )
137 #define ATOM_dec3 VLC_FOURCC( 'd', 'e', 'c', '3' )
138 #define ATOM_dvc1 VLC_FOURCC( 'd', 'v', 'c', '1' )
139 #define ATOM_enda VLC_FOURCC( 'e', 'n', 'd', 'a' )
140 #define ATOM_gnre VLC_FOURCC( 'g', 'n', 'r', 'e' )
141 #define ATOM_trkn VLC_FOURCC( 't', 'r', 'k', 'n' )
142
143 #define ATOM_zlib VLC_FOURCC( 'z', 'l', 'i', 'b' )
144 #define ATOM_SVQ1 VLC_FOURCC( 'S', 'V', 'Q', '1' )
145 #define ATOM_SVQ3 VLC_FOURCC( 'S', 'V', 'Q', '3' )
146 #define ATOM_ZyGo VLC_FOURCC( 'Z', 'y', 'G', 'o' )
147 #define ATOM_3IV1 VLC_FOURCC( '3', 'I', 'V', '1' )
148 #define ATOM_3iv1 VLC_FOURCC( '3', 'i', 'v', '1' )
149 #define ATOM_3IV2 VLC_FOURCC( '3', 'I', 'V', '2' )
150 #define ATOM_3iv2 VLC_FOURCC( '3', 'i', 'v', '2' )
151 #define ATOM_3IVD VLC_FOURCC( '3', 'I', 'V', 'D' )
152 #define ATOM_3ivd VLC_FOURCC( '3', 'i', 'v', 'd' )
153 #define ATOM_3VID VLC_FOURCC( '3', 'V', 'I', 'D' )
154 #define ATOM_3vid VLC_FOURCC( '3', 'v', 'i', 'd' )
155 #define ATOM_h263 VLC_FOURCC( 'h', '2', '6', '3' )
156 #define ATOM_s263 VLC_FOURCC( 's', '2', '6', '3' )
157 #define ATOM_DIVX VLC_FOURCC( 'D', 'I', 'V', 'X' )
158 #define ATOM_XVID VLC_FOURCC( 'X', 'V', 'I', 'D' )
159 #define ATOM_cvid VLC_FOURCC( 'c', 'v', 'i', 'd' )
160 #define ATOM_mjpa VLC_FOURCC( 'm', 'j', 'p', 'a' )
161 #define ATOM_mjpb VLC_FOURCC( 'm', 'j', 'q', 't' )
162 #define ATOM_mjqt VLC_FOURCC( 'm', 'j', 'h', 't' )
163 #define ATOM_mjht VLC_FOURCC( 'm', 'j', 'p', 'b' )
164 #define ATOM_VP31 VLC_FOURCC( 'V', 'P', '3', '1' )
165 #define ATOM_vp31 VLC_FOURCC( 'v', 'p', '3', '1' )
166 #define ATOM_h264 VLC_FOURCC( 'h', '2', '6', '4' )
167 #define ATOM_qdrw VLC_FOURCC( 'q', 'd', 'r', 'w' )
168
169 #define ATOM_avc1 VLC_FOURCC( 'a', 'v', 'c', '1' )
170 #define ATOM_avcC VLC_FOURCC( 'a', 'v', 'c', 'C' )
171 #define ATOM_m4ds VLC_FOURCC( 'm', '4', 'd', 's' )
172
173 #define ATOM_hvcC VLC_FOURCC( 'h', 'v', 'c', 'C' )
174
175 #define ATOM_dvc  VLC_FOURCC( 'd', 'v', 'c', ' ' )
176 #define ATOM_dvp  VLC_FOURCC( 'd', 'v', 'p', ' ' )
177 #define ATOM_dv5n VLC_FOURCC( 'd', 'v', '5', 'n' )
178 #define ATOM_dv5p VLC_FOURCC( 'd', 'v', '5', 'p' )
179 #define ATOM_raw  VLC_FOURCC( 'r', 'a', 'w', ' ' )
180
181 #define ATOM_jpeg VLC_FOURCC( 'j', 'p', 'e', 'g' )
182
183 #define ATOM_yv12 VLC_FOURCC( 'y', 'v', '1', '2' )
184 #define ATOM_yuv2 VLC_FOURCC( 'y', 'u', 'v', '2' )
185
186 #define ATOM_rmra VLC_FOURCC( 'r', 'm', 'r', 'a' )
187 #define ATOM_rmda VLC_FOURCC( 'r', 'm', 'd', 'a' )
188 #define ATOM_rdrf VLC_FOURCC( 'r', 'd', 'r', 'f' )
189 #define ATOM_rmdr VLC_FOURCC( 'r', 'm', 'd', 'r' )
190 #define ATOM_rmvc VLC_FOURCC( 'r', 'm', 'v', 'c' )
191 #define ATOM_rmcd VLC_FOURCC( 'r', 'm', 'c', 'd' )
192 #define ATOM_rmqu VLC_FOURCC( 'r', 'm', 'q', 'u' )
193 #define ATOM_alis VLC_FOURCC( 'a', 'l', 'i', 's' )
194
195 #define ATOM_gmhd VLC_FOURCC( 'g', 'm', 'h', 'd' )
196 #define ATOM_wave VLC_FOURCC( 'w', 'a', 'v', 'e' )
197
198 #define ATOM_drms VLC_FOURCC( 'd', 'r', 'm', 's' )
199 #define ATOM_sinf VLC_FOURCC( 's', 'i', 'n', 'f' )
200 #define ATOM_schi VLC_FOURCC( 's', 'c', 'h', 'i' )
201 #define ATOM_user VLC_FOURCC( 'u', 's', 'e', 'r' )
202 #define ATOM_key  VLC_FOURCC( 'k', 'e', 'y', ' ' )
203 #define ATOM_iviv VLC_FOURCC( 'i', 'v', 'i', 'v' )
204 #define ATOM_name VLC_FOURCC( 'n', 'a', 'm', 'e' )
205 #define ATOM_priv VLC_FOURCC( 'p', 'r', 'i', 'v' )
206 #define ATOM_drmi VLC_FOURCC( 'd', 'r', 'm', 'i' )
207 #define ATOM_frma VLC_FOURCC( 'f', 'r', 'm', 'a' )
208 #define ATOM_skcr VLC_FOURCC( 's', 'k', 'c', 'r' )
209
210 #define ATOM_text VLC_FOURCC( 't', 'e', 'x', 't' )
211 #define ATOM_tx3g VLC_FOURCC( 't', 'x', '3', 'g' )
212 #define ATOM_subp VLC_FOURCC( 's', 'u', 'b', 'p' )
213 #define ATOM_sbtl VLC_FOURCC( 's', 'b', 't', 'l' )
214
215 #define ATOM_0xa9nam VLC_FOURCC( 0xa9, 'n', 'a', 'm' )
216 #define ATOM_0xa9aut VLC_FOURCC( 0xa9, 'a', 'u', 't' )
217 #define ATOM_0xa9cpy VLC_FOURCC( 0xa9, 'c', 'p', 'y' )
218 #define ATOM_0xa9inf VLC_FOURCC( 0xa9, 'i', 'n', 'f' )
219 #define ATOM_0xa9ART VLC_FOURCC( 0xa9, 'A', 'R', 'T' )
220 #define ATOM_0xa9des VLC_FOURCC( 0xa9, 'd', 'e', 's' )
221 #define ATOM_0xa9dir VLC_FOURCC( 0xa9, 'd', 'i', 'r' )
222 #define ATOM_0xa9cmt VLC_FOURCC( 0xa9, 'c', 'm', 't' )
223 #define ATOM_0xa9req VLC_FOURCC( 0xa9, 'r', 'e', 'q' )
224 #define ATOM_0xa9day VLC_FOURCC( 0xa9, 'd', 'a', 'y' )
225 #define ATOM_0xa9fmt VLC_FOURCC( 0xa9, 'f', 'm', 't' )
226 #define ATOM_0xa9prd VLC_FOURCC( 0xa9, 'p', 'r', 'd' )
227 #define ATOM_0xa9prf VLC_FOURCC( 0xa9, 'p', 'r', 'f' )
228 #define ATOM_0xa9src VLC_FOURCC( 0xa9, 's', 'r', 'c' )
229 #define ATOM_0xa9alb VLC_FOURCC( 0xa9, 'a', 'l', 'b' )
230 #define ATOM_0xa9dis VLC_FOURCC( 0xa9, 'd', 'i', 's' )
231 #define ATOM_0xa9enc VLC_FOURCC( 0xa9, 'e', 'n', 'c' )
232 #define ATOM_0xa9trk VLC_FOURCC( 0xa9, 't', 'r', 'k' )
233 #define ATOM_0xa9url VLC_FOURCC( 0xa9, 'u', 'r', 'l' )
234 #define ATOM_0xa9dsa VLC_FOURCC( 0xa9, 'd', 's', 'a' )
235 #define ATOM_0xa9hst VLC_FOURCC( 0xa9, 'h', 's', 't' )
236 #define ATOM_0xa9ope VLC_FOURCC( 0xa9, 'o', 'p', 'e' )
237 #define ATOM_0xa9wrt VLC_FOURCC( 0xa9, 'w', 'r', 't' )
238 #define ATOM_0xa9com VLC_FOURCC( 0xa9, 'c', 'o', 'm' )
239 #define ATOM_0xa9gen VLC_FOURCC( 0xa9, 'g', 'e', 'n' )
240 #define ATOM_0xa9too VLC_FOURCC( 0xa9, 't', 'o', 'o' )
241 #define ATOM_0xa9wrn VLC_FOURCC( 0xa9, 'w', 'r', 'n' )
242 #define ATOM_0xa9swr VLC_FOURCC( 0xa9, 's', 'w', 'r' )
243 #define ATOM_0xa9mak VLC_FOURCC( 0xa9, 'm', 'a', 'k' )
244 #define ATOM_0xa9mod VLC_FOURCC( 0xa9, 'm', 'o', 'd' )
245 #define ATOM_0xa9PRD VLC_FOURCC( 0xa9, 'P', 'R', 'D' )
246 #define ATOM_0xa9grp VLC_FOURCC( 0xa9, 'g', 'r', 'p' )
247 #define ATOM_0xa9lyr VLC_FOURCC( 0xa9, 'l', 'y', 'r' )
248 #define ATOM_0xa9gen VLC_FOURCC( 0xa9, 'g', 'e', 'n' )
249 #define ATOM_0xa9st3 VLC_FOURCC( 0xa9, 's', 't', '3' )
250 #define ATOM_0xa9ard VLC_FOURCC( 0xa9, 'a', 'r', 'd' )
251 #define ATOM_0xa9arg VLC_FOURCC( 0xa9, 'a', 'r', 'g' )
252 #define ATOM_0xa9cak VLC_FOURCC( 0xa9, 'c', 'a', 'k' )
253 #define ATOM_0xa9con VLC_FOURCC( 0xa9, 'c', 'o', 'n' )
254 #define ATOM_0xa9des VLC_FOURCC( 0xa9, 'd', 'e', 's' )
255 #define ATOM_0xa9lnt VLC_FOURCC( 0xa9, 'l', 'n', 't' )
256 #define ATOM_0xa9phg VLC_FOURCC( 0xa9, 'p', 'h', 'g' )
257 #define ATOM_0xa9pub VLC_FOURCC( 0xa9, 'p', 'u', 'b' )
258 #define ATOM_0xa9sne VLC_FOURCC( 0xa9, 's', 'n', 'e' )
259 #define ATOM_0xa9sol VLC_FOURCC( 0xa9, 's', 'o', 'l' )
260 #define ATOM_0xa9thx VLC_FOURCC( 0xa9, 't', 'h', 'x' )
261 #define ATOM_0xa9xpd VLC_FOURCC( 0xa9, 'x', 'p', 'd' )
262 #define ATOM_chpl VLC_FOURCC( 'c', 'h', 'p', 'l' )
263 #define ATOM_WLOC VLC_FOURCC( 'W', 'L', 'O', 'C' )
264
265 #define ATOM_meta VLC_FOURCC( 'm', 'e', 't', 'a' )
266 #define ATOM_ilst VLC_FOURCC( 'i', 'l', 's', 't' )
267 #define ATOM_covr VLC_FOURCC( 'c', 'o', 'v', 'r' )
268
269 #define ATOM_chap VLC_FOURCC( 'c', 'h', 'a', 'p' )
270
271 /* Do you want some debug information on all read boxes ? */
272 #ifndef NDEBUG
273 # define MP4_VERBOSE  1
274 #endif
275
276 struct MP4_Box_s;
277
278
279 /* uuid Universal Unique IDentifiers */
280 typedef struct UUID_s
281 {
282     uint8_t b[16];
283 } UUID_t;
284
285 /* specific structure for all boxes */
286
287 typedef struct MP4_Box_data_tfxd_s
288 {
289     uint8_t  i_version;
290     uint32_t i_flags;
291
292     uint64_t i_fragment_duration;
293     uint64_t i_fragment_abs_time;
294
295 } MP4_Box_data_tfxd_t;
296
297 typedef struct TfrfBoxDataFields_s
298 {
299     uint64_t i_fragment_duration;
300     uint64_t i_fragment_abs_time;
301
302 } TfrfBoxDataFields_t;
303
304 typedef struct MP4_Box_data_tfrf_s
305 {
306     uint8_t  i_version;
307     uint8_t  i_fragment_count;
308     uint32_t i_flags;
309
310     TfrfBoxDataFields_t *p_tfrf_data_fields;
311
312 } MP4_Box_data_tfrf_t;
313
314 typedef struct MP4_Box_data_ftyp_s
315 {
316     uint32_t i_major_brand;
317     uint32_t i_minor_version;
318
319     uint32_t i_compatible_brands_count;
320     uint32_t *i_compatible_brands;
321
322 } MP4_Box_data_ftyp_t;
323
324 typedef struct MP4_Box_data_mvhd_s
325 {
326     uint8_t  i_version;
327     uint32_t i_flags;
328
329     uint64_t i_creation_time;
330     uint64_t i_modification_time;
331     uint32_t i_timescale;
332     uint64_t i_duration;
333
334     int32_t  i_rate;
335     int16_t  i_volume;
336     int16_t  i_reserved1;
337     uint32_t i_reserved2[2];
338     int32_t  i_matrix[9];
339     uint32_t i_predefined[6];
340     uint32_t i_next_track_id;
341
342 } MP4_Box_data_mvhd_t;
343
344 #define MP4_TRACK_ENABLED    0x000001
345 #define MP4_TRACK_IN_MOVIE   0x000002
346 #define MP4_TRACK_IN_PREVIEW 0x000004
347 typedef struct MP4_Box_data_tkhd_s
348 {
349     uint8_t  i_version;
350     uint32_t i_flags;
351
352     uint64_t i_creation_time;
353     uint64_t i_modification_time;
354     uint32_t i_track_ID;
355     uint32_t i_reserved;
356     uint64_t i_duration;
357
358     uint32_t i_reserved2[2];
359     int16_t  i_layer;
360     int16_t  i_predefined;
361
362     int16_t  i_volume;
363     uint16_t i_reserved3;
364     int32_t  i_matrix[9];
365     int32_t  i_width;
366     int32_t  i_height;
367     float    f_rotation;
368
369 } MP4_Box_data_tkhd_t;
370
371 typedef struct MP4_Box_data_mdhd_s
372 {
373     uint8_t  i_version;
374     uint32_t i_flags;
375
376     uint64_t i_creation_time;
377     uint64_t i_modification_time;
378     uint32_t i_timescale;
379     uint64_t i_duration;
380
381     /* one bit for pad */
382     uint16_t      i_language_code;
383     /* unsigned int(5)[3] language difference with 0x60*/
384     unsigned char i_language[3];
385     uint16_t i_predefined;
386
387 } MP4_Box_data_mdhd_t;
388
389 typedef struct MP4_Box_data_hdlr_s
390 {
391     uint8_t  i_version;
392     uint32_t i_flags;
393
394     uint32_t i_predefined;
395     uint32_t i_handler_type; /* "vide" "soun" "hint" "odsm"
396                            "crsm" "sdsm" "m7sm" "ocsm"
397                            "ipsm" "mjsm" */
398
399     unsigned char *psz_name; /* in UTF-8 */
400
401 } MP4_Box_data_hdlr_t;
402
403 typedef struct MP4_Box_data_vmhd_s
404 {
405     uint8_t  i_version;
406     uint32_t i_flags;
407
408     int16_t  i_graphics_mode;
409     int16_t  i_opcolor[3];
410
411 } MP4_Box_data_vmhd_t;
412
413 typedef struct MP4_Box_data_smhd_s
414 {
415     uint8_t  i_version;
416     uint32_t i_flags;
417
418     int16_t  i_balance;
419     int16_t  i_reserved;
420
421 } MP4_Box_data_smhd_t;
422
423 typedef struct MP4_Box_data_hmhd_s
424 {
425     uint8_t  i_version;
426     uint32_t i_flags;
427
428     uint16_t i_max_PDU_size;
429     uint16_t i_avg_PDU_size;
430     uint32_t i_max_bitrate;
431     uint32_t i_avg_bitrate;
432     uint32_t i_reserved;
433
434 } MP4_Box_data_hmhd_t;
435
436 typedef struct MP4_Box_data_url_s
437 {
438     uint8_t  i_version;
439     uint32_t i_flags;
440
441     char *psz_location;
442
443 } MP4_Box_data_url_t;
444
445 typedef struct MP4_Box_data_urn_s
446 {
447     uint8_t  i_version;
448     uint32_t i_flags;
449
450     char *psz_name;
451     char *psz_location;
452
453 } MP4_Box_data_urn_t;
454
455 typedef struct MP4_Box_data_dref_s
456 {
457     uint8_t  i_version;
458     uint32_t i_flags;
459
460     uint32_t i_entry_count;
461 /* XXX it's also a container with i_entry_count entry */
462 } MP4_Box_data_dref_t;
463
464 typedef struct MP4_Box_data_stts_s
465 {
466     uint8_t  i_version;
467     uint32_t i_flags;
468
469     uint32_t i_entry_count;
470     uint32_t *pi_sample_count; /* these are array */
471     int32_t  *pi_sample_delta;
472
473 } MP4_Box_data_stts_t;
474
475 typedef struct MP4_Box_data_ctts_s
476 {
477     uint8_t  i_version;
478     uint32_t i_flags;
479
480     uint32_t i_entry_count;
481
482     uint32_t *pi_sample_count; /* these are array */
483     int32_t *pi_sample_offset;
484
485 } MP4_Box_data_ctts_t;
486
487
488 typedef struct MP4_Box_data_sample_soun_s
489 {
490     uint8_t  i_reserved1[6];
491     uint16_t i_data_reference_index;
492
493     //uint32_t i_reserved2[2];
494     uint16_t i_qt_version;
495     uint16_t i_qt_revision_level;
496     uint32_t i_qt_vendor;
497
498     uint16_t i_channelcount;
499     uint16_t i_samplesize;
500     uint16_t i_predefined;
501     uint16_t i_reserved3;
502     uint16_t i_sampleratehi; /* timescale of track */
503     uint16_t i_sampleratelo;
504
505     /* for version 1 (i_reserved1[0] == 1) */
506     uint32_t i_sample_per_packet;
507     uint32_t i_bytes_per_packet;
508     uint32_t i_bytes_per_frame;
509     uint32_t i_bytes_per_sample;
510
511     /* v2 */
512     uint32_t i_constbitsperchannel; /* consts are nonzero only if constant */
513     uint32_t i_formatflags;
514     uint32_t i_constbytesperaudiopacket;
515     uint32_t i_constLPCMframesperaudiopacket;
516
517     /* XXX hack */
518     int     i_qt_description;
519     uint8_t *p_qt_description;
520
521 } MP4_Box_data_sample_soun_t;
522
523 typedef struct MP4_Box_data_sample_vide_s
524 {
525     uint8_t  i_reserved1[6];
526     uint16_t i_data_reference_index;
527
528     uint16_t i_qt_version;
529     uint16_t i_qt_revision_level;
530     uint32_t i_qt_vendor;
531
532     uint32_t i_qt_temporal_quality;
533     uint32_t i_qt_spatial_quality;
534
535     int16_t  i_width;
536     int16_t  i_height;
537
538     uint32_t i_horizresolution;
539     uint32_t i_vertresolution;
540
541     uint32_t i_qt_data_size;
542     uint16_t i_qt_frame_count;
543
544     uint8_t  i_compressorname[32];
545     int16_t  i_depth;
546
547     int16_t  i_qt_color_table;
548
549     /* XXX hack ImageDescription */
550     int     i_qt_image_description;
551     uint8_t *p_qt_image_description;
552
553 } MP4_Box_data_sample_vide_t;
554
555 #define MP4_TEXT_DISPLAY_FLAG_DONT_DISPLAY       (1<<0)
556 #define MP4_TEXT_DISPLAY_FLAG_AUTO_SCALE         (1<<1)
557 #define MP4_TEXT_DISPLAY_FLAG_CLIP_TO_TEXT_BOX   (1<<2)
558 #define MP4_TEXT_DISPLAY_FLAG_USE_MOVIE_BG_COLOR (1<<3)
559 #define MP4_TEXT_DISPLAY_FLAG_SHRINK_TEXT_BOX_TO_FIT (1<<4)
560 #define MP4_TEXT_DISPLAY_FLAG_SCROLL_IN          (1<<5)
561 #define MP4_TEXT_DISPLAY_FLAG_SCROLL_OUT         (1<<6)
562 #define MP4_TEXT_DISPLAY_FLAG_HORIZONTAL_SCROLL  (1<<7)
563 #define MP4_TEXT_DISPLAY_FLAG_REVERSE_SCROLL     (1<<8)
564 #define MP4_TEXT_DISPLAY_FLAG_CONTINUOUS_SCROLL  (1<<9)
565 #define MP4_TEXT_DISPLAY_FLAG_FLOW_HORIZONTAL    (1<<10)
566 #define MP4_TEXT_DISPLAY_FLAG_CONTINUOUS_KARAOKE (1<<11)
567 #define MP4_TEXT_DISPLAY_FLAG_DROP_SHADOW        (1<<12)
568 #define MP4_TEXT_DISPLAY_FLAG_ANTI_ALIAS         (1<<13)
569 #define MP4_TEXT_DISPLAY_FLAG_KEYED_TEXT         (1<<14)
570 #define MP4_TEXT_DISPLAY_FLAG_INVERSE_HILITE     (1<<15)
571 #define MP4_TEXT_DISPLAY_FLAG_COLOR_HILITE       (1<<16)
572 #define MP4_TEXT_DISPLAY_FLAG_WRITE_VERTICALLY   (1<<17)
573
574 typedef struct
575 {
576     uint32_t i_reserved1;
577     uint16_t i_reserved2;
578
579     uint16_t i_data_reference_index;
580
581     uint32_t i_display_flags;   // TextDescription and Tx3gDescription
582
583     int8_t i_justification_horizontal; // left(0), centered(1), right(-1)
584     int8_t i_justification_vertical;   // top(0), centered(1), bottom(-1)
585
586     uint16_t i_background_color[4];
587
588     uint16_t i_text_box_top;
589     uint16_t i_text_box_left;
590     uint16_t i_text_box_bottom;
591     uint16_t i_text_box_right;
592
593     uint32_t i_reserved3;
594
595     uint16_t i_font_id;
596     uint8_t  i_font_face;
597     uint8_t  i_font_size;
598     uint32_t i_font_color; //RGBA
599
600     // TODO to complete
601 } MP4_Box_data_sample_text_t;
602
603 typedef struct MP4_Box_data_sample_hint_s
604 {
605     uint8_t  i_reserved1[6];
606     uint16_t i_data_reference_index;
607
608     uint8_t *p_data;
609
610 } MP4_Box_data_sample_hint_t;
611
612 typedef struct MP4_Box_data_moviehintinformation_rtp_s
613 {
614     uint32_t i_description_format;
615     unsigned char *psz_text;
616
617 } MP4_Box_data_moviehintinformation_rtp_t;
618
619
620
621 typedef struct MP4_Box_data_stsd_s
622 {
623     uint8_t  i_version;
624     uint32_t i_flags;
625
626     uint32_t i_entry_count;
627
628     /* it contains SampleEntry handled as if it was Box */
629
630 } MP4_Box_data_stsd_t;
631
632
633 typedef struct MP4_Box_data_stsz_s
634 {
635     uint8_t  i_version;
636     uint32_t i_flags;
637
638     uint32_t i_sample_size;
639     uint32_t i_sample_count;
640
641     uint32_t *i_entry_size; /* array , empty if i_sample_size != 0 */
642
643 } MP4_Box_data_stsz_t;
644
645 typedef struct MP4_Box_data_stz2_s
646 {
647     uint8_t  i_version;
648     uint32_t i_flags;
649
650     uint32_t i_sample_size; /* 24 bits */
651     uint8_t  i_field_size;
652     uint32_t i_sample_count;
653
654     uint32_t *i_entry_size; /* array: unsigned int(i_field_size) entry_size */
655
656 } MP4_Box_data_stz2_t;
657
658 typedef struct MP4_Box_data_stsc_s
659 {
660     uint8_t  i_version;
661     uint32_t i_flags;
662
663     uint32_t i_entry_count;
664
665     uint32_t *i_first_chunk; /* theses are arrays */
666     uint32_t *i_samples_per_chunk;
667     uint32_t *i_sample_description_index;
668
669 } MP4_Box_data_stsc_t;
670
671
672 typedef struct MP4_Box_data_co64_s
673 {
674     uint8_t  i_version;
675     uint32_t i_flags;
676
677     uint32_t i_entry_count;
678
679     uint64_t *i_chunk_offset;
680
681 } MP4_Box_data_co64_t;
682
683
684 typedef struct MP4_Box_data_stss_s
685 {
686     uint8_t  i_version;
687     uint32_t i_flags;
688
689     uint32_t i_entry_count;
690
691     uint32_t *i_sample_number;
692
693 } MP4_Box_data_stss_t;
694
695 typedef struct MP4_Box_data_stsh_s
696 {
697     uint8_t  i_version;
698     uint32_t i_flags;
699
700     uint32_t i_entry_count;
701
702     uint32_t *i_shadowed_sample_number;
703     uint32_t *i_sync_sample_number;
704
705 } MP4_Box_data_stsh_t;
706
707 typedef struct MP4_Box_data_stdp_s
708 {
709     uint8_t  i_version;
710     uint32_t i_flags;
711
712     uint16_t *i_priority;
713
714 } MP4_Box_data_stdp_t;
715
716 typedef struct MP4_Box_data_padb_s
717 {
718     uint8_t  i_version;
719     uint32_t i_flags;
720
721     uint32_t i_sample_count;
722
723     uint16_t *i_reserved1;   /* 1bit  */
724     uint16_t *i_pad2;        /* 3bits */
725     uint16_t *i_reserved2;   /* 1bit  */
726     uint16_t *i_pad1;        /* 3bits */
727
728
729 } MP4_Box_data_padb_t;
730
731
732 typedef struct MP4_Box_data_elst_s
733 {
734     uint8_t  i_version;
735     uint32_t i_flags;
736
737     uint32_t i_entry_count;
738
739     uint64_t *i_segment_duration;
740     int64_t  *i_media_time;
741     uint16_t *i_media_rate_integer;
742     uint16_t *i_media_rate_fraction;
743
744
745 } MP4_Box_data_elst_t;
746
747 typedef struct MP4_Box_data_cprt_s
748 {
749     uint8_t  i_version;
750     uint32_t i_flags;
751     /* 1 pad bit */
752     unsigned char i_language[3];
753
754     char *psz_notice;
755 } MP4_Box_data_cprt_t;
756
757
758 /* DecoderConfigDescriptor */
759 typedef struct MP4_descriptor_decoder_config_s
760 {
761     uint8_t i_objectTypeIndication;
762     uint8_t i_streamType;
763     int     b_upStream;
764     int     i_buffer_sizeDB;
765     int     i_max_bitrate;
766     int     i_avg_bitrate;
767
768     int     i_decoder_specific_info_len;
769     uint8_t *p_decoder_specific_info;
770     /* some other stuff */
771
772 } MP4_descriptor_decoder_config_t;
773
774 typedef struct MP4_descriptor_SL_config_s
775 {
776
777     int i_dummy; /* ANSI C forbids empty structures */
778
779 } MP4_descriptor_SL_config_t;
780
781
782 typedef struct MP4_descriptor_ES_s
783 {
784     uint16_t i_ES_ID;
785     int      b_stream_dependence;
786     int      b_url;
787     int      b_OCRstream;
788     int      i_stream_priority;
789
790     int      i_depend_on_ES_ID; /* if b_stream_dependence set */
791
792     unsigned char *psz_URL;
793
794     uint16_t i_OCR_ES_ID;       /* if b_OCRstream */
795     MP4_descriptor_decoder_config_t *p_decConfigDescr;
796
797     MP4_descriptor_SL_config_t *p_slConfigDescr;
798
799     /* some other stuff ... */
800
801 } MP4_descriptor_ES_t;
802
803 /* ES descriptor */
804 typedef struct MP4_Box_data_esds_s
805 {
806     uint8_t  i_version;
807     uint32_t i_flags;
808
809     MP4_descriptor_ES_t es_descriptor;
810
811 } MP4_Box_data_esds_t;
812
813
814 typedef struct MP4_Box_data_dcom_s
815 {
816     uint32_t i_algorithm; /* fourcc */
817
818 } MP4_Box_data_dcom_t;
819
820 typedef struct MP4_Box_data_cmvd_s
821 {
822     uint32_t i_uncompressed_size;
823     uint32_t i_compressed_size;
824
825     int     b_compressed; /* Set to 1 if compressed data, 0 if uncompressed */
826     uint8_t *p_data;
827
828 } MP4_Box_data_cmvd_t;
829
830 typedef struct MP4_Box_data_cmov_s
831 {
832     struct MP4_Box_s *p_moov; /* uncompressed moov */
833
834 } MP4_Box_data_cmov_t;
835
836 typedef struct
837 {
838     uint32_t i_type;
839 } MP4_Box_data_frma_t;
840
841 typedef struct
842 {
843     uint32_t i_init;
844     uint32_t i_encr;
845     uint32_t i_decr;
846 } MP4_Box_data_skcr_t;
847
848 typedef struct
849 {
850     uint8_t  i_version;
851     uint32_t i_flags;
852
853     uint32_t i_ref_type;
854     char     *psz_ref;
855
856 } MP4_Box_data_rdrf_t;
857
858 typedef struct
859 {
860     uint8_t  i_version;
861     uint32_t i_flags;
862
863     uint32_t i_rate;
864
865 } MP4_Box_data_rmdr_t;
866
867 typedef struct
868 {
869     uint8_t  i_version;
870     uint32_t i_flags;
871
872     uint32_t i_gestaltType;
873     uint32_t i_val1;
874     uint32_t i_val2;
875     uint16_t i_checkType;   /* 0: val1 is version min
876                                1: gestalt value & val2 == val1 */
877
878 } MP4_Box_data_rmvc_t;
879
880 typedef struct
881 {
882     uint8_t  i_version;
883     uint32_t i_flags;
884
885
886 } MP4_Box_data_rmcd_t;
887
888 typedef struct
889 {
890     uint32_t i_quality;
891
892 } MP4_Box_data_rmqu_t;
893
894 typedef struct MP4_Box_data_mfhd_s
895 {
896     uint8_t  i_version;
897     uint32_t i_flags;
898
899     uint32_t i_sequence_number;
900
901 } MP4_Box_data_mfhd_t;
902
903 typedef struct MP4_Box_sidx_item_s
904 {
905     bool     b_reference_type;
906     uint32_t i_referenced_size;
907     uint32_t i_subsegment_duration;
908     bool     b_starts_with_SAP;
909     uint8_t  i_SAP_type;
910     uint32_t i_SAP_delta_time;
911
912 } MP4_Box_sidx_item_t;
913
914 typedef struct MP4_Box_data_sidx_s
915 {
916     uint8_t  i_version;
917     uint32_t i_flags;
918
919     uint32_t i_reference_ID;
920     uint32_t i_timescale;
921     uint64_t i_earliest_presentation_time;
922     uint64_t i_first_offset;
923     uint16_t i_reference_count;
924
925     MP4_Box_sidx_item_t *p_items;
926
927 } MP4_Box_data_sidx_t;
928
929 #define MP4_TFHD_BASE_DATA_OFFSET     (1LL<<0)
930 #define MP4_TFHD_SAMPLE_DESC_INDEX    (1LL<<1)
931 #define MP4_TFHD_DFLT_SAMPLE_DURATION (1LL<<3)
932 #define MP4_TFHD_DFLT_SAMPLE_SIZE     (1LL<<4)
933 #define MP4_TFHD_DFLT_SAMPLE_FLAGS    (1LL<<5)
934 #define MP4_TFHD_DURATION_IS_EMPTY    (1LL<<16)
935 #define MP4_TFHD_DEFAULT_BASE_IS_MOOF (1LL<<17)
936 typedef struct MP4_Box_data_tfhd_s
937 {
938     uint8_t  i_version;
939     bool     b_empty;
940     uint32_t i_flags;
941     uint32_t i_track_ID;
942
943     /* optional fields */
944     uint64_t i_base_data_offset;
945     uint32_t i_sample_description_index;
946     uint32_t i_default_sample_duration;
947     uint32_t i_default_sample_size;
948     uint32_t i_default_sample_flags;
949
950 } MP4_Box_data_tfhd_t;
951
952 #define MP4_TRUN_DATA_OFFSET         (1<<0)
953 #define MP4_TRUN_FIRST_FLAGS         (1<<2)
954 #define MP4_TRUN_SAMPLE_DURATION     (1<<8)
955 #define MP4_TRUN_SAMPLE_SIZE         (1<<9)
956 #define MP4_TRUN_SAMPLE_FLAGS        (1<<10)
957 #define MP4_TRUN_SAMPLE_TIME_OFFSET  (1<<11)
958 typedef struct MP4_descriptor_trun_sample_t
959 {
960     uint32_t i_duration;
961     uint32_t i_size;
962     uint32_t i_flags;
963     uint32_t i_composition_time_offset; /* version == 0 ? signed : unsigned */
964 } MP4_descriptor_trun_sample_t;
965
966 typedef struct MP4_Box_data_trun_s
967 {
968     uint8_t  i_version;
969     uint32_t i_flags;
970     uint32_t i_sample_count;
971
972     /* optional fields */
973     int32_t i_data_offset;
974     uint32_t i_first_sample_flags;
975
976     MP4_descriptor_trun_sample_t *p_samples;
977
978 } MP4_Box_data_trun_t;
979
980
981 typedef struct
982 {
983     char *psz_text;
984
985 } MP4_Box_data_0xa9xxx_t;
986
987 typedef struct
988 {
989     char *psz_text;
990
991 } MP4_Box_data_name_t;
992
993 typedef struct
994 {
995     uint32_t i_entry_count;
996     uint32_t *i_track_ID;
997
998 } MP4_Box_data_tref_generic_t;
999
1000 typedef struct
1001 {
1002     uint8_t  i_version;
1003     uint32_t i_flags;
1004
1005     uint8_t i_chapter;
1006     struct
1007     {
1008         char    *psz_name;
1009         int64_t  i_start;
1010     } chapter[256];
1011 } MP4_Box_data_chpl_t;
1012
1013 typedef struct
1014 {
1015     uint8_t i_version;
1016     uint8_t i_profile;
1017     uint8_t i_profile_compatibility;
1018     uint8_t i_level;
1019
1020     uint8_t i_reserved1;     /* 6 bits */
1021     uint8_t i_length_size;
1022
1023     uint8_t i_reserved2;    /* 3 bits */
1024     uint8_t  i_sps;
1025     uint16_t *i_sps_length;
1026     uint8_t  **sps;
1027
1028     uint8_t  i_pps;
1029     uint16_t *i_pps_length;
1030     uint8_t  **pps;
1031
1032     /* XXX: Hack raw avcC atom payload */
1033     int     i_avcC;
1034     uint8_t *p_avcC;
1035
1036 } MP4_Box_data_avcC_t;
1037
1038 typedef struct
1039 {
1040     uint8_t i_fscod;
1041     uint8_t i_bsid;
1042     uint8_t i_bsmod;
1043     uint8_t i_acmod;
1044     uint8_t i_lfeon;
1045     uint8_t i_bitrate_code;
1046
1047 } MP4_Box_data_dac3_t;
1048
1049 typedef struct
1050 {
1051     uint8_t i_profile_level;
1052
1053     int i_vc1;
1054     uint8_t *p_vc1;
1055
1056 } MP4_Box_data_dvc1_t;
1057
1058 typedef struct
1059 {
1060     uint16_t i_little_endian;
1061
1062 } MP4_Box_data_enda_t;
1063
1064 typedef struct
1065 {
1066     uint16_t i_genre;
1067
1068 } MP4_Box_data_gnre_t;
1069
1070 typedef struct
1071 {
1072     uint32_t i_track_number;
1073     uint32_t i_track_total;
1074
1075 } MP4_Box_data_trkn_t;
1076
1077 typedef struct
1078 {
1079     uint8_t  i_version;
1080     uint32_t i_flags;
1081
1082     uint16_t i_object_descriptor;
1083     uint8_t i_OD_profile_level;
1084     uint8_t i_scene_profile_level;
1085     uint8_t i_audio_profile_level;
1086     uint8_t i_visual_profile_level;
1087     uint8_t i_graphics_profile_level;
1088
1089 } MP4_Box_data_iods_t;
1090
1091 typedef struct
1092 {
1093     uint32_t i_horizontal_spacing;
1094     uint32_t i_vertical_spacing;
1095 } MP4_Box_data_pasp_t;
1096
1097 typedef struct
1098 {
1099     uint8_t  i_version;
1100     uint32_t i_flags;
1101
1102     uint64_t i_fragment_duration;
1103 } MP4_Box_data_mehd_t;
1104
1105 typedef struct
1106 {
1107     uint8_t  i_version;
1108     uint32_t i_flags;
1109
1110     uint32_t i_track_ID;
1111     uint32_t i_default_sample_description_index;
1112     uint32_t i_default_sample_duration;
1113     uint32_t i_default_sample_size;
1114     uint32_t i_default_sample_flags;
1115 } MP4_Box_data_trex_t;
1116
1117 typedef struct
1118 {
1119     uint8_t  i_version;
1120     uint32_t i_flags;
1121
1122     uint8_t *p_sample_table;
1123 } MP4_Box_data_sdtp_t;
1124
1125 typedef struct
1126 {
1127     uint8_t  i_version;
1128     uint32_t i_flags;
1129
1130     uint32_t i_size;
1131 } MP4_Box_data_mfro_t;
1132
1133 typedef struct
1134 {
1135     uint8_t  i_version;
1136     uint32_t i_flags;
1137
1138     uint32_t i_track_ID;
1139     uint32_t i_number_of_entries;
1140
1141     uint8_t i_length_size_of_traf_num;
1142     uint8_t i_length_size_of_trun_num;
1143     uint8_t i_length_size_of_sample_num;
1144
1145     uint32_t *p_time;
1146     uint32_t *p_moof_offset;
1147     uint8_t *p_traf_number;
1148     uint8_t *p_trun_number;
1149     uint8_t *p_sample_number;
1150 } MP4_Box_data_tfra_t;
1151
1152 typedef struct
1153 {
1154     uint64_t i_duration;
1155     uint32_t i_timescale;
1156     uint16_t i_track_ID;
1157     uint8_t  i_es_cat;
1158
1159     uint32_t FourCC;
1160     uint32_t Bitrate;
1161     uint32_t MaxWidth;
1162     uint32_t MaxHeight;
1163     uint32_t SamplingRate;
1164     uint32_t Channels;
1165     uint32_t BitsPerSample;
1166     uint32_t AudioTag;
1167     uint16_t nBlockAlign;
1168     uint8_t  cpd_len;
1169     uint8_t  *CodecPrivateData;
1170 } MP4_Box_data_stra_t;
1171
1172 typedef struct
1173 {
1174     uint32_t i_hvcC;
1175     uint8_t *p_hvcC;
1176 } MP4_Box_data_hvcC_t;
1177
1178 typedef struct
1179 {
1180     uint8_t *p_blob;
1181     uint32_t i_blob;
1182 } MP4_Box_data_data_t;
1183
1184 /*
1185 typedef struct MP4_Box_data__s
1186 {
1187     uint8_t  i_version;
1188     uint32_t i_flags;
1189
1190 } MP4_Box_data__t;
1191
1192 */
1193
1194 typedef union MP4_Box_data_s
1195 {
1196     MP4_Box_data_ftyp_t *p_ftyp;
1197     MP4_Box_data_mvhd_t *p_mvhd;
1198     MP4_Box_data_mfhd_t *p_mfhd;
1199     MP4_Box_data_sidx_t *p_sidx;
1200     MP4_Box_data_tfhd_t *p_tfhd;
1201     MP4_Box_data_trun_t *p_trun;
1202     MP4_Box_data_tkhd_t *p_tkhd;
1203     MP4_Box_data_mdhd_t *p_mdhd;
1204     MP4_Box_data_hdlr_t *p_hdlr;
1205     MP4_Box_data_vmhd_t *p_vmhd;
1206     MP4_Box_data_smhd_t *p_smhd;
1207     MP4_Box_data_hmhd_t *p_hmhd;
1208     MP4_Box_data_url_t  *p_url;
1209     MP4_Box_data_urn_t  *p_urn;
1210     MP4_Box_data_dref_t *p_dref;
1211     MP4_Box_data_stts_t *p_stts;
1212     MP4_Box_data_ctts_t *p_ctts;
1213     MP4_Box_data_stsd_t *p_stsd;
1214     MP4_Box_data_sample_vide_t *p_sample_vide;
1215     MP4_Box_data_sample_soun_t *p_sample_soun;
1216     MP4_Box_data_sample_text_t *p_sample_text;
1217     MP4_Box_data_sample_hint_t *p_sample_hint;
1218
1219     MP4_Box_data_esds_t *p_esds;
1220     MP4_Box_data_avcC_t *p_avcC;
1221     MP4_Box_data_dac3_t *p_dac3;
1222     MP4_Box_data_dvc1_t *p_dvc1;
1223     MP4_Box_data_enda_t *p_enda;
1224     MP4_Box_data_gnre_t *p_gnre;
1225     MP4_Box_data_trkn_t *p_trkn;
1226     MP4_Box_data_iods_t *p_iods;
1227     MP4_Box_data_pasp_t *p_pasp;
1228     MP4_Box_data_trex_t *p_trex;
1229     MP4_Box_data_mehd_t *p_mehd;
1230     MP4_Box_data_sdtp_t *p_sdtp;
1231
1232     MP4_Box_data_tfra_t *p_tfra;
1233     MP4_Box_data_mfro_t *p_mfro;
1234     MP4_Box_data_stra_t *p_stra;
1235
1236     MP4_Box_data_stsz_t *p_stsz;
1237     MP4_Box_data_stz2_t *p_stz2;
1238     MP4_Box_data_stsc_t *p_stsc;
1239     MP4_Box_data_co64_t *p_co64;
1240     MP4_Box_data_stss_t *p_stss;
1241     MP4_Box_data_stsh_t *p_stsh;
1242     MP4_Box_data_stdp_t *p_stdp;
1243     MP4_Box_data_padb_t *p_padb;
1244     MP4_Box_data_elst_t *p_elst;
1245     MP4_Box_data_cprt_t *p_cprt;
1246
1247     MP4_Box_data_dcom_t *p_dcom;
1248     MP4_Box_data_cmvd_t *p_cmvd;
1249     MP4_Box_data_cmov_t *p_cmov;
1250
1251     MP4_Box_data_moviehintinformation_rtp_t p_moviehintinformation_rtp;
1252
1253     MP4_Box_data_frma_t *p_frma;
1254     MP4_Box_data_skcr_t *p_skcr;
1255
1256     MP4_Box_data_rdrf_t *p_rdrf;
1257     MP4_Box_data_rmdr_t *p_rmdr;
1258     MP4_Box_data_rmqu_t *p_rmqu;
1259     MP4_Box_data_rmvc_t *p_rmvc;
1260
1261     MP4_Box_data_0xa9xxx_t *p_0xa9xxx;
1262     MP4_Box_data_chpl_t *p_chpl;
1263     MP4_Box_data_tref_generic_t *p_tref_generic;
1264     MP4_Box_data_name_t *p_name;
1265
1266     MP4_Box_data_tfrf_t *p_tfrf;
1267     MP4_Box_data_tfxd_t *p_tfxd;
1268     MP4_Box_data_hvcC_t *p_hvcC;
1269
1270     MP4_Box_data_data_t *p_data;
1271
1272     void                *p_payload; /* for unknow type */
1273 } MP4_Box_data_t;
1274
1275
1276
1277 /* the most basic structure */
1278 typedef struct MP4_Box_s
1279 {
1280     off_t        i_pos;      /* absolute position */
1281
1282     uint32_t     i_type;
1283     uint32_t     i_shortsize;
1284
1285     UUID_t       i_uuid;  /* Set if i_type == "uuid" */
1286
1287     uint64_t     i_size; /* always set so use it */
1288
1289     MP4_Box_data_t   data;   /* union of pointers on extended data depending
1290                                 on i_type (or i_usertype) */
1291
1292     struct MP4_Box_s *p_father; /* pointer on the father Box */
1293
1294     struct MP4_Box_s *p_first;  /* pointer on the first child Box */
1295     struct MP4_Box_s *p_last;
1296
1297     struct MP4_Box_s *p_next;   /* pointer on the next boxes at the same level */
1298
1299 } MP4_Box_t;
1300
1301 static inline size_t mp4_box_headersize( MP4_Box_t *p_box )
1302 {
1303     return 8
1304         + ( p_box->i_shortsize == 1 ? 8 : 0 )
1305         + ( p_box->i_type == ATOM_uuid ? 16 : 0 );
1306 }
1307
1308 #define MP4_GETX_PRIVATE(dst, code, size) do { \
1309     if( (i_read) >= (size) ) { dst = (code); p_peek += (size); } \
1310     else { dst = 0; }   \
1311     i_read -= (size);   \
1312   } while(0)
1313
1314 #define MP4_GET1BYTE( dst )  MP4_GETX_PRIVATE( dst, *p_peek, 1 )
1315 #define MP4_GET2BYTES( dst ) MP4_GETX_PRIVATE( dst, GetWBE(p_peek), 2 )
1316 #define MP4_GET3BYTES( dst ) MP4_GETX_PRIVATE( dst, Get24bBE(p_peek), 3 )
1317 #define MP4_GET4BYTES( dst ) MP4_GETX_PRIVATE( dst, GetDWBE(p_peek), 4 )
1318 #define MP4_GET8BYTES( dst ) MP4_GETX_PRIVATE( dst, GetQWBE(p_peek), 8 )
1319 #define MP4_GETFOURCC( dst ) MP4_GETX_PRIVATE( dst, \
1320                 VLC_FOURCC(p_peek[0],p_peek[1],p_peek[2],p_peek[3]), 4)
1321
1322 #define MP4_GETVERSIONFLAGS( p_void ) \
1323     MP4_GET1BYTE( p_void->i_version ); \
1324     MP4_GET3BYTES( p_void->i_flags )
1325
1326 #define MP4_GETSTRINGZ( p_str )         \
1327     if( (i_read > 0) && (p_peek[0]) )   \
1328     {       \
1329         const int __i_copy__ = strnlen( (char*)p_peek, i_read-1 );  \
1330         p_str = malloc( __i_copy__+1 );               \
1331         if( p_str ) \
1332         { \
1333              memcpy( p_str, p_peek, __i_copy__ ); \
1334              p_str[__i_copy__] = 0; \
1335         } \
1336         p_peek += __i_copy__ + 1;   \
1337         i_read -= __i_copy__ + 1;   \
1338     }       \
1339     else    \
1340     {       \
1341         p_str = NULL; \
1342     }
1343
1344 #define MP4_READBOX_ENTER( MP4_Box_data_TYPE_t ) \
1345     int64_t  i_read = p_box->i_size; \
1346     uint8_t *p_peek, *p_buff; \
1347     int i_actually_read; \
1348     if( !( p_peek = p_buff = malloc( i_read ) ) ) \
1349     { \
1350         return( 0 ); \
1351     } \
1352     i_actually_read = stream_Read( p_stream, p_peek, i_read ); \
1353     if( i_actually_read < 0 || (int64_t)i_actually_read < i_read )\
1354     { \
1355         msg_Warn( p_stream, "MP4_READBOX_ENTER: I got %i bytes, "\
1356         "but I requested %"PRId64"", i_actually_read, i_read );\
1357         free( p_buff ); \
1358         return( 0 ); \
1359     } \
1360     p_peek += mp4_box_headersize( p_box ); \
1361     i_read -= mp4_box_headersize( p_box ); \
1362     if( !( p_box->data.p_payload = calloc( 1, sizeof( MP4_Box_data_TYPE_t ) ) ) ) \
1363     { \
1364         free( p_buff ); \
1365         return( 0 ); \
1366     }
1367
1368 #define MP4_READBOX_EXIT( i_code ) \
1369     do \
1370     { \
1371         free( p_buff ); \
1372         if( i_read < 0 ) \
1373             msg_Warn( p_stream, "Not enough data" ); \
1374         return( i_code ); \
1375     } while (0)
1376
1377
1378 /* This macro is used when we want to printf the box type
1379  * APPLE annotation box is :
1380  *  either 0xA9 + 24-bit ASCII text string (and 0xA9 isn't printable)
1381  *  either 32-bit ASCII text string
1382  */
1383 #define MP4_BOX_TYPE_ASCII() ( ((char*)&p_box->i_type)[0] != (char)0xA9 )
1384
1385 static inline uint32_t Get24bBE( const uint8_t *p )
1386 {
1387     return( ( p[0] <<16 ) + ( p[1] <<8 ) + p[2] );
1388 }
1389
1390 static inline void GetUUID( UUID_t *p_uuid, const uint8_t *p_buff )
1391 {
1392     memcpy( p_uuid, p_buff, 16 );
1393 }
1394
1395 static inline int CmpUUID( const UUID_t *u1, const UUID_t *u2 )
1396 {
1397     return memcmp( u1, u2, 16 );
1398 }
1399
1400 static inline void CreateUUID( UUID_t *p_uuid, uint32_t i_fourcc )
1401 {
1402     /* made by 0xXXXXXXXX-0011-0010-8000-00aa00389b71
1403             where XXXXXXXX is the fourcc */
1404     /* FIXME implement this */
1405     (void)p_uuid;
1406     (void)i_fourcc;
1407 }
1408
1409 static const UUID_t TfrfBoxUUID = {
1410                 { 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
1411                   0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f } };
1412
1413 static const UUID_t TfxdBoxUUID = {
1414                 { 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
1415                   0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2 } };
1416
1417 static const UUID_t SmooBoxUUID = {
1418                 { 0xe1, 0xda, 0x72, 0xba, 0x24, 0xd7, 0x43, 0xc3,
1419                   0xa6, 0xa5, 0x1b, 0x57, 0x59, 0xa1, 0xa9, 0x2c } };
1420
1421 static const UUID_t StraBoxUUID = {
1422                 { 0xb0, 0x3e, 0xf7, 0x70, 0x33, 0xbd, 0x4b, 0xac,
1423                   0x96, 0xc7, 0xbf, 0x25, 0xf9, 0x7e, 0x24, 0x47 } };
1424
1425 MP4_Box_t *MP4_BoxGetSmooBox( stream_t * );
1426
1427 /*****************************************************************************
1428  * MP4_BoxGetNextChunk : Parse the entire moof box.
1429  *****************************************************************************
1430  *  The first box is a virtual box "root" and is the father of the boxes
1431  *  'moof' and, possibly, 'sidx'.
1432  *****************************************************************************/
1433 MP4_Box_t *MP4_BoxGetNextChunk( stream_t * );
1434
1435 /*****************************************************************************
1436  * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
1437  *****************************************************************************
1438  *  The first box is a virtual box "root" and is the father for all first
1439  *  level boxes
1440  *****************************************************************************/
1441 MP4_Box_t *MP4_BoxGetRoot( stream_t * );
1442
1443 /*****************************************************************************
1444  * MP4_FreeBox : free memory allocated after read with MP4_ReadBox
1445  *               or MP4_BoxGetRoot, this means also children boxes
1446  * XXX : all children have to be allocated by a malloc !! and
1447  *         p_box is freed
1448  *****************************************************************************/
1449 void MP4_BoxFree( stream_t *, MP4_Box_t *p_box );
1450
1451 /*****************************************************************************
1452  * MP4_DumpBoxStructure: print the structure of the p_box
1453  *****************************************************************************
1454  * Useful while debugging
1455  *****************************************************************************/
1456 void MP4_BoxDumpStructure( stream_t *p_input, MP4_Box_t *p_box );
1457
1458 /*****************************************************************************
1459  * MP4_BoxGet: find a box given a path relative to p_box
1460  *****************************************************************************
1461  * Path Format: . .. / as usual
1462  *              [number] to specifie box number ex: trak[12]
1463  *
1464  * ex: /moov/trak[12]
1465  *     ../mdia
1466  *****************************************************************************/
1467 MP4_Box_t *MP4_BoxGet( MP4_Box_t *p_box, const char *psz_fmt, ... );
1468
1469 /*****************************************************************************
1470  * MP4_BoxCount: find number of box given a path relative to p_box
1471  *****************************************************************************
1472  * Path Format: . .. / as usual
1473  *              [number] to specifie box number ex: trak[12]
1474  *
1475  * ex: /moov/trak
1476  *     ../mdia
1477  *****************************************************************************/
1478 int MP4_BoxCount( MP4_Box_t *p_box, const char *psz_fmt, ... );
1479
1480 /* Internal functions exposed for MKV demux */
1481 int MP4_ReadBoxCommon( stream_t *p_stream, MP4_Box_t *p_box );
1482 int MP4_ReadBoxContainerChildren( stream_t *p_stream, MP4_Box_t *p_container,
1483                                   uint32_t i_last_child );
1484 int MP4_ReadBox_sample_vide( stream_t *p_stream, MP4_Box_t *p_box );
1485 void MP4_FreeBox_sample_vide( MP4_Box_t *p_box );
1486
1487 #endif