]> git.sesse.net Git - mlt/blob - src/modules/kino/avi.h
Initial version
[mlt] / src / modules / kino / avi.h
1 /*
2 * avi.h library for AVI file format i/o
3 * Copyright (C) 2000 - 2002 Arne Schirmacher <arne@schirmacher.de>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 * Tag: $Name$
20 *
21 * Change log:
22
23 * $Log$
24 * Revision 1.1  2005/04/15 14:28:26  lilo_booter
25 * Initial version
26 *
27 * Revision 1.16  2005/04/01 23:43:10  ddennedy
28 * apply endian fixes from Daniel Kobras
29 *
30 * Revision 1.15  2004/10/11 01:37:11  ddennedy
31 * mutex safety locks in RIFF and AVI classes, type 2 AVI optimization, mencoder export script
32 *
33 * Revision 1.14  2003/11/25 23:00:52  ddennedy
34 * cleanup and a few bugfixes
35 *
36 * Revision 1.13  2003/10/21 16:34:32  ddennedy
37 * GNOME2 port phase 1: initial checkin
38 *
39 * Revision 1.11.2.5  2003/07/24 14:13:57  ddennedy
40 * support for distinct audio stream in type2 AVI and Quicktime; support for more DV FOURCCs
41 *
42 * Revision 1.11.2.4  2003/06/10 23:53:36  ddennedy
43 * Daniel Kobras' WriteFrame error handling and automatic OpenDML, bugfixes in scene list updates, export AV/C Record
44 *
45 * Revision 1.11.2.3  2003/02/20 21:59:57  ddennedy
46 * bugfixes to capture and AVI
47 *
48 * Revision 1.11.2.2  2003/01/13 05:15:31  ddennedy
49 * added More Info panel and supporting methods
50 *
51 * Revision 1.11.2.1  2002/11/25 04:48:31  ddennedy
52 * bugfix to report errors when loading files
53 *
54 * Revision 1.11  2002/10/08 07:46:41  ddennedy
55 * AVI bugfixes, compatibility, optimization, warn bad file in capture and export dv file, allow no mplex
56 *
57 * Revision 1.10  2002/05/17 08:04:25  ddennedy
58 * revert const-ness of Frame references in Frame, FileHandler, and AVI classes
59 *
60 * Revision 1.9  2002/05/15 04:39:35  ddennedy
61 * bugfixes to dv2 AVI write, audio export, Xv init
62 *
63 * Revision 1.8  2002/04/29 05:09:22  ddennedy
64 * raw dv file support, Frame::ExtractAudio uses libdv, audioScrub prefs
65 *
66 * Revision 1.7  2002/04/09 06:53:42  ddennedy
67 * cleanup, new libdv 0.9.5, large AVI, dnd storyboard
68 *
69 * Revision 1.7  2002/03/25 21:34:25  arne
70 * Support for large (64 bit) files mostly completed
71 *
72 * Revision 1.6  2002/03/10 13:29:41  arne
73 * more changes for 64 bit access
74 *
75 * Revision 1.5  2002/03/09 17:59:28  arne
76 * moved index routines to AVIFile
77 *
78 * Revision 1.4  2002/03/09 10:26:26  arne
79 * improved constructors and assignment operator
80 *
81 * Revision 1.3  2002/03/09 08:55:57  arne
82 * moved a few variables to AVIFile
83 *
84 * Revision 1.2  2002/03/04 19:22:43  arne
85 * updated to latest Kino avi code
86 *
87 * Revision 1.1.1.1  2002/03/03 19:08:08  arne
88 * import of version 1.01
89 *
90 */
91
92 /** Common AVI declarations
93  
94     Some of this comes from the public domain AVI specification, which
95     explains the microsoft-style definitions.
96  
97     \file avi.h
98 */
99
100 #ifndef _AVI_H
101 #define _AVI_H 1
102
103 #include "riff.h"
104
105 #define PACKED(x)       __attribute__((packed)) x
106
107 #define AVI_SMALL_INDEX (0x01)
108 #define AVI_LARGE_INDEX (0x02)
109 #define AVI_INDEX_OF_INDEXES (0x00)
110 #define AVI_INDEX_OF_CHUNKS (0x01)
111 #define AVI_INDEX_2FIELD (0x01)
112
113 enum { AVI_PAL, AVI_NTSC, AVI_AUDIO_48KHZ, AVI_AUDIO_44KHZ, AVI_AUDIO_32KHZ };
114
115 /** Declarations of the main AVI file header
116  
117     The contents of this struct goes into the 'avih' chunk.  */
118
119 typedef struct
120 {
121         /// frame display rate (or 0L)
122         DWORD dwMicroSecPerFrame;
123
124         /// max. transfer rate
125         DWORD dwMaxBytesPerSec;
126
127         /// pad to multiples of this size, normally 2K
128         DWORD dwPaddingGranularity;
129
130         /// the ever-present flags
131         DWORD dwFlags;
132
133         /// # frames in file
134         DWORD dwTotalFrames;
135         DWORD dwInitialFrames;
136         DWORD dwStreams;
137         DWORD dwSuggestedBufferSize;
138
139         DWORD dwWidth;
140         DWORD dwHeight;
141
142         DWORD dwReserved[ 4 ];
143 }
144 PACKED(MainAVIHeader);
145
146 typedef struct
147 {
148         WORD top, bottom, left, right;
149 }
150 PACKED(RECT);
151
152 /** Declaration of a stream header
153  
154     The contents of this struct goes into the 'strh' header. */
155
156 typedef struct
157 {
158         FOURCC fccType;
159         FOURCC fccHandler;
160         DWORD dwFlags;                /* Contains AVITF_* flags */
161         WORD wPriority;
162         WORD wLanguage;
163         DWORD dwInitialFrames;
164         DWORD dwScale;
165         DWORD dwRate;                 /* dwRate / dwScale == samples/second */
166         DWORD dwStart;
167         DWORD dwLength;               /* In units above... */
168         DWORD dwSuggestedBufferSize;
169         DWORD dwQuality;
170         DWORD dwSampleSize;
171         RECT rcFrame;
172 }
173 PACKED(AVIStreamHeader);
174
175 typedef struct
176 {
177         DWORD dwDVAAuxSrc;
178         DWORD dwDVAAuxCtl;
179         DWORD dwDVAAuxSrc1;
180         DWORD dwDVAAuxCtl1;
181         DWORD dwDVVAuxSrc;
182         DWORD dwDVVAuxCtl;
183         DWORD dwDVReserved[ 2 ];
184 }
185 PACKED(DVINFO);
186
187 typedef struct
188 {
189         DWORD biSize;
190         LONG biWidth;
191         LONG biHeight;
192         WORD biPlanes;
193         WORD biBitCount;
194         DWORD biCompression;
195         DWORD biSizeImage;
196         LONG biXPelsPerMeter;
197         LONG biYPelsPerMeter;
198         DWORD biClrUsed;
199         DWORD biClrImportant;
200 }
201 PACKED(BITMAPINFOHEADER);
202
203 typedef struct
204 {
205         WORD wFormatTag;
206         WORD nChannels;
207         DWORD nSamplesPerSec;
208         DWORD nAvgBytesPerSec;
209         WORD nBlockAlign;
210         WORD wBitsPerSample;
211         WORD cbSize;
212         WORD dummy;
213 }
214 PACKED(WAVEFORMATEX);
215
216 typedef struct
217 {
218         WORD wLongsPerEntry;
219         BYTE bIndexSubType;
220         BYTE bIndexType;
221         DWORD nEntriesInUse;
222         FOURCC dwChunkId;
223         DWORD dwReserved[ 3 ];
224         struct avisuperindex_entry
225         {
226                 QUADWORD qwOffset;
227                 DWORD dwSize;
228                 DWORD dwDuration;
229         }
230         aIndex[ 2014 ];
231 }
232 PACKED(AVISuperIndex);
233
234 typedef struct
235 {
236         WORD wLongsPerEntry;
237         BYTE bIndexSubType;
238         BYTE bIndexType;
239         DWORD nEntriesInUse;
240         FOURCC dwChunkId;
241         QUADWORD qwBaseOffset;
242         DWORD dwReserved;
243         struct avifieldindex_entry
244         {
245                 DWORD dwOffset;
246                 DWORD dwSize;
247         }
248         aIndex[ 4028 ];
249 }
250 PACKED(AVIStdIndex);
251
252 typedef struct
253 {
254         struct avisimpleindex_entry
255         {
256                 FOURCC  dwChunkId;
257                 DWORD   dwFlags;
258                 DWORD   dwOffset;
259                 DWORD   dwSize;
260         }
261         aIndex[ 20000 ];
262         DWORD   nEntriesInUse;
263 }
264 PACKED(AVISimpleIndex);
265
266 typedef struct
267 {
268         DWORD dirEntryType;
269         DWORD dirEntryName;
270         DWORD dirEntryLength;
271         size_t dirEntryOffset;
272         int dirEntryWrittenFlag;
273         int dirEntryParentList;
274 }
275 AviDirEntry;
276
277
278 /** base class for all AVI type files
279  
280     It contains methods and members which are the same in all AVI type files regardless of the particular compression, number
281     of streams etc. 
282  
283     The AVIFile class also contains methods for handling several indexes to the video frame content. */
284
285 class AVIFile : public RIFFFile
286 {
287 public:
288         AVIFile();
289         AVIFile( const AVIFile& );
290         virtual ~AVIFile();
291         virtual AVIFile& operator=( const AVIFile& );
292
293         virtual void Init( int format, int sampleFrequency, int indexType );
294         virtual int GetDVFrameInfo( off_t &offset, int &size, int frameNum );
295         virtual int GetFrameInfo( off_t &offset, int &size, int frameNum, FOURCC chunkID );
296         virtual int GetDVFrame( uint8_t *data, int frameNum );
297         virtual int getFrame( void *data, int frameNum, FOURCC chunkID );
298         virtual int GetTotalFrames() const;
299         virtual void PrintDirectoryEntryData( const RIFFDirEntry &entry ) const;
300         //virtual bool WriteFrame( const Frame &frame ) { return false; }
301         virtual void ParseList( int parent );
302         virtual void ParseRIFF( void );
303         virtual void ReadIndex( void );
304         virtual void WriteRIFF( void )
305         { }
306         virtual void FlushIndx( int stream );
307         virtual void UpdateIndx( int stream, int chunk, int duration );
308         virtual void UpdateIdx1( int chunk, int flags );
309         virtual bool verifyStreamFormat( FOURCC type );
310         virtual bool verifyStream( FOURCC type );
311         virtual bool isOpenDML( void );
312         virtual void setDVINFO( DVINFO& )
313         { }
314         virtual void setFccHandler( FOURCC type, FOURCC handler );
315         virtual bool getStreamFormat( void* data, FOURCC type );
316
317 protected:
318         MainAVIHeader mainHdr;
319         AVISimpleIndex *idx1;
320         int file_list;
321         int riff_list;
322         int hdrl_list;
323         int avih_chunk;
324         int movi_list;
325         int junk_chunk;
326         int idx1_chunk;
327
328         AVIStreamHeader streamHdr[ 2 ];
329         AVISuperIndex *indx[ 2 ];
330         AVIStdIndex *ix[ 2 ];
331         int indx_chunk[ 2 ];
332         int ix_chunk[ 2 ];
333         int strl_list[ 2 ];
334         int strh_chunk[ 2 ];
335         int strf_chunk[ 2 ];
336
337         int index_type;
338         int current_ix00;
339
340         DWORD dmlh[ 62 ];
341         int odml_list;
342         int dmlh_chunk;
343         bool isUpdateIdx1;
344
345 };
346
347
348 /** writing Type 1 DV AVIs
349  
350 */
351
352 class AVI1File : public AVIFile
353 {
354 public:
355         AVI1File();
356         virtual ~AVI1File();
357
358         virtual void Init( int format, int sampleFrequency, int indexType );
359         //virtual bool WriteFrame( const Frame &frame );
360         virtual void WriteRIFF( void );
361         virtual void setDVINFO( DVINFO& );
362
363 private:
364         DVINFO dvinfo;
365
366         AVI1File( const AVI1File& );
367         AVI1File& operator=( const AVI1File& );
368 };
369
370
371 /** writing Type 2 (separate audio data) DV AVIs
372  
373 This file type contains both audio and video tracks. It is therefore more compatible
374 to certain Windows programs, which expect any AVI having both audio and video tracks.
375 The video tracks contain the raw DV data (as in type 1) and the extracted audio tracks.
376  
377 Note that because the DV data contains audio information anyway, this means duplication
378 of data and a slight increase of file size.
379  
380 */
381
382 class AVI2File : public AVIFile
383 {
384 public:
385         AVI2File();
386         virtual ~AVI2File();
387
388         virtual void Init( int format, int sampleFrequency, int indexType );
389         //virtual bool WriteFrame( const Frame &frame );
390         virtual void WriteRIFF( void );
391         virtual void setDVINFO( DVINFO& );
392
393 private:
394         BITMAPINFOHEADER bitmapinfo;
395         WAVEFORMATEX waveformatex;
396
397         AVI2File( const AVI2File& );
398         AVI2File& operator=( const AVI2File& );
399 };
400 #endif