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