* avi: clean up. Some standard file (ie with 1 RIFF chunk ) has OpenDML
[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.10 2003/04/27 13:55:51 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_AVIX         VLC_FOURCC('A','V','I','X')
50 #define AVIFOURCC_WAVE         VLC_FOURCC('W','A','V','E')
51 #define AVIFOURCC_INFO         VLC_FOURCC('I','N','F','O')
52
53 #define AVIFOURCC_avih         VLC_FOURCC('a','v','i','h')
54 #define AVIFOURCC_hdrl         VLC_FOURCC('h','d','r','l')
55 #define AVIFOURCC_movi         VLC_FOURCC('m','o','v','i')
56 #define AVIFOURCC_idx1         VLC_FOURCC('i','d','x','1')
57
58 #define AVIFOURCC_strl         VLC_FOURCC('s','t','r','l')
59 #define AVIFOURCC_strh         VLC_FOURCC('s','t','r','h')
60 #define AVIFOURCC_strf         VLC_FOURCC('s','t','r','f')
61 #define AVIFOURCC_strd         VLC_FOURCC('s','t','r','d')
62 #define AVIFOURCC_strn         VLC_FOURCC('s','t','r','n')
63 #define AVIFOURCC_indx         VLC_FOURCC('i','n','d','x')
64
65 #define AVIFOURCC_rec          VLC_FOURCC('r','e','c',' ')
66 #define AVIFOURCC_auds         VLC_FOURCC('a','u','d','s')
67 #define AVIFOURCC_vids         VLC_FOURCC('v','i','d','s')
68
69 #define AVIFOURCC_IARL         VLC_FOURCC('I','A','R','L')
70 #define AVIFOURCC_IART         VLC_FOURCC('I','A','R','T')
71 #define AVIFOURCC_ICMS         VLC_FOURCC('I','C','M','S')
72 #define AVIFOURCC_ICMT         VLC_FOURCC('I','C','M','T')
73 #define AVIFOURCC_ICOP         VLC_FOURCC('I','C','O','P')
74 #define AVIFOURCC_ICRD         VLC_FOURCC('I','C','R','D')
75 #define AVIFOURCC_ICRP         VLC_FOURCC('I','C','R','P')
76 #define AVIFOURCC_IDIM         VLC_FOURCC('I','D','I','M')
77 #define AVIFOURCC_IDPI         VLC_FOURCC('I','D','P','I')
78 #define AVIFOURCC_IENG         VLC_FOURCC('I','E','N','G')
79 #define AVIFOURCC_IGNR         VLC_FOURCC('I','G','N','R')
80 #define AVIFOURCC_IKEY         VLC_FOURCC('I','K','E','Y')
81 #define AVIFOURCC_ILGT         VLC_FOURCC('I','L','G','T')
82 #define AVIFOURCC_IMED         VLC_FOURCC('I','M','E','D')
83 #define AVIFOURCC_INAM         VLC_FOURCC('I','N','A','M')
84 #define AVIFOURCC_IPLT         VLC_FOURCC('I','P','L','T')
85 #define AVIFOURCC_IPRD         VLC_FOURCC('I','P','R','D')
86 #define AVIFOURCC_ISBJ         VLC_FOURCC('I','S','B','J')
87 #define AVIFOURCC_ISFT         VLC_FOURCC('I','S','F','T')
88 #define AVIFOURCC_ISHP         VLC_FOURCC('I','S','H','P')
89 #define AVIFOURCC_ISRC         VLC_FOURCC('I','S','R','C')
90 #define AVIFOURCC_ISRF         VLC_FOURCC('I','S','R','F')
91 #define AVIFOURCC_ITCH         VLC_FOURCC('I','T','C','H')
92 #define AVIFOURCC_ISMP         VLC_FOURCC('I','S','M','P')
93 #define AVIFOURCC_IDIT         VLC_FOURCC('I','D','I','T')
94
95     
96 #define AVITWOCC_wb            VLC_TWOCC('w','b')
97 #define AVITWOCC_db            VLC_TWOCC('d','b')
98 #define AVITWOCC_dc            VLC_TWOCC('d','c')
99 #define AVITWOCC_pc            VLC_TWOCC('p','c')
100     /* *** codex stuff ***  */
101
102     /* MPEG4 video */
103 #define FOURCC_DIVX         VLC_FOURCC('D','I','V','X')
104 #define FOURCC_divx         VLC_FOURCC('d','i','v','x')
105 #define FOURCC_DIV1         VLC_FOURCC('D','I','V','1')
106 #define FOURCC_div1         VLC_FOURCC('d','i','v','1')
107 #define FOURCC_MP4S         VLC_FOURCC('M','P','4','S')
108 #define FOURCC_mp4s         VLC_FOURCC('m','p','4','s')
109 #define FOURCC_M4S2         VLC_FOURCC('M','4','S','2')
110 #define FOURCC_m4s2         VLC_FOURCC('m','4','s','2')
111 #define FOURCC_xvid         VLC_FOURCC('x','v','i','d')
112 #define FOURCC_XVID         VLC_FOURCC('X','V','I','D')
113 #define FOURCC_XviD         VLC_FOURCC('X','v','i','D')
114 #define FOURCC_DX50         VLC_FOURCC('D','X','5','0')
115 #define FOURCC_mp4v         VLC_FOURCC('m','p','4','v')
116 #define FOURCC_4            VLC_FOURCC( 4,  0,  0,  0 )
117
118     /* MSMPEG4 v2 */
119 #define FOURCC_MPG4         VLC_FOURCC('M','P','G','4')
120 #define FOURCC_mpg4         VLC_FOURCC('m','p','g','4')
121 #define FOURCC_DIV2         VLC_FOURCC('D','I','V','2')
122 #define FOURCC_div2         VLC_FOURCC('d','i','v','2')
123 #define FOURCC_MP42         VLC_FOURCC('M','P','4','2')
124 #define FOURCC_mp42         VLC_FOURCC('m','p','4','2')
125
126     /* MSMPEG4 v3 / M$ mpeg4 v3 */
127 #define FOURCC_MPG3         VLC_FOURCC('M','P','G','3')
128 #define FOURCC_mpg3         VLC_FOURCC('m','p','g','3')
129 #define FOURCC_div3         VLC_FOURCC('d','i','v','3')
130 #define FOURCC_MP43         VLC_FOURCC('M','P','4','3')
131 #define FOURCC_mp43         VLC_FOURCC('m','p','4','3')
132
133     /* DivX 3.20 */
134 #define FOURCC_DIV3         VLC_FOURCC('D','I','V','3')
135 #define FOURCC_DIV4         VLC_FOURCC('D','I','V','4')
136 #define FOURCC_div4         VLC_FOURCC('d','i','v','4')
137 #define FOURCC_DIV5         VLC_FOURCC('D','I','V','5')
138 #define FOURCC_div5         VLC_FOURCC('d','i','v','5')
139 #define FOURCC_DIV6         VLC_FOURCC('D','I','V','6')
140 #define FOURCC_div6         VLC_FOURCC('d','i','v','6')
141
142     /* AngelPotion stuff */
143 #define FOURCC_AP41         VLC_FOURCC('A','P','4','1')
144
145     /* 3IVX */
146 #define FOURCC_3IV1         VLC_FOURCC('3','I','V','1')
147 #define FOURCC_3iv1         VLC_FOURCC('2','i','v','1')
148 #define FOURCC_3IV2         VLC_FOURCC('3','I','V','2')
149 #define FOURCC_3iv2         VLC_FOURCC('3','i','v','2')
150 #define FOURCC_3IVD         VLC_FOURCC('3','I','V','D')
151 #define FOURCC_3ivd         VLC_FOURCC('3','i','v','d')
152 #define FOURCC_3VID         VLC_FOURCC('3','V','I','D')
153 #define FOURCC_3vid         VLC_FOURCC('3','v','i','d')
154
155     /* H263 and H263i */
156 #define FOURCC_H263         VLC_FOURCC('H','2','6','3')
157 #define FOURCC_h263         VLC_FOURCC('h','2','6','3')
158 #define FOURCC_U263         VLC_FOURCC('U','2','6','3')
159 #define FOURCC_I263         VLC_FOURCC('I','2','6','3')
160 #define FOURCC_i263         VLC_FOURCC('i','2','6','3')
161
162 /* Sound formats */
163 #define WAVE_FORMAT_UNKNOWN         0x0000
164 #define WAVE_FORMAT_PCM             0x0001
165 #define WAVE_FORMAT_MPEG            0x0050
166 #define WAVE_FORMAT_MPEGLAYER3      0x0055
167 #define WAVE_FORMAT_A52             0x2000
168 #define WAVE_FORMAT_WMA1            0x0160
169 #define WAVE_FORMAT_WMA2            0x0161
170
171
172 #define AVI_CHUNK_COMMON            \
173     vlc_fourcc_t i_chunk_fourcc;    \
174     uint64_t i_chunk_size;          \
175     uint64_t i_chunk_pos;           \
176     union  avi_chunk_u *p_next;    \
177     union  avi_chunk_u *p_father;  \
178     union  avi_chunk_u *p_first;   \
179     union  avi_chunk_u *p_last;
180
181 #define AVI_CHUNK( p_chk ) (avi_chunk_t*)(p_chk)
182
183 typedef struct idx1_entry_s
184 {
185     vlc_fourcc_t i_fourcc;
186     uint32_t i_flags;
187     uint32_t i_pos;
188     uint32_t i_length;
189
190 } idx1_entry_t;
191 typedef struct avi_chunk_common_s
192 {
193     AVI_CHUNK_COMMON
194 } avi_chunk_common_t;
195
196 typedef struct avi_chunk_list_s
197 {
198     AVI_CHUNK_COMMON
199     vlc_fourcc_t i_type;
200 } avi_chunk_list_t;
201
202 typedef struct avi_chunk_idx1_s
203 {
204     AVI_CHUNK_COMMON
205     unsigned int i_entry_count;
206     unsigned int i_entry_max;
207     idx1_entry_t *entry;
208
209 } avi_chunk_idx1_t;
210
211 typedef struct avi_chunk_avih_s
212 {
213     AVI_CHUNK_COMMON
214     uint32_t i_microsecperframe;
215     uint32_t i_maxbytespersec;
216     uint32_t i_reserved1; /* dwPaddingGranularity;    pad to multiples of this
217                              size; normally 2K */
218     uint32_t i_flags;
219     uint32_t i_totalframes;
220     uint32_t i_initialframes;
221     uint32_t i_streams;
222     uint32_t i_suggestedbuffersize;
223     uint32_t i_width;
224     uint32_t i_height;
225     uint32_t i_scale;
226     uint32_t i_rate;
227     uint32_t i_start;
228     uint32_t i_length;
229 } avi_chunk_avih_t;
230
231 typedef struct avi_chunk_strh_s
232 {
233     AVI_CHUNK_COMMON
234     vlc_fourcc_t i_type;
235     uint32_t i_handler;
236     uint32_t i_flags;
237     uint32_t i_reserved1;    /* wPriority wLanguage */
238     uint32_t i_initialframes;
239     uint32_t i_scale;
240     uint32_t i_rate;
241     uint32_t i_start;
242     uint32_t i_length;       /* In units above... */
243     uint32_t i_suggestedbuffersize;
244     uint32_t i_quality;
245     uint32_t i_samplesize;
246 } avi_chunk_strh_t;
247
248 typedef struct avi_chunk_strf_auds_s
249 {
250     AVI_CHUNK_COMMON
251     int             i_cat;
252     WAVEFORMATEX    *p_wf;
253 } avi_chunk_strf_auds_t;
254
255 typedef struct avi_chunk_strf_vids_s
256 {
257     AVI_CHUNK_COMMON
258     int              i_cat;
259     BITMAPINFOHEADER *p_bih;
260 } avi_chunk_strf_vids_t;
261
262 typedef union avi_chunk_strf_u
263 {
264     avi_chunk_strf_auds_t   auds;
265     avi_chunk_strf_vids_t   vids;
266     struct
267     {
268         AVI_CHUNK_COMMON
269         int i_cat;
270     }                       common;
271 } avi_chunk_strf_t;
272
273 typedef struct avi_chunk_strd_s
274 {
275     AVI_CHUNK_COMMON
276     uint8_t  *p_data;
277 } avi_chunk_strd_t;
278
279
280 #define AVI_INDEX_OF_INDEXES    0x00
281 #define AVI_INDEX_OF_CHUNKS     0x01
282 #define AVI_INDEX_IS_DATA       0x80
283
284 #define AVI_INDEX_2FIELD        0x01
285 typedef struct
286 {
287     uint32_t i_offset;
288     uint32_t i_size;
289 } indx_std_entry_t;
290
291 typedef struct
292 {
293     uint32_t i_offset;
294     uint32_t i_size;
295     uint32_t i_offsetfield2;
296 } indx_field_entry_t;
297
298 typedef struct
299 {
300     uint64_t i_offset;
301     uint32_t i_size;
302     uint32_t i_duration;
303 } indx_super_entry_t;
304
305 typedef struct avi_chunk_indx_s
306 {
307     AVI_CHUNK_COMMON
308     int16_t i_longsperentry;
309     int8_t  i_indexsubtype;
310     int8_t  i_indextype;
311     int32_t i_entriesinuse;
312     vlc_fourcc_t i_id;
313
314     int64_t i_baseoffset;
315
316     union
317     {
318         indx_std_entry_t    *std;
319         indx_field_entry_t  *field;
320         indx_super_entry_t  *super;
321     } idx;
322 } avi_chunk_indx_t;
323
324 typedef struct avi_chunk_STRING_s
325 {
326     AVI_CHUNK_COMMON
327     char *p_type;
328     char *p_str;
329 } avi_chunk_STRING_t;
330
331 typedef union avi_chunk_u
332 {
333     avi_chunk_common_t  common;
334     avi_chunk_list_t    list;
335     avi_chunk_idx1_t    idx1;
336     avi_chunk_avih_t    avih;
337     avi_chunk_strh_t    strh;
338     avi_chunk_strf_t    strf;
339     avi_chunk_strd_t    strd;
340     avi_chunk_indx_t    indx;
341     avi_chunk_STRING_t  strz;
342 } avi_chunk_t;
343
344 /****************************************************************************
345  * AVI_TestFile : test file header to see if it's an avi file
346  ****************************************************************************/
347 int     AVI_TestFile( input_thread_t *p_input );
348
349 /****************************************************************************
350  * Stream(input) access functions
351  ****************************************************************************/
352 off_t   AVI_TellAbsolute( input_thread_t *p_input );
353 int     AVI_SeekAbsolute( input_thread_t *p_input, off_t i_pos);
354 int     AVI_ReadData( input_thread_t *p_input, uint8_t *p_buff, int i_size );
355 int     AVI_SkipBytes( input_thread_t *p_input, int64_t i_count );
356
357 int     _AVI_ChunkRead( input_thread_t *p_input,
358                         avi_chunk_t *p_chk,
359                         avi_chunk_t *p_father,
360                         vlc_bool_t b_seekable );
361 void    _AVI_ChunkFree( input_thread_t *p_input,
362                          avi_chunk_t *p_chk );
363 int     _AVI_ChunkGoto( input_thread_t *p_input,
364                         avi_chunk_t *p_chk );
365 void    _AVI_ChunkDumpDebug( input_thread_t *p_input,
366                              avi_chunk_t  *p_chk );
367
368 int     _AVI_ChunkCount( avi_chunk_t *, vlc_fourcc_t );
369 avi_chunk_t *_AVI_ChunkFind( avi_chunk_t *, vlc_fourcc_t, int );
370
371 int     AVI_ChunkReadRoot( input_thread_t *p_input,
372                            avi_chunk_t *p_root,
373                            vlc_bool_t b_seekable );
374 void    AVI_ChunkFreeRoot( input_thread_t *p_input,
375                            avi_chunk_t  *p_chk );
376 #define AVI_ChunkRead( p_input, p_chk, p_father, b_seekable ) \
377     _AVI_ChunkRead( p_input, \
378                     (avi_chunk_t*)p_chk, \
379                     (avi_chunk_t*)p_father, \
380                     b_seekable )
381 #define AVI_ChunkFree( p_input, p_chk ) \
382     _AVI_ChunkFree( p_input, (avi_chunk_t*)p_chk )
383     
384 #define AVI_ChunkGoto( p_input, p_chk ) \
385     _AVI_ChunkGoto( p_input, (avi_chunk_t*)p_chk )
386     
387 #define AVI_ChunkDumpDebug( p_input, p_chk ) \
388     _AVI_ChunkDumpDebug( p_input, (avi_chunk_t*)p_chk )
389
390
391 #define AVI_ChunkCount( p_chk, i_fourcc ) \
392     _AVI_ChunkCount( (avi_chunk_t*)p_chk, i_fourcc )
393 #define AVI_ChunkFind( p_chk, i_fourcc, i_number ) \
394     _AVI_ChunkFind( (avi_chunk_t*)p_chk, i_fourcc, i_number )
395
396
397