1 /* 2 // Copyright (C) 2022 Beken Corporation 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef AVILIB_H 16 #define AVILIB_H 17 #ifdef __cplusplus 18 extern "C" { 19 #endif 20 21 22 #include "ff.h" 23 24 typedef struct { 25 long pos; 26 long len; 27 long tot; 28 } video_index_entry; 29 30 typedef struct { 31 long pos; 32 long len; 33 long tot; 34 } audio_index_entry; 35 36 typedef struct { 37 FIL *fdes; /* File descriptor of AVI file */ 38 long mode; /* 0 for reading, 1 for writing */ 39 40 long width; /* Width of a video frame */ 41 long height; /* Height of a video frame */ 42 double fps; /* Frames per second */ 43 char compressor[8]; /* Type of compressor, 4 bytes + padding for 0 byte */ 44 long video_strn; /* Video stream number */ 45 long video_frames; /* Number of video frames */ 46 char video_tag[4]; /* Tag of video data */ 47 long video_pos; /* Number of next frame to be read 48 (if index present) */ 49 long video_posb; /* video position: byte within chunk */ 50 51 long a_fmt; /* Audio format, see #defines below */ 52 long a_chans; /* Audio channels, 0 for no audio */ 53 long a_rate; /* Rate in Hz */ 54 long a_bits; /* bits per audio sample */ 55 long audio_strn; /* Audio stream number */ 56 long audio_bytes; /* Total number of bytes of audio data */ 57 long audio_chunks; /* Chunks of audio data in the file */ 58 char audio_tag[4]; /* Tag of audio data */ 59 long audio_posc; /* Audio position: chunk */ 60 long audio_posb; /* Audio position: byte within chunk */ 61 62 long pos; /* position in file */ 63 long n_idx; /* number of index entries actually filled */ 64 long max_idx; /* number of index entries actually allocated */ 65 unsigned char (*idx)[16]; /* index entries (AVI idx1 tag) */ 66 video_index_entry *video_index; 67 audio_index_entry *audio_index; 68 long last_pos; /* Position of last frame written */ 69 long last_len; /* Length of last frame written */ 70 int must_use_index; /* Flag if frames are duplicated */ 71 long movi_start; 72 } avi_t; 73 74 #define AVI_MODE_WRITE 0 75 #define AVI_MODE_READ 1 76 77 /* The error codes delivered by avi_open_input_file */ 78 79 #define AVI_ERR_SIZELIM 1 /* The write of the data would exceed 80 the maximum size of the AVI file. 81 This is more a warning than an error 82 since the file may be closed safely */ 83 84 #define AVI_ERR_OPEN 2 /* Error opening the AVI file - wrong path 85 name or file nor readable/writable */ 86 87 #define AVI_ERR_READ 3 /* Error reading from AVI File */ 88 89 #define AVI_ERR_WRITE 4 /* Error writing to AVI File, 90 disk full ??? */ 91 92 #define AVI_ERR_WRITE_INDEX 5 /* Could not write index to AVI file 93 during close, file may still be 94 usable */ 95 96 #define AVI_ERR_CLOSE 6 /* Could not write header to AVI file 97 or not truncate the file during close, 98 file is most probably corrupted */ 99 100 #define AVI_ERR_NOT_PERM 7 /* Operation not permitted: 101 trying to read from a file open 102 for writing or vice versa */ 103 104 #define AVI_ERR_NO_MEM 8 /* malloc failed */ 105 106 #define AVI_ERR_NO_AVI 9 /* Not an AVI file */ 107 108 #define AVI_ERR_NO_HDRL 10 /* AVI file has no has no header list, 109 corrupted ??? */ 110 111 #define AVI_ERR_NO_MOVI 11 /* AVI file has no has no MOVI list, 112 corrupted ??? */ 113 114 #define AVI_ERR_NO_VIDS 12 /* AVI file contains no video data */ 115 116 #define AVI_ERR_NO_IDX 13 /* The file has been opened with 117 getIndex==0, but an operation has been 118 performed that needs an index */ 119 120 /* Possible Audio formats */ 121 #ifndef WAVE_FORMAT_UNKNOWN 122 /* Most of these are defined by Microsoft - don't redefine them */ 123 #define WAVE_FORMAT_UNKNOWN (0x0000) 124 #ifndef WAVE_FORMAT_PCM 125 #define WAVE_FORMAT_PCM (0x0001) 126 #endif 127 #define WAVE_FORMAT_ADPCM (0x0002) 128 #define WAVE_FORMAT_IBM_CVSD (0x0005) 129 #define WAVE_FORMAT_ALAW (0x0006) 130 #define WAVE_FORMAT_MULAW (0x0007) 131 #define WAVE_FORMAT_OKI_ADPCM (0x0010) 132 #define WAVE_FORMAT_DVI_ADPCM (0x0011) 133 #define WAVE_FORMAT_DIGISTD (0x0015) 134 #define WAVE_FORMAT_DIGIFIX (0x0016) 135 #define WAVE_FORMAT_YAMAHA_ADPCM (0x0020) 136 #define WAVE_FORMAT_DSP_TRUESPEECH (0x0022) 137 #define WAVE_FORMAT_GSM610 (0x0031) 138 #endif 139 #define IBM_FORMAT_MULAW (0x0101) 140 #define IBM_FORMAT_ALAW (0x0102) 141 #define IBM_FORMAT_ADPCM (0x0103) 142 143 144 void AVI_open_output_file(avi_t **avi_p, char *filename); 145 146 void AVI_set_video(avi_t *AVI, int width, int height, double fps, char *compressor); 147 148 void AVI_set_audio(avi_t *AVI, int channels, long rate, int bits, int format); 149 150 int AVI_write_frame(avi_t *AVI, char *data, long bytes); 151 152 int AVI_dup_frame(avi_t *AVI); 153 154 int AVI_write_audio(avi_t *AVI, char *data, long bytes); 155 156 long AVI_bytes_remain(avi_t *AVI); 157 158 int AVI_close(avi_t *AVI); 159 160 avi_t *AVI_open_input_file(const char *filename, int getIndex); 161 162 long AVI_video_frames(avi_t *AVI); 163 164 int AVI_video_width(avi_t *AVI); 165 166 int AVI_video_height(avi_t *AVI); 167 168 double AVI_video_frame_rate(avi_t *AVI); 169 170 char *AVI_video_compressor(avi_t *AVI); 171 172 int AVI_audio_channels(avi_t *AVI); 173 174 int AVI_audio_bits(avi_t *AVI); 175 176 int AVI_audio_format(avi_t *AVI); 177 178 long AVI_audio_rate(avi_t *AVI); 179 180 long AVI_audio_bytes(avi_t *AVI); 181 182 long AVI_frame_size(avi_t *AVI, long frame); 183 184 int AVI_seek_start(avi_t *AVI); 185 186 int AVI_set_video_position(avi_t *AVI, long frame, long *frame_len); 187 188 long AVI_read_frame(avi_t *AVI, char *vidbuf, long byte); 189 190 int AVI_set_audio_position(avi_t *AVI, long byte); 191 192 int AVI_set_audio_frame(avi_t *AVI, long frame, long *frame_len); 193 194 long AVI_read_audio(avi_t *AVI, char *audbuf, long bytes); 195 196 int AVI_read_data(avi_t *AVI, char *vidbuf, long max_vidbuf, char *audbuf, long max_audbuf, long *len); 197 198 #if 0 199 void AVI_print_error(char *str); 200 char *AVI_strerror(); 201 char *AVI_syserror(); 202 #endif 203 204 #ifdef __cplusplus 205 } 206 #endif 207 #endif 208