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