avilib.h 11 KB

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