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