* all: Fix an other (and last ? ;) endian issue.
[vlc.git] / modules / demux / avi / libavi.h
1 /*****************************************************************************
2  * libavi.h : LibAVI library 
3  ******************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: libavi.h,v 1.5 2002/12/04 15:47:31 fenrir Exp $
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
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 General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 #include "codecs.h"                                      /* BITMAPINFOHEADER */
24
25 /* flags for use in <dwFlags> in AVIFileHdr */
26 #define AVIF_HASINDEX       0x00000010  /* Index at end of file? */
27 #define AVIF_MUSTUSEINDEX   0x00000020
28 #define AVIF_ISINTERLEAVED  0x00000100
29 #define AVIF_TRUSTCKTYPE    0x00000800  /* Use CKType to find key frames? */
30 #define AVIF_WASCAPTUREFILE 0x00010000
31 #define AVIF_COPYRIGHTED    0x00020000
32
33 /* Flags for index */
34 #define AVIIF_LIST          0x00000001L /* chunk is a 'LIST' */
35 #define AVIIF_KEYFRAME      0x00000010L /* this frame is a key frame.*/
36 #define AVIIF_NOTIME        0x00000100L /* this frame doesn't take any time */
37 #define AVIIF_COMPUSE       0x0FFF0000L /* these bits are for compressor use */
38
39 #define AVIIF_FIXKEYFRAME   0x00001000L /* invented; used to say that 
40                                            the keyframe flag isn't a true flag
41                                            but have to be verified */
42
43     /* *** avi stuff *** */
44
45 #define AVIFOURCC_RIFF         VLC_FOURCC('R','I','F','F')
46 #define AVIFOURCC_LIST         VLC_FOURCC('L','I','S','T')
47 #define AVIFOURCC_JUNK         VLC_FOURCC('J','U','N','K')
48 #define AVIFOURCC_AVI          VLC_FOURCC('A','V','I',' ')
49 #define AVIFOURCC_WAVE         VLC_FOURCC('W','A','V','E')
50 #define AVIFOURCC_INFO         VLC_FOURCC('I','N','F','O')
51
52 #define AVIFOURCC_avih         VLC_FOURCC('a','v','i','h')
53 #define AVIFOURCC_hdrl         VLC_FOURCC('h','d','r','l')
54 #define AVIFOURCC_movi         VLC_FOURCC('m','o','v','i')
55 #define AVIFOURCC_idx1         VLC_FOURCC('i','d','x','1')
56
57 #define AVIFOURCC_strl         VLC_FOURCC('s','t','r','l')
58 #define AVIFOURCC_strh         VLC_FOURCC('s','t','r','h')
59 #define AVIFOURCC_strf         VLC_FOURCC('s','t','r','f')
60 #define AVIFOURCC_strd         VLC_FOURCC('s','t','r','d')
61
62 #define AVIFOURCC_rec          VLC_FOURCC('r','e','c',' ')
63 #define AVIFOURCC_auds         VLC_FOURCC('a','u','d','s')
64 #define AVIFOURCC_vids         VLC_FOURCC('v','i','d','s')
65
66 #define AVIFOURCC_IARL         VLC_FOURCC('I','A','R','L')
67 #define AVIFOURCC_IART         VLC_FOURCC('I','A','R','T')
68 #define AVIFOURCC_ICMS         VLC_FOURCC('I','C','M','S')
69 #define AVIFOURCC_ICMT         VLC_FOURCC('I','C','M','T')
70 #define AVIFOURCC_ICOP         VLC_FOURCC('I','C','O','P')
71 #define AVIFOURCC_ICRD         VLC_FOURCC('I','C','R','D')
72 #define AVIFOURCC_ICRP         VLC_FOURCC('I','C','R','P')
73 #define AVIFOURCC_IDIM         VLC_FOURCC('I','D','I','M')
74 #define AVIFOURCC_IDPI         VLC_FOURCC('I','D','P','I')
75 #define AVIFOURCC_IENG         VLC_FOURCC('I','E','N','G')
76 #define AVIFOURCC_IGNR         VLC_FOURCC('I','G','N','R')
77 #define AVIFOURCC_IKEY         VLC_FOURCC('I','K','E','Y')
78 #define AVIFOURCC_ILGT         VLC_FOURCC('I','L','G','T')
79 #define AVIFOURCC_IMED         VLC_FOURCC('I','M','E','D')
80 #define AVIFOURCC_INAM         VLC_FOURCC('I','N','A','M')
81 #define AVIFOURCC_IPLT         VLC_FOURCC('I','P','L','T')
82 #define AVIFOURCC_IPRD         VLC_FOURCC('I','P','R','D')
83 #define AVIFOURCC_ISBJ         VLC_FOURCC('I','S','B','J')
84 #define AVIFOURCC_ISFT         VLC_FOURCC('I','S','F','T')
85 #define AVIFOURCC_ISHP         VLC_FOURCC('I','S','H','P')
86 #define AVIFOURCC_ISRC         VLC_FOURCC('I','S','R','C')
87 #define AVIFOURCC_ISRF         VLC_FOURCC('I','S','R','F')
88 #define AVIFOURCC_ITCH         VLC_FOURCC('I','T','C','H')
89 #define AVIFOURCC_ISMP         VLC_FOURCC('I','S','M','P')
90 #define AVIFOURCC_IDIT         VLC_FOURCC('I','D','I','T')
91
92     
93 #define AVITWOCC_wb            VLC_TWOCC('w','b')
94 #define AVITWOCC_db            VLC_TWOCC('d','b')
95 #define AVITWOCC_dc            VLC_TWOCC('d','c')
96 #define AVITWOCC_pc            VLC_TWOCC('p','c')
97     /* *** codex stuff ***  */
98
99     /* MPEG4 video */
100 #define FOURCC_DIVX         VLC_FOURCC('D','I','V','X')
101 #define FOURCC_divx         VLC_FOURCC('d','i','v','x')
102 #define FOURCC_DIV1         VLC_FOURCC('D','I','V','1')
103 #define FOURCC_div1         VLC_FOURCC('d','i','v','1')
104 #define FOURCC_MP4S         VLC_FOURCC('M','P','4','S')
105 #define FOURCC_mp4s         VLC_FOURCC('m','p','4','s')
106 #define FOURCC_M4S2         VLC_FOURCC('M','4','S','2')
107 #define FOURCC_m4s2         VLC_FOURCC('m','4','s','2')
108 #define FOURCC_xvid         VLC_FOURCC('x','v','i','d')
109 #define FOURCC_XVID         VLC_FOURCC('X','V','I','D')
110 #define FOURCC_XviD         VLC_FOURCC('X','v','i','D')
111 #define FOURCC_DX50         VLC_FOURCC('D','X','5','0')
112 #define FOURCC_mp4v         VLC_FOURCC('m','p','4','v')
113 #define FOURCC_4            VLC_FOURCC( 4,  0,  0,  0 )
114
115     /* MSMPEG4 v2 */
116 #define FOURCC_MPG4         VLC_FOURCC('M','P','G','4')
117 #define FOURCC_mpg4         VLC_FOURCC('m','p','g','4')
118 #define FOURCC_DIV2         VLC_FOURCC('D','I','V','2')
119 #define FOURCC_div2         VLC_FOURCC('d','i','v','2')
120 #define FOURCC_MP42         VLC_FOURCC('M','P','4','2')
121 #define FOURCC_mp42         VLC_FOURCC('m','p','4','2')
122
123     /* MSMPEG4 v3 / M$ mpeg4 v3 */
124 #define FOURCC_MPG3         VLC_FOURCC('M','P','G','3')
125 #define FOURCC_mpg3         VLC_FOURCC('m','p','g','3')
126 #define FOURCC_div3         VLC_FOURCC('d','i','v','3')
127 #define FOURCC_MP43         VLC_FOURCC('M','P','4','3')
128 #define FOURCC_mp43         VLC_FOURCC('m','p','4','3')
129
130     /* DivX 3.20 */
131 #define FOURCC_DIV3         VLC_FOURCC('D','I','V','3')
132 #define FOURCC_DIV4         VLC_FOURCC('D','I','V','4')
133 #define FOURCC_div4         VLC_FOURCC('d','i','v','4')
134 #define FOURCC_DIV5         VLC_FOURCC('D','I','V','5')
135 #define FOURCC_div5         VLC_FOURCC('d','i','v','5')
136 #define FOURCC_DIV6         VLC_FOURCC('D','I','V','6')
137 #define FOURCC_div6         VLC_FOURCC('d','i','v','6')
138
139     /* AngelPotion stuff */
140 #define FOURCC_AP41         VLC_FOURCC('A','P','4','1')
141
142     /* ?? */
143 #define FOURCC_3IV1         VLC_FOURCC('3','I','V','1')
144     /* H263 and H263i */
145 #define FOURCC_H263         VLC_FOURCC('H','2','6','3')
146 #define FOURCC_h263         VLC_FOURCC('h','2','6','3')
147 #define FOURCC_U263         VLC_FOURCC('U','2','6','3')
148 #define FOURCC_I263         VLC_FOURCC('I','2','6','3')
149 #define FOURCC_i263         VLC_FOURCC('i','2','6','3')
150
151 /* Sound formats */
152 #define WAVE_FORMAT_UNKNOWN         0x0000
153 #define WAVE_FORMAT_PCM             0x0001
154 #define WAVE_FORMAT_MPEG            0x0050
155 #define WAVE_FORMAT_MPEGLAYER3      0x0055
156 #define WAVE_FORMAT_A52             0x2000
157 #define WAVE_FORMAT_WMA1            0x0160
158 #define WAVE_FORMAT_WMA2            0x0161
159
160
161 #define AVI_CHUNK_COMMON            \
162     vlc_fourcc_t i_chunk_fourcc;    \
163     uint64_t i_chunk_size;          \
164     uint64_t i_chunk_pos;           \
165     union  avi_chunk_u *p_next;    \
166     union  avi_chunk_u *p_father;  \
167     union  avi_chunk_u *p_first;   \
168     union  avi_chunk_u *p_last;
169
170 #define AVI_CHUNK( p_chk ) (avi_chunk_t*)(p_chk)
171
172 typedef struct idx1_entry_s
173 {
174     vlc_fourcc_t i_fourcc;
175     uint32_t i_flags;
176     uint32_t i_pos;
177     uint32_t i_length;
178
179 } idx1_entry_t;
180 typedef struct avi_chunk_common_s
181 {
182     AVI_CHUNK_COMMON
183 } avi_chunk_common_t;
184
185 typedef struct avi_chunk_list_s
186 {
187     AVI_CHUNK_COMMON
188     vlc_fourcc_t i_type;
189 } avi_chunk_list_t;
190
191 typedef struct avi_chunk_idx1_s
192 {
193     AVI_CHUNK_COMMON
194     int i_entry_count;
195     int i_entry_max;
196     idx1_entry_t *entry;
197
198 } avi_chunk_idx1_t;
199
200 typedef struct avi_chunk_avih_s
201 {
202     AVI_CHUNK_COMMON
203     uint32_t i_microsecperframe;
204     uint32_t i_maxbytespersec;
205     uint32_t i_reserved1; /* dwPaddingGranularity;    pad to multiples of this
206                              size; normally 2K */
207     uint32_t i_flags;
208     uint32_t i_totalframes;
209     uint32_t i_initialframes;
210     uint32_t i_streams;
211     uint32_t i_suggestedbuffersize;
212     uint32_t i_width;
213     uint32_t i_height;
214     uint32_t i_scale;
215     uint32_t i_rate;
216     uint32_t i_start;
217     uint32_t i_length;
218 } avi_chunk_avih_t;
219
220 typedef struct avi_chunk_strh_s
221 {
222     AVI_CHUNK_COMMON
223     vlc_fourcc_t i_type;
224     uint32_t i_handler;
225     uint32_t i_flags;
226     uint32_t i_reserved1;    /* wPriority wLanguage */
227     uint32_t i_initialframes;
228     uint32_t i_scale;
229     uint32_t i_rate;
230     uint32_t i_start;
231     uint32_t i_length;       /* In units above... */
232     uint32_t i_suggestedbuffersize;
233     uint32_t i_quality;
234     uint32_t i_samplesize;
235 } avi_chunk_strh_t;
236
237 typedef struct avi_chunk_strf_auds_s
238 {
239     AVI_CHUNK_COMMON
240     WAVEFORMATEX    *p_wf;
241 } avi_chunk_strf_auds_t;
242
243 typedef struct avi_chunk_strf_vids_s
244 {
245     AVI_CHUNK_COMMON
246     BITMAPINFOHEADER *p_bih;
247 } avi_chunk_strf_vids_t;
248
249 typedef union avi_chunk_strf_u
250 {
251     avi_chunk_strf_auds_t   auds;
252     avi_chunk_strf_vids_t   vids;
253 } avi_chunk_strf_t;
254
255 typedef struct avi_chunk_strd_s
256 {
257     AVI_CHUNK_COMMON
258     uint8_t  *p_data;
259 } avi_chunk_strd_t;
260
261 typedef struct avi_chunk_STRING_s
262 {
263     AVI_CHUNK_COMMON
264     char *p_type;
265     char *p_str;
266 } avi_chunk_STRING_t;
267
268 typedef union avi_chunk_u
269 {
270     avi_chunk_common_t  common;
271     avi_chunk_list_t    list;
272     avi_chunk_idx1_t    idx1;
273     avi_chunk_avih_t    avih;
274     avi_chunk_strh_t    strh;
275     avi_chunk_strf_t    strf;
276     avi_chunk_strd_t    strd;
277     avi_chunk_STRING_t  strz;
278 } avi_chunk_t;
279
280 /****************************************************************************
281  * AVI_TestFile : test file header to see if it's an avi file
282  ****************************************************************************/
283 int     AVI_TestFile( input_thread_t *p_input );
284
285 /****************************************************************************
286  * Stream(input) access functions
287  ****************************************************************************/
288 off_t   AVI_TellAbsolute( input_thread_t *p_input );
289 int     AVI_SeekAbsolute( input_thread_t *p_input, off_t i_pos);
290 int     AVI_ReadData( input_thread_t *p_input, uint8_t *p_buff, int i_size );
291 int     AVI_SkipBytes( input_thread_t *p_input, int i_count );
292
293 int     _AVI_ChunkRead( input_thread_t *p_input,
294                         avi_chunk_t *p_chk,
295                         avi_chunk_t *p_father,
296                         vlc_bool_t b_seekable );
297 void    _AVI_ChunkFree( input_thread_t *p_input,
298                          avi_chunk_t *p_chk );
299 int     _AVI_ChunkGoto( input_thread_t *p_input,
300                         avi_chunk_t *p_chk );
301 void    _AVI_ChunkDumpDebug( input_thread_t *p_input,
302                              avi_chunk_t  *p_chk );
303
304 int     _AVI_ChunkCount( avi_chunk_t *, vlc_fourcc_t );
305 avi_chunk_t *_AVI_ChunkFind( avi_chunk_t *, vlc_fourcc_t, int );
306
307 int     AVI_ChunkReadRoot( input_thread_t *p_input,
308                            avi_chunk_t *p_root,
309                            vlc_bool_t b_seekable );
310 void    AVI_ChunkFreeRoot( input_thread_t *p_input,
311                            avi_chunk_t  *p_chk );
312 #define AVI_ChunkRead( p_input, p_chk, p_father, b_seekable ) \
313     _AVI_ChunkRead( p_input, \
314                     (avi_chunk_t*)p_chk, \
315                     (avi_chunk_t*)p_father, \
316                     b_seekable )
317 #define AVI_ChunkFree( p_input, p_chk ) \
318     _AVI_ChunkFree( p_input, (avi_chunk_t*)p_chk )
319     
320 #define AVI_ChunkGoto( p_input, p_chk ) \
321     _AVI_ChunkGoto( p_input, (avi_chunk_t*)p_chk )
322     
323 #define AVI_ChunkDumpDebug( p_input, p_chk ) \
324     _AVI_ChunkDumpDebug( p_input, (avi_chunk_t*)p_chk )
325
326
327 #define AVI_ChunkCount( p_chk, i_fourcc ) \
328     _AVI_ChunkCount( (avi_chunk_t*)p_chk, i_fourcc )
329 #define AVI_ChunkFind( p_chk, i_fourcc, i_number ) \
330     _AVI_ChunkFind( (avi_chunk_t*)p_chk, i_fourcc, i_number )
331
332
333