avilib.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * avilib.h
  3. *
  4. * Copyright (C) Thomas �streich - June 2001
  5. * multiple audio track support Copyright (C) 2002 Thomas �streich
  6. *
  7. * Original code:
  8. * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
  9. *
  10. * This file is part of transcode, a linux video stream processing tool
  11. *
  12. * transcode is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * transcode is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with GNU Make; see the file COPYING. If not, write to
  24. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. */
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <stdio.h>
  30. #include <fcntl.h>
  31. #include <unistd.h>
  32. #include <inttypes.h>
  33. #include <limits.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <errno.h>
  37. #ifndef AVILIB_H
  38. #define AVILIB_H
  39. #define AVI_MAX_TRACKS 8
  40. typedef struct
  41. {
  42. unsigned long key;
  43. unsigned long pos;
  44. unsigned long len;
  45. } video_index_entry;
  46. typedef struct
  47. {
  48. unsigned long pos;
  49. unsigned long len;
  50. unsigned long tot;
  51. } audio_index_entry;
  52. typedef struct track_s
  53. {
  54. long a_fmt; /* Audio format, see #defines below */
  55. long a_chans; /* Audio channels, 0 for no audio */
  56. long a_rate; /* Rate in Hz */
  57. long a_bits; /* bits per audio sample */
  58. long mp3rate; /* mp3 bitrate kbs*/
  59. long audio_strn; /* Audio stream number */
  60. long audio_bytes; /* Total number of bytes of audio data */
  61. long audio_chunks; /* Chunks of audio data in the file */
  62. char audio_tag[4]; /* Tag of audio data */
  63. long audio_posc; /* Audio position: chunk */
  64. long audio_posb; /* Audio position: byte within chunk */
  65. long a_codech_off; /* absolut offset of audio codec information */
  66. long a_codecf_off; /* absolut offset of audio codec information */
  67. audio_index_entry *audio_index;
  68. } track_t;
  69. typedef struct
  70. {
  71. long fdes; /* File descriptor of AVI file */
  72. long mode; /* 0 for reading, 1 for writing */
  73. long width; /* Width of a video frame */
  74. long height; /* Height of a video frame */
  75. double fps; /* Frames per second */
  76. char compressor[8]; /* Type of compressor, 4 bytes + padding for 0 byte */
  77. char compressor2[8]; /* Type of compressor, 4 bytes + padding for 0 byte */
  78. long video_strn; /* Video stream number */
  79. long video_frames; /* Number of video frames */
  80. char video_tag[4]; /* Tag of video data */
  81. long video_pos; /* Number of next frame to be read
  82. (if index present) */
  83. unsigned long max_len; /* maximum video chunk present */
  84. track_t track[AVI_MAX_TRACKS]; // up to AVI_MAX_TRACKS audio tracks supported
  85. unsigned long pos; /* position in file */
  86. long n_idx; /* number of index entries actually filled */
  87. long max_idx; /* number of index entries actually allocated */
  88. long v_codech_off; /* absolut offset of video codec (strh) info */
  89. long v_codecf_off; /* absolut offset of video codec (strf) info */
  90. unsigned char (*idx)[16]; /* index entries (AVI idx1 tag) */
  91. video_index_entry *video_index;
  92. unsigned long last_pos; /* Position of last frame written */
  93. unsigned long last_len; /* Length of last frame written */
  94. int must_use_index; /* Flag if frames are duplicated */
  95. unsigned long movi_start;
  96. int anum; // total number of audio tracks
  97. int aptr; // current audio working track
  98. } avi_t;
  99. #define AVI_MODE_WRITE 0
  100. #define AVI_MODE_READ 1
  101. /* The error codes delivered by avi_open_input_file */
  102. #define AVI_ERR_SIZELIM 1 /* The write of the data would exceed
  103. the maximum size of the AVI file.
  104. This is more a warning than an error
  105. since the file may be closed safely */
  106. #define AVI_ERR_OPEN 2 /* Error opening the AVI file - wrong path
  107. name or file nor readable/writable */
  108. #define AVI_ERR_READ 3 /* Error reading from AVI File */
  109. #define AVI_ERR_WRITE 4 /* Error writing to AVI File,
  110. disk full ??? */
  111. #define AVI_ERR_WRITE_INDEX 5 /* Could not write index to AVI file
  112. during close, file may still be
  113. usable */
  114. #define AVI_ERR_CLOSE 6 /* Could not write header to AVI file
  115. or not truncate the file during close,
  116. file is most probably corrupted */
  117. #define AVI_ERR_NOT_PERM 7 /* Operation not permitted:
  118. trying to read from a file open
  119. for writing or vice versa */
  120. #define AVI_ERR_NO_MEM 8 /* malloc failed */
  121. #define AVI_ERR_NO_AVI 9 /* Not an AVI file */
  122. #define AVI_ERR_NO_HDRL 10 /* AVI file has no has no header list,
  123. corrupted ??? */
  124. #define AVI_ERR_NO_MOVI 11 /* AVI file has no has no MOVI list,
  125. corrupted ??? */
  126. #define AVI_ERR_NO_VIDS 12 /* AVI file contains no video data */
  127. #define AVI_ERR_NO_IDX 13 /* The file has been opened with
  128. getIndex==0, but an operation has been
  129. performed that needs an index */
  130. /* Possible Audio formats */
  131. #ifndef WAVE_FORMAT_PCM
  132. #define WAVE_FORMAT_UNKNOWN (0x0000)
  133. #define WAVE_FORMAT_PCM (0x0001)
  134. #define WAVE_FORMAT_ADPCM (0x0002)
  135. #define WAVE_FORMAT_IBM_CVSD (0x0005)
  136. #define WAVE_FORMAT_ALAW (0x0006)
  137. #define WAVE_FORMAT_MULAW (0x0007)
  138. #define WAVE_FORMAT_OKI_ADPCM (0x0010)
  139. #define WAVE_FORMAT_DVI_ADPCM (0x0011)
  140. #define WAVE_FORMAT_DIGISTD (0x0015)
  141. #define WAVE_FORMAT_DIGIFIX (0x0016)
  142. #define WAVE_FORMAT_YAMAHA_ADPCM (0x0020)
  143. #define WAVE_FORMAT_DSP_TRUESPEECH (0x0022)
  144. #define WAVE_FORMAT_GSM610 (0x0031)
  145. #define IBM_FORMAT_MULAW (0x0101)
  146. #define IBM_FORMAT_ALAW (0x0102)
  147. #define IBM_FORMAT_ADPCM (0x0103)
  148. #endif
  149. avi_t* AVI_open_output_file(char * filename);
  150. void AVI_set_video(avi_t *AVI, int width, int height, double fps, char *compressor);
  151. void AVI_set_audio(avi_t *AVI, int channels, long rate, int bits, int format, long mp3rate);
  152. int AVI_write_frame(avi_t *AVI, char *data, long bytes, int keyframe);
  153. int AVI_dup_frame(avi_t *AVI);
  154. int AVI_write_audio(avi_t *AVI, char *data, long bytes);
  155. int AVI_append_audio(avi_t *AVI, char *data, long bytes);
  156. long AVI_bytes_remain(avi_t *AVI);
  157. int AVI_close(avi_t *AVI);
  158. long AVI_bytes_written(avi_t *AVI);
  159. avi_t *AVI_open_input_file(char *filename, int getIndex);
  160. avi_t *AVI_open_fd(int fd, int getIndex);
  161. int avi_parse_input_file(avi_t *AVI, int getIndex);
  162. long AVI_audio_mp3rate(avi_t *AVI);
  163. long AVI_video_frames(avi_t *AVI);
  164. int AVI_video_width(avi_t *AVI);
  165. int AVI_video_height(avi_t *AVI);
  166. double AVI_frame_rate(avi_t *AVI);
  167. char* AVI_video_compressor(avi_t *AVI);
  168. int AVI_audio_channels(avi_t *AVI);
  169. int AVI_audio_bits(avi_t *AVI);
  170. int AVI_audio_format(avi_t *AVI);
  171. long AVI_audio_rate(avi_t *AVI);
  172. long AVI_audio_bytes(avi_t *AVI);
  173. long AVI_audio_chunks(avi_t *AVI);
  174. long AVI_max_video_chunk(avi_t *AVI);
  175. long AVI_frame_size(avi_t *AVI, long frame);
  176. long AVI_audio_size(avi_t *AVI, long frame);
  177. int AVI_seek_start(avi_t *AVI);
  178. int AVI_set_video_position(avi_t *AVI, long frame);
  179. long AVI_get_video_position(avi_t *AVI, long frame);
  180. long AVI_read_frame(avi_t *AVI, char *vidbuf, int *keyframe);
  181. int AVI_set_audio_position(avi_t *AVI, long byte);
  182. int AVI_set_audio_bitrate(avi_t *AVI, long bitrate);
  183. long AVI_read_audio(avi_t *AVI, char *audbuf, long bytes);
  184. long AVI_audio_codech_offset(avi_t *AVI);
  185. long AVI_audio_codecf_offset(avi_t *AVI);
  186. long AVI_video_codech_offset(avi_t *AVI);
  187. long AVI_video_codecf_offset(avi_t *AVI);
  188. int AVI_read_data(avi_t *AVI, char *vidbuf, long max_vidbuf,
  189. char *audbuf, long max_audbuf,
  190. long *len);
  191. void AVI_print_error(char *str);
  192. char *AVI_strerror();
  193. char *AVI_syserror();
  194. int AVI_scan(char *name);
  195. int AVI_dump(char *name, int mode);
  196. char *AVI_codec2str(short cc);
  197. int AVI_file_check(char *import_file);
  198. void AVI_info(avi_t *avifile);
  199. uint64_t AVI_max_size();
  200. int avi_update_header(avi_t *AVI);
  201. int AVI_set_audio_track(avi_t *AVI, int track);
  202. int AVI_get_audio_track(avi_t *AVI);
  203. int AVI_audio_tracks(avi_t *AVI);
  204. struct riff_struct
  205. {
  206. unsigned char id[4]; /* RIFF */
  207. unsigned long len;
  208. unsigned char wave_id[4]; /* WAVE */
  209. };
  210. struct chunk_struct
  211. {
  212. unsigned char id[4];
  213. unsigned long len;
  214. };
  215. struct common_struct
  216. {
  217. unsigned short wFormatTag;
  218. unsigned short wChannels;
  219. unsigned long dwSamplesPerSec;
  220. unsigned long dwAvgBytesPerSec;
  221. unsigned short wBlockAlign;
  222. unsigned short wBitsPerSample; /* Only for PCM */
  223. };
  224. struct wave_header
  225. {
  226. struct riff_struct riff;
  227. struct chunk_struct format;
  228. struct common_struct common;
  229. struct chunk_struct data;
  230. };
  231. struct AVIStreamHeader {
  232. long fccType;
  233. long fccHandler;
  234. long dwFlags;
  235. long dwPriority;
  236. long dwInitialFrames;
  237. long dwScale;
  238. long dwRate;
  239. long dwStart;
  240. long dwLength;
  241. long dwSuggestedBufferSize;
  242. long dwQuality;
  243. long dwSampleSize;
  244. };
  245. #endif