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