avilib.h

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 #include <sys/types.h>
00029 #include <sys/stat.h>
00030 #include <stdio.h>
00031 #include <fcntl.h>
00032 
00033 //SLM
00034 #ifdef __CYGWIN__
00035 #include <sys/types.h>
00036 #elif defined WIN32
00037 #if defined __GNUWIN32__
00038 #include <stdint.h>
00039 #else
00040 #define uint32_t unsigned __int32
00041 #define uint8_t unsigned __int8
00042 #define uint16_t unsigned __int16
00043 #define uint64_t unsigned __int64
00044 #endif
00045 #else
00046 #include <unistd.h>
00047 #include <inttypes.h>
00048 #endif
00049 
00050 
00051 #include <limits.h>
00052 #include <stdlib.h>
00053 #include <string.h>
00054 #include <errno.h>
00055 
00056 #ifndef AVILIB_H
00057 #define AVILIB_H
00058 
00059 #define AVI_MAX_TRACKS 8
00060 
00061 typedef struct
00062 {
00063   off_t key;
00064   off_t pos;
00065   off_t len;
00066 } video_index_entry;
00067 
00068 typedef struct
00069 {
00070    off_t pos;
00071    off_t len;
00072    off_t tot;
00073 } audio_index_entry;
00074 
00075 typedef struct track_s
00076 {
00077 
00078     long   a_fmt;             /* Audio format, see #defines below */
00079     long   a_chans;           /* Audio channels, 0 for no audio */
00080     long   a_rate;            /* Rate in Hz */
00081     long   a_bits;            /* bits per audio sample */
00082     long   mp3rate;           /* mp3 bitrate kbs*/
00083 
00084     long   audio_strn;        /* Audio stream number */
00085     off_t  audio_bytes;       /* Total number of bytes of audio data */
00086     long   audio_chunks;      /* Chunks of audio data in the file */
00087 
00088     char   audio_tag[4];      /* Tag of audio data */
00089     long   audio_posc;        /* Audio position: chunk */
00090     long   audio_posb;        /* Audio position: byte within chunk */
00091 
00092     off_t a_codech_off;       /* absolut offset of audio codec information */
00093     off_t a_codecf_off;       /* absolut offset of audio codec information */
00094 
00095     audio_index_entry *audio_index;
00096 
00097 } track_t;
00098 
00099 typedef struct
00100 {
00101   uint32_t  bi_size;
00102   uint32_t  bi_width;
00103   uint32_t  bi_height;
00104   uint16_t  bi_planes;
00105   uint16_t  bi_bit_count;
00106   uint32_t  bi_compression;
00107   uint32_t  bi_size_image;
00108   uint32_t  bi_x_pels_per_meter;
00109   uint32_t  bi_y_pels_per_meter;
00110   uint32_t  bi_clr_used;
00111   uint32_t  bi_clr_important;
00112 } BITMAPINFOHEADER_avilib;
00113 
00114 typedef struct
00115 {
00116   uint16_t  w_format_tag;
00117   uint16_t  n_channels;
00118   uint32_t  n_samples_per_sec;
00119   uint32_t  n_avg_bytes_per_sec;
00120   uint16_t  n_block_align;
00121   uint16_t  w_bits_per_sample;
00122   uint16_t  cb_size;
00123 } WAVEFORMATEX_avilib;
00124 
00125 typedef struct
00126 {
00127   uint32_t fcc_type;
00128   uint32_t fcc_handler;
00129   uint32_t dw_flags;
00130   uint32_t dw_caps;
00131   uint16_t w_priority;
00132   uint16_t w_language;
00133   uint32_t dw_scale;
00134   uint32_t dw_rate;
00135   uint32_t dw_start;
00136   uint32_t dw_length;
00137   uint32_t dw_initial_frames;
00138   uint32_t dw_suggested_buffer_size;
00139   uint32_t dw_quality;
00140   uint32_t dw_sample_size;
00141   uint32_t dw_left;
00142   uint32_t dw_top;
00143   uint32_t dw_right;
00144   uint32_t dw_bottom;
00145   uint32_t dw_edit_count;
00146   uint32_t dw_format_change_count;
00147   char     sz_name[64];
00148 } AVISTREAMINFO;
00149 
00150 typedef struct
00151 {
00152 
00153   long   fdes;              /* File descriptor of AVI file */
00154   long   mode;              /* 0 for reading, 1 for writing */
00155 
00156   long   width;             /* Width  of a video frame */
00157   long   height;            /* Height of a video frame */
00158   double fps;               /* Frames per second */
00159   char   compressor[8];     /* Type of compressor, 4 bytes + padding for 0 byte */
00160   char   compressor2[8];     /* Type of compressor, 4 bytes + padding for 0 byte */
00161   long   video_strn;        /* Video stream number */
00162   long   video_frames;      /* Number of video frames */
00163   char   video_tag[4];      /* Tag of video data */
00164   long   video_pos;         /* Number of next frame to be read
00165                                (if index present) */
00166 
00167   unsigned long max_len;    /* maximum video chunk present */
00168 
00169   track_t track[AVI_MAX_TRACKS];  // up to AVI_MAX_TRACKS audio tracks supported
00170 
00171   off_t pos;        /* position in file */
00172   long   n_idx;             /* number of index entries actually filled */
00173   long   max_idx;           /* number of index entries actually allocated */
00174 
00175   off_t v_codech_off;       /* absolut offset of video codec (strh) info */
00176   off_t v_codecf_off;       /* absolut offset of video codec (strf) info */
00177 
00178   unsigned char (*idx)[16]; /* index entries (AVI idx1 tag) */
00179   video_index_entry *video_index;
00180 
00181   off_t last_pos;          /* Position of last frame written */
00182   unsigned long last_len;          /* Length of last frame written */
00183   int must_use_index;              /* Flag if frames are duplicated */
00184   off_t movi_start;
00185 
00186   int anum;            // total number of audio tracks
00187   int aptr;            // current audio working track
00188 
00189   BITMAPINFOHEADER_avilib *bitmap_info_header;
00190   WAVEFORMATEX_avilib *wave_format_ex[AVI_MAX_TRACKS];
00191 } avi_t;
00192 
00193 #define AVI_MODE_WRITE  0
00194 #define AVI_MODE_READ   1
00195 
00196 /* The error codes delivered by avi_open_input_file */
00197 
00198 #define AVI_ERR_SIZELIM      1     /* The write of the data would exceed
00199                                       the maximum size of the AVI file.
00200                                       This is more a warning than an error
00201                                       since the file may be closed safely */
00202 
00203 #define AVI_ERR_OPEN         2     /* Error opening the AVI file - wrong path
00204                                       name or file nor readable/writable */
00205 
00206 #define AVI_ERR_READ         3     /* Error reading from AVI File */
00207 
00208 #define AVI_ERR_WRITE        4     /* Error writing to AVI File,
00209                                       disk full ??? */
00210 
00211 #define AVI_ERR_WRITE_INDEX  5     /* Could not write index to AVI file
00212                                       during close, file may still be
00213                                       usable */
00214 
00215 #define AVI_ERR_CLOSE        6     /* Could not write header to AVI file
00216                                       or not truncate the file during close,
00217                                       file is most probably corrupted */
00218 
00219 #define AVI_ERR_NOT_PERM     7     /* Operation not permitted:
00220                                       trying to read from a file open
00221                                       for writing or vice versa */
00222 
00223 #define AVI_ERR_NO_MEM       8     /* malloc failed */
00224 
00225 #define AVI_ERR_NO_AVI       9     /* Not an AVI file */
00226 
00227 #define AVI_ERR_NO_HDRL     10     /* AVI file has no has no header list,
00228                                       corrupted ??? */
00229 
00230 #define AVI_ERR_NO_MOVI     11     /* AVI file has no has no MOVI list,
00231                                       corrupted ??? */
00232 
00233 #define AVI_ERR_NO_VIDS     12     /* AVI file contains no video data */
00234 
00235 #define AVI_ERR_NO_IDX      13     /* The file has been opened with
00236                                       getIndex==0, but an operation has been
00237                                       performed that needs an index */
00238 
00239 /* Possible Audio formats */
00240 
00241 #ifndef WAVE_FORMAT_PCM
00242 #define WAVE_FORMAT_UNKNOWN             (0x0000)
00243 #define WAVE_FORMAT_PCM                 (0x0001)
00244 #define WAVE_FORMAT_ADPCM               (0x0002)
00245 #define WAVE_FORMAT_IBM_CVSD            (0x0005)
00246 #define WAVE_FORMAT_ALAW                (0x0006)
00247 #define WAVE_FORMAT_MULAW               (0x0007)
00248 #define WAVE_FORMAT_OKI_ADPCM           (0x0010)
00249 #define WAVE_FORMAT_DVI_ADPCM           (0x0011)
00250 #define WAVE_FORMAT_DIGISTD             (0x0015)
00251 #define WAVE_FORMAT_DIGIFIX             (0x0016)
00252 #define WAVE_FORMAT_YAMAHA_ADPCM        (0x0020)
00253 #define WAVE_FORMAT_DSP_TRUESPEECH      (0x0022)
00254 #define WAVE_FORMAT_GSM610              (0x0031)
00255 #define IBM_FORMAT_MULAW                (0x0101)
00256 #define IBM_FORMAT_ALAW                 (0x0102)
00257 #define IBM_FORMAT_ADPCM                (0x0103)
00258 #endif
00259 
00260 avi_t* AVI_open_output_file(char * filename);
00261 void AVI_set_video(avi_t *AVI, int width, int height, double fps, char *compressor);
00262 void AVI_set_audio(avi_t *AVI, int channels, long rate, int bits, int format, long mp3rate);
00263 int  AVI_write_frame(avi_t *AVI, char *data, long bytes, int keyframe);
00264 int  AVI_dup_frame(avi_t *AVI);
00265 int  AVI_write_audio(avi_t *AVI, char *data, long bytes);
00266 int  AVI_append_audio(avi_t *AVI, char *data, long bytes);
00267 long AVI_bytes_remain(avi_t *AVI);
00268 int  AVI_close(avi_t *AVI);
00269 long AVI_bytes_written(avi_t *AVI);
00270 
00271 avi_t *AVI_open_input_file(const char *filename, int getIndex);
00272 avi_t *AVI_open_fd(int fd, int getIndex);
00273 int avi_parse_input_file(avi_t *AVI, int getIndex);
00274 long AVI_audio_mp3rate(avi_t *AVI);
00275 long AVI_video_frames(avi_t *AVI);
00276 int  AVI_video_width(avi_t *AVI);
00277 int  AVI_video_height(avi_t *AVI);
00278 double AVI_frame_rate(avi_t *AVI);
00279 char* AVI_video_compressor(avi_t *AVI);
00280 
00281 int  AVI_audio_channels(avi_t *AVI);
00282 int  AVI_audio_bits(avi_t *AVI);
00283 int  AVI_audio_format(avi_t *AVI);
00284 long AVI_audio_rate(avi_t *AVI);
00285 long AVI_audio_bytes(avi_t *AVI);
00286 long AVI_audio_chunks(avi_t *AVI);
00287 
00288 long AVI_max_video_chunk(avi_t *AVI);
00289 
00290 long AVI_frame_size(avi_t *AVI, long frame);
00291 long AVI_audio_size(avi_t *AVI, long frame);
00292 int  AVI_seek_start(avi_t *AVI);
00293 int  AVI_set_video_position(avi_t *AVI, long frame);
00294 long AVI_get_video_position(avi_t *AVI, long frame);
00295 long AVI_read_frame(avi_t *AVI, char *vidbuf, int *keyframe);
00296 
00297 int  AVI_set_audio_position(avi_t *AVI, long byte);
00298 int  AVI_set_audio_bitrate(avi_t *AVI, long bitrate);
00299 
00300 long AVI_read_audio(avi_t *AVI, char *audbuf, long bytes);
00301 long AVI_read_audio_chunk(avi_t *AVI, char *audbuf);
00302 
00303 long AVI_audio_codech_offset(avi_t *AVI);
00304 long AVI_audio_codecf_offset(avi_t *AVI);
00305 long AVI_video_codech_offset(avi_t *AVI);
00306 long AVI_video_codecf_offset(avi_t *AVI);
00307 
00308 int  AVI_read_data(avi_t *AVI, char *vidbuf, long max_vidbuf,
00309                                char *audbuf, long max_audbuf,
00310                                long *len);
00311 
00312 void AVI_print_error(char *str);
00313 char *AVI_strerror(void);
00314 char *AVI_syserror(void);
00315 
00316 int AVI_scan(char *name);
00317 int AVI_dump(char *name, int mode);
00318 
00319 char *AVI_codec2str(short cc);
00320 int AVI_file_check(char *import_file);
00321 
00322 void AVI_info(avi_t *avifile);
00323 uint64_t AVI_max_size(void);
00324 int avi_update_header(avi_t *AVI);
00325 
00326 int AVI_set_audio_track(avi_t *AVI, int track);
00327 int AVI_get_audio_track(avi_t *AVI);
00328 int AVI_audio_tracks(avi_t *AVI);
00329 
00330 
00331 struct riff_struct
00332 {
00333   unsigned char id[4];   /* RIFF */
00334   uint32_t len;
00335   unsigned char wave_id[4]; /* WAVE */
00336 };
00337 
00338 
00339 struct chunk_struct
00340 {
00341         unsigned char id[4];
00342         uint32_t len;
00343 };
00344 
00345 struct common_struct
00346 {
00347         uint16_t wFormatTag;
00348         uint16_t wChannels;
00349         uint32_t dwSamplesPerSec;
00350         uint32_t dwAvgBytesPerSec;
00351         uint16_t wBlockAlign;
00352         uint16_t wBitsPerSample;  /* Only for PCM */
00353 };
00354 
00355 struct wave_header
00356 {
00357         struct riff_struct   riff;
00358         struct chunk_struct  format;
00359         struct common_struct common;
00360         struct chunk_struct  data;
00361 };
00362 
00363 
00364 
00365 struct AVIStreamHeader {
00366   long  fccType;
00367   long  fccHandler;
00368   long  dwFlags;
00369   long  dwPriority;
00370   long  dwInitialFrames;
00371   long  dwScale;
00372   long  dwRate;
00373   long  dwStart;
00374   long  dwLength;
00375   long  dwSuggestedBufferSize;
00376   long  dwQuality;
00377   long  dwSampleSize;
00378 };
00379 
00380 #endif
Generated on Sun May 8 08:05:40 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3