Wavelet and Image class library  1.3.2
avilib.h
Go to the documentation of this file.
00001 /*
00002  *  avilib.h
00003  *
00004  *  Copyright (C) Thomas Östreich - June 2001
00005  *  multiple audio track support Copyright (C) 2002 Thomas Östreich
00006  *
00007  *  Original code:
00008  *  Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de> 
00009  *
00010  *  This file is part of transcode, a linux video stream processing tool
00011  *      
00012  *  transcode is free software; you can redistribute it and/or modify
00013  *  it under the terms of the GNU General Public License as published by
00014  *  the Free Software Foundation; either version 2, or (at your option)
00015  *  any later version.
00016  *   
00017  *  transcode is distributed in the hope that it will be useful,
00018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  *  GNU General Public License for more details.
00021  *   
00022  *  You should have received a copy of the GNU General Public License
00023  *  along with GNU Make; see the file COPYING.  If not, write to
00024  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
00025  *
00026  */
00027 
00028 #if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(_MSC_VER) || defined(__WATCOMC__)
00029 # define SYS_WINDOWS
00030 # if defined __MINGW32__
00031 #  define COMP_MINGW
00032 # elif defined __CYGWIN__
00033 #  define COMP_CYGWIN
00034 # elif defined (_MSC_VER)
00035 #  define COMP_MSC
00036 # elif defined (__WATCOMC__)
00037 #  define COMP_WATCOM
00038 # else
00039 # error "Unknown Win32 compiler"
00040 # endif
00041 #elif defined(__APPLE__)
00042 # define SYS_APPLE
00043 # define COMP_GCC
00044 # define SYS_UNIX
00045 # define SYS_BSD
00046 #else
00047 # define COMP_GCC
00048 # define SYS_UNIX
00049 # if defined(__bsdi__) || defined(__FreeBSD__) 
00050 #  define SYS_BSD
00051 # else
00052 #  define SYS_LINUX
00053 # endif
00054 #endif
00055 
00056 #ifdef COMP_MSC
00057 typedef __int64 int64_t;
00058 typedef __int32 int32_t;
00059 typedef __int16 int16_t;
00060 typedef __int8  int8_t;
00061 typedef unsigned __int64 uint64_t;
00062 typedef unsigned __int32 uint32_t;
00063 typedef unsigned __int16 uint16_t;
00064 typedef unsigned __int8  uint8_t;
00065 typedef long ssize_t;
00066 #define S_IRUSR _S_IREAD
00067 #define S_IWUSR _S_IWRITE
00068 #define S_IGRP  0x00
00069 #define S_IROTH 0x00
00070 #define strtoll avilib_strtoll
00071 #pragma warning (disable: 4244 4761)
00072 #define __attribute__(__packed__)
00073 #elif defined(COMP_WATCOM)
00074 #define __attribute__(__packed__)
00075 #if __WATCOMC__ < 1230
00076 typedef long ssize_t;
00077 typedef __int64 int64_t;
00078 typedef int int32_t;
00079 typedef short int16_t;
00080 typedef char int8_t;
00081 typedef unsigned __int64 uint64_t;
00082 typedef unsigned int uint32_t;
00083 typedef unsigned short uint16_t;
00084 typedef unsigned char uint8_t;
00085 #define strtoll strtol
00086 #endif
00087 #define strncasecmp strnicmp
00088 #define S_IGRP  0x00
00089 #endif
00090 
00091 #if defined(COMP_MSC) || defined(COMP_MINGW)
00092 #include <io.h>
00093 #define ftruncate _chsize
00094 #define strncasecmp _strnicmp
00095 #else 
00096 #include <unistd.h>
00097 #endif
00098 
00099 #include <sys/types.h>
00100 #include <sys/stat.h>
00101 #include <stdio.h>
00102 #include <fcntl.h>
00103 #include <stdint.h>
00104 
00105 #if !defined(COMP_MSC)
00106 #include <unistd.h>
00107 #endif
00108 
00109 #if defined(SYS_LINUX) || defined(SYS_APPLE)
00110 #include <stdint.h>
00111 #endif
00112 #include <limits.h>
00113 #if !(defined __WATCOMC__ && __WATCOMC__ > 1229)
00114 #include <stdlib.h>
00115 #endif
00116 #include <string.h>
00117 #include <errno.h>
00118 
00119 #ifndef AVILIB_H
00120 #define AVILIB_H
00121 
00122 #define AVI_MAX_TRACKS 8
00123 
00124 typedef struct
00125 {
00126   off_t key;
00127   off_t pos;
00128   off_t len;
00129 } video_index_entry;
00130 
00131 typedef struct
00132 {
00133    off_t pos;
00134    off_t len;
00135    off_t tot;
00136 } audio_index_entry;
00137 
00138 
00139 // Index types
00140 
00141 
00142 #define AVI_INDEX_OF_INDEXES 0x00             // when each entry in aIndex
00143                                               // array points to an index chunk
00144 #define AVI_INDEX_OF_CHUNKS  0x01             // when each entry in aIndex
00145                                               // array points to a chunk in the file
00146 #define AVI_INDEX_IS_DATA    0x80             // when each entry is aIndex is
00147                                               // really the data
00148 // bIndexSubtype codes for INDEX_OF_CHUNKS
00149 //
00150 #define AVI_INDEX_2FIELD     0x01             // when fields within frames
00151                                               // are also indexed
00152 
00153 
00154 
00155 typedef struct _avisuperindex_entry {
00156     uint64_t qwOffset;           // absolute file offset
00157     uint32_t dwSize;                  // size of index chunk at this offset
00158     uint32_t dwDuration;              // time span in stream ticks
00159 } avisuperindex_entry;
00160 
00161 typedef struct _avistdindex_entry {
00162     uint32_t dwOffset;                // qwBaseOffset + this is absolute file offset
00163     uint32_t dwSize;                  // bit 31 is set if this is NOT a keyframe
00164 } avistdindex_entry;
00165 
00166 // Standard index 
00167 typedef struct _avistdindex_chunk {
00168     char           fcc[4];                 // ix##
00169     uint32_t  dwSize;                 // size of this chunk
00170     uint16_t wLongsPerEntry;         // must be sizeof(aIndex[0])/sizeof(DWORD)
00171     uint8_t  bIndexSubType;          // must be 0
00172     uint8_t  bIndexType;             // must be AVI_INDEX_OF_CHUNKS
00173     uint32_t  nEntriesInUse;          //
00174     char           dwChunkId[4];           // '##dc' or '##db' or '##wb' etc..
00175     uint64_t qwBaseOffset;       // all dwOffsets in aIndex array are relative to this
00176     uint32_t  dwReserved3;            // must be 0
00177     avistdindex_entry *aIndex;
00178 } avistdindex_chunk;
00179     
00180 
00181 // Base Index Form 'indx'
00182 typedef struct _avisuperindex_chunk {
00183     char           fcc[4];
00184     uint32_t  dwSize;                 // size of this chunk
00185     uint16_t wLongsPerEntry;         // size of each entry in aIndex array (must be 8 for us)
00186     uint8_t  bIndexSubType;          // future use. must be 0
00187     uint8_t  bIndexType;             // one of AVI_INDEX_* codes
00188     uint32_t  nEntriesInUse;          // index of first unused member in aIndex array
00189     char           dwChunkId[4];           // fcc of what is indexed
00190     uint32_t  dwReserved[3];          // meaning differs for each index type/subtype.
00191                                            // 0 if unused
00192     avisuperindex_entry *aIndex;           // where are the ix## chunks
00193     avistdindex_chunk **stdindex;          // the ix## chunks itself (array)
00194 } avisuperindex_chunk;
00195     
00196 
00197 
00198 typedef struct track_s
00199 {
00200 
00201     long   a_fmt;             /* Audio format, see #defines below */
00202     long   a_chans;           /* Audio channels, 0 for no audio */
00203     long   a_rate;            /* Rate in Hz */
00204     long   a_bits;            /* bits per audio sample */
00205     long   mp3rate;           /* mp3 bitrate kbs*/
00206     long   a_vbr;             /* 0 == no Variable BitRate */
00207     long   padrate;           /* byte rate used for zero padding */
00208 
00209     long   audio_strn;        /* Audio stream number */
00210     off_t  audio_bytes;       /* Total number of bytes of audio data */
00211     long   audio_chunks;      /* Chunks of audio data in the file */
00212 
00213     char   audio_tag[4];      /* Tag of audio data */
00214     long   audio_posc;        /* Audio position: chunk */
00215     long   audio_posb;        /* Audio position: byte within chunk */
00216  
00217     off_t  a_codech_off;       /* absolut offset of audio codec information */ 
00218     off_t  a_codecf_off;       /* absolut offset of audio codec information */ 
00219 
00220     audio_index_entry *audio_index;
00221     avisuperindex_chunk *audio_superindex;
00222 
00223 } track_t;
00224 
00225 typedef struct
00226 {
00227   uint32_t  bi_size;
00228   uint32_t  bi_width;
00229   uint32_t  bi_height;
00230   uint16_t  bi_planes;
00231   uint16_t  bi_bit_count;
00232   uint32_t  bi_compression;
00233   uint32_t  bi_size_image;
00234   uint32_t  bi_x_pels_per_meter;
00235   uint32_t  bi_y_pels_per_meter;
00236   uint32_t  bi_clr_used;
00237   uint32_t  bi_clr_important;
00238 } alBITMAPINFOHEADER;
00239 
00240 typedef struct __attribute__((__packed__))
00241 {
00242   uint16_t  w_format_tag;
00243   uint16_t  n_channels;
00244   uint32_t  n_samples_per_sec;
00245   uint32_t  n_avg_bytes_per_sec;
00246   uint16_t  n_block_align;
00247   uint16_t  w_bits_per_sample;
00248   uint16_t  cb_size;
00249 } alWAVEFORMATEX;
00250 
00251 typedef struct __attribute__((__packed__))
00252 {
00253   uint32_t fcc_type; 
00254   uint32_t fcc_handler; 
00255   uint32_t dw_flags; 
00256   uint32_t dw_caps; 
00257   uint16_t w_priority;
00258   uint16_t w_language;
00259   uint32_t dw_scale;
00260   uint32_t dw_rate;
00261   uint32_t dw_start;
00262   uint32_t dw_length;
00263   uint32_t dw_initial_frames;
00264   uint32_t dw_suggested_buffer_size;
00265   uint32_t dw_quality;
00266   uint32_t dw_sample_size;
00267   uint32_t dw_left;
00268   uint32_t dw_top;
00269   uint32_t dw_right;
00270   uint32_t dw_bottom;
00271   uint32_t dw_edit_count;
00272   uint32_t dw_format_change_count;
00273   char     sz_name[64];
00274 } alAVISTREAMINFO;
00275 
00276 typedef struct
00277 {
00278   
00279   long   fdes;              /* File descriptor of AVI file */
00280   long   mode;              /* 0 for reading, 1 for writing */
00281   
00282   long   width;             /* Width  of a video frame */
00283   long   height;            /* Height of a video frame */
00284   double fps;               /* Frames per second */
00285   char   compressor[8];     /* Type of compressor, 4 bytes + padding for 0 byte */
00286   char   compressor2[8];     /* Type of compressor, 4 bytes + padding for 0 byte */
00287   long   video_strn;        /* Video stream number */
00288   long   video_frames;      /* Number of video frames */
00289   char   video_tag[4];      /* Tag of video data */
00290   long   video_pos;         /* Number of next frame to be read
00291                                (if index present) */
00292   
00293   uint32_t max_len;    /* maximum video chunk present */
00294   
00295   track_t track[AVI_MAX_TRACKS];  // up to AVI_MAX_TRACKS audio tracks supported
00296   
00297   off_t  pos;               /* position in file */
00298   long   n_idx;             /* number of index entries actually filled */
00299   long   max_idx;           /* number of index entries actually allocated */
00300   
00301   off_t  v_codech_off;      /* absolut offset of video codec (strh) info */ 
00302   off_t  v_codecf_off;      /* absolut offset of video codec (strf) info */ 
00303   
00304   uint8_t (*idx)[16]; /* index entries (AVI idx1 tag) */
00305 
00306   video_index_entry *video_index;
00307   avisuperindex_chunk *video_superindex;  /* index of indices */
00308   int is_opendml;           /* set to 1 if this is an odml file with multiple index chunks */
00309   
00310   off_t  last_pos;          /* Position of last frame written */
00311   uint32_t last_len;   /* Length of last frame written */
00312   int must_use_index;       /* Flag if frames are duplicated */
00313   off_t  movi_start;
00314   int total_frames;         /* total number of frames if dmlh is present */
00315   
00316   int anum;            // total number of audio tracks 
00317   int aptr;            // current audio working track 
00318   int comment_fd;      // Read avi header comments from this fd
00319   char *index_file;    // read the avi index from this file
00320   
00321   alBITMAPINFOHEADER *bitmap_info_header;
00322   alWAVEFORMATEX *wave_format_ex[AVI_MAX_TRACKS];
00323 } avi_t;
00324 
00325 #define AVI_MODE_WRITE  0
00326 #define AVI_MODE_READ   1
00327 
00328 /* The error codes delivered by avi_open_input_file */
00329 
00330 #define AVI_ERR_SIZELIM      1     /* The write of the data would exceed
00331                                       the maximum size of the AVI file.
00332                                       This is more a warning than an error
00333                                       since the file may be closed safely */
00334 
00335 #define AVI_ERR_OPEN         2     /* Error opening the AVI file - wrong path
00336                                       name or file nor readable/writable */
00337 
00338 #define AVI_ERR_READ         3     /* Error reading from AVI File */
00339 
00340 #define AVI_ERR_WRITE        4     /* Error writing to AVI File,
00341                                       disk full ??? */
00342 
00343 #define AVI_ERR_WRITE_INDEX  5     /* Could not write index to AVI file
00344                                       during close, file may still be
00345                                       usable */
00346 
00347 #define AVI_ERR_CLOSE        6     /* Could not write header to AVI file
00348                                       or not truncate the file during close,
00349                                       file is most probably corrupted */
00350 
00351 #define AVI_ERR_NOT_PERM     7     /* Operation not permitted:
00352                                       trying to read from a file open
00353                                       for writing or vice versa */
00354 
00355 #define AVI_ERR_NO_MEM       8     /* malloc failed */
00356 
00357 #define AVI_ERR_NO_AVI       9     /* Not an AVI file */
00358 
00359 #define AVI_ERR_NO_HDRL     10     /* AVI file has no has no header list,
00360                                       corrupted ??? */
00361 
00362 #define AVI_ERR_NO_MOVI     11     /* AVI file has no has no MOVI list,
00363                                       corrupted ??? */
00364 
00365 #define AVI_ERR_NO_VIDS     12     /* AVI file contains no video data */
00366 
00367 #define AVI_ERR_NO_IDX      13     /* The file has been opened with
00368                                       getIndex==0, but an operation has been
00369                                       performed that needs an index */
00370 
00371 /* Possible Audio formats */
00372 
00373 #ifndef WAVE_FORMAT_PCM
00374 #define WAVE_FORMAT_UNKNOWN             (0x0000)
00375 #define WAVE_FORMAT_PCM                 (0x0001)
00376 #define WAVE_FORMAT_ADPCM               (0x0002)
00377 #define WAVE_FORMAT_IBM_CVSD            (0x0005)
00378 #define WAVE_FORMAT_ALAW                (0x0006)
00379 #define WAVE_FORMAT_MULAW               (0x0007)
00380 #define WAVE_FORMAT_OKI_ADPCM           (0x0010)
00381 #define WAVE_FORMAT_DVI_ADPCM           (0x0011)
00382 #define WAVE_FORMAT_DIGISTD             (0x0015)
00383 #define WAVE_FORMAT_DIGIFIX             (0x0016)
00384 #define WAVE_FORMAT_YAMAHA_ADPCM        (0x0020)
00385 #define WAVE_FORMAT_DSP_TRUESPEECH      (0x0022)
00386 #define WAVE_FORMAT_GSM610              (0x0031)
00387 #define IBM_FORMAT_MULAW                (0x0101)
00388 #define IBM_FORMAT_ALAW                 (0x0102)
00389 #define IBM_FORMAT_ADPCM                (0x0103)
00390 #endif
00391 
00392 avi_t* AVI_open_output_file(char * filename);
00393 void AVI_set_video(avi_t *AVI, int width, int height, double fps, char *compressor);
00394 void AVI_set_audio(avi_t *AVI, int channels, long rate, int bits, int format, long mp3rate);
00395 int  AVI_write_frame(avi_t *AVI, char *data, long bytes, int keyframe);
00396 int  AVI_dup_frame(avi_t *AVI);
00397 int  AVI_write_audio(avi_t *AVI, char *data, long bytes);
00398 int  AVI_append_audio(avi_t *AVI, char *data, long bytes);
00399 long AVI_bytes_remain(avi_t *AVI);
00400 int  AVI_close(avi_t *AVI);
00401 long AVI_bytes_written(avi_t *AVI);
00402 
00403 avi_t *AVI_open_input_file(char *filename, int getIndex);
00404 avi_t *AVI_open_input_indexfile(char *filename, int getIndex, char *indexfile);
00405 avi_t *AVI_open_fd(int fd, int getIndex);
00406 avi_t *AVI_open_indexfd(int fd, int getIndex, char *indexfile);
00407 int avi_parse_input_file(avi_t *AVI, int getIndex);
00408 int avi_parse_index_from_file(avi_t *AVI, char *filename);
00409 long AVI_audio_mp3rate(avi_t *AVI);
00410 long AVI_audio_padrate(avi_t *AVI);
00411 long AVI_video_frames(avi_t *AVI);
00412 int  AVI_video_width(avi_t *AVI);
00413 int  AVI_video_height(avi_t *AVI);
00414 double AVI_frame_rate(avi_t *AVI);
00415 char* AVI_video_compressor(avi_t *AVI);
00416 
00417 int  AVI_audio_channels(avi_t *AVI);
00418 int  AVI_audio_bits(avi_t *AVI);
00419 int  AVI_audio_format(avi_t *AVI);
00420 long AVI_audio_rate(avi_t *AVI);
00421 long AVI_audio_bytes(avi_t *AVI);
00422 long AVI_audio_chunks(avi_t *AVI);
00423 int  AVI_can_read_audio(avi_t *AVI);
00424 
00425 long AVI_max_video_chunk(avi_t *AVI);
00426 
00427 long AVI_frame_size(avi_t *AVI, long frame);
00428 long AVI_audio_size(avi_t *AVI, long frame);
00429 int  AVI_seek_start(avi_t *AVI);
00430 int  AVI_set_video_position(avi_t *AVI, long frame);
00431 long AVI_get_video_position(avi_t *AVI, long frame);
00432 long AVI_read_frame(avi_t *AVI, char *vidbuf, int *keyframe);
00433 
00434 int  AVI_set_audio_position(avi_t *AVI, long byte);
00435 int  AVI_set_audio_bitrate(avi_t *AVI, long bitrate);
00436 
00437 long AVI_get_audio_position_index(avi_t *AVI);
00438 int  AVI_set_audio_position_index(avi_t *AVI, long indexpos);
00439 
00440 long AVI_read_audio(avi_t *AVI, char *audbuf, long bytes);
00441 long AVI_read_audio_chunk(avi_t *AVI, char *audbuf);
00442 
00443 long AVI_audio_codech_offset(avi_t *AVI);
00444 long AVI_audio_codecf_offset(avi_t *AVI);
00445 long AVI_video_codech_offset(avi_t *AVI);
00446 long AVI_video_codecf_offset(avi_t *AVI);
00447 
00448 int  AVI_read_data(avi_t *AVI, char *vidbuf, long max_vidbuf,
00449                                char *audbuf, long max_audbuf,
00450                                long *len);
00451 
00452 void AVI_print_error(char *str);
00453 char *AVI_strerror(void);
00454 char *AVI_syserror(void);
00455 
00456 int AVI_scan(char *name);
00457 int AVI_dump(char *name, int mode);
00458 
00459 char *AVI_codec2str(short cc);
00460 int AVI_file_check(char *import_file);
00461 
00462 void AVI_info(avi_t *avifile);
00463 uint64_t AVI_max_size(void);
00464 int avi_update_header(avi_t *AVI);
00465 
00466 int AVI_set_audio_track(avi_t *AVI, int track);
00467 int AVI_get_audio_track(avi_t *AVI);
00468 int AVI_audio_tracks(avi_t *AVI);
00469 
00470 void AVI_set_audio_vbr(avi_t *AVI, long is_vbr);
00471 long AVI_get_audio_vbr(avi_t *AVI);
00472 
00473 void AVI_set_comment_fd(avi_t *AVI, int fd);
00474 int  AVI_get_comment_fd(avi_t *AVI);
00475 
00476 struct riff_struct 
00477 {
00478   uint8_t id[4];   /* RIFF */
00479   uint32_t len;
00480   uint8_t wave_id[4]; /* WAVE */
00481 };
00482 
00483 
00484 struct chunk_struct 
00485 {
00486         uint8_t id[4];
00487         uint32_t len;
00488 };
00489 
00490 struct common_struct 
00491 {
00492         uint16_t wFormatTag;
00493         uint16_t wChannels;
00494         uint32_t dwSamplesPerSec;
00495         uint32_t dwAvgBytesPerSec;
00496         uint16_t wBlockAlign;
00497         uint16_t wBitsPerSample;  /* Only for PCM */
00498 };
00499 
00500 struct wave_header 
00501 {
00502         struct riff_struct   riff;
00503         struct chunk_struct  format;
00504         struct common_struct common;
00505         struct chunk_struct  data;
00506 };
00507 
00508 // Simple WAV IO
00509 int AVI_read_wave_header( int fd, struct wave_header * wave );
00510 int AVI_write_wave_header( int fd, const struct wave_header * wave );
00511 size_t AVI_read_wave_pcm_data( int fd, void * buffer, size_t buflen );
00512 size_t AVI_write_wave_pcm_data( int fd, const void * buffer, size_t buflen );
00513 
00514 
00515 struct AVIStreamHeader {
00516   long  fccType;
00517   long  fccHandler;
00518   long  dwFlags;
00519   long  dwPriority;
00520   long  dwInitialFrames;
00521   long  dwScale;
00522   long  dwRate;
00523   long  dwStart;
00524   long  dwLength;
00525   long  dwSuggestedBufferSize;
00526   long  dwQuality;
00527   long  dwSampleSize;
00528 };
00529 
00530 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines