comparison src/main.c @ 2:d00bc412900e

*: fix up lots of stuff and make the thing actually usable
author Paper <mrpapersonic@gmail.com>
date Sun, 24 Dec 2023 20:01:30 -0500
parents 0ea1ec2da443
children
comparison
equal deleted inserted replaced
1:db8b7e45ee55 2:d00bc412900e
3 #include <stdio.h> 3 #include <stdio.h>
4 #include <stdlib.h> 4 #include <stdlib.h>
5 #include <stdint.h> 5 #include <stdint.h>
6 #include "edl.h" 6 #include "edl.h"
7 7
8 int strings_amt = -1; 8 #define strnchr(haystack, needle, length) memchr(haystack, needle, strnlen(haystack, length))
9 9
10 /* CRC32 hash function */ 10 static uint32_t EDL_internal_crcn32b(const unsigned char* restrict message, size_t length) {
11 uint32_t crc32b(unsigned char* restrict message) { 11 uint32_t crc = 0xFFFFFFFF;
12 int32_t i, j; 12
13 uint32_t byte, crc, mask; 13 size_t i;
14 14 for (i = 0; i < length && message[i]; i++) {
15 crc = 0xFFFFFFFF; 15 crc = crc ^ message[i];
16 for (i = 0; message[i] != 0; i++) { 16 size_t j;
17 byte = message[i]; // Get next byte. 17 for (j = 0; j < 8; j++)
18 crc = crc ^ byte; 18 crc = (crc >> 1) ^ (0xEDB88320 & -(crc & 1));
19 for (j = 7; j >= 0; j--) { // Do eight times. 19 }
20 mask = -(crc & 1); 20
21 crc = (crc >> 1) ^ (0xEDB88320 & mask); 21 return ~crc;
22 }
23
24 /* This parses the first line of the EDL and
25 * creates an array with CRC32 hashes in
26 * the order of the strings.
27 */
28 static uint32_t* EDL_internal_parse_first_line(const char* data, size_t* restrict length) {
29 size_t hashes_size = 32, current_hash = 0;
30 uint32_t* hashes = malloc(hashes_size * sizeof(uint32_t));
31
32 size_t len_until_newline = strchr(data, '\n') - data, i, b;
33 for (i = 0, b = 0; i < len_until_newline; i++, b++) {
34 if (data[i] == ';') {
35 if (current_hash >= hashes_size)
36 hashes = realloc(hashes, (hashes_size *= 2) * sizeof(uint32_t));
37 hashes[current_hash++] = EDL_internal_crcn32b((const unsigned char*)&data[i - b], b);
38 b = -1; // ew
22 } 39 }
23 } 40 }
24 return ~crc; 41
25 } 42 *length = current_hash;
26 43
27 /* This parses the first line of the EDL and
28 creates an array with CRC32 hashes in
29 the order of the strings. */
30 uint32_t* parse_first_line(char* first_line) {
31 uint32_t* hashes = calloc(32, sizeof(uint32_t));
32 int i = 0;
33 char* token;
34 hashes[i++] = crc32b(strtok_r(first_line, ";", &token));
35 for (;;i++) {
36 char* token = strtok_r(NULL, ";", &token);
37 if (token == NULL)
38 break;
39 hashes[i] = crc32b(token);
40 }
41 strings_amt = i;
42 free(first_line);
43 return hashes; 44 return hashes;
44 } 45 }
45 46
46 /* -- Functions to extract different datatypes -- */ 47 /* -- Functions to extract different datatypes -- */
47 48
48 char* get_int(char* input, int* int_return) { 49 static size_t EDL_internal_append_offset(const char* input, size_t offset, size_t length) {
49 char* ptr_return; 50 size_t s = 0;
50 *int_return = strtol(input, &ptr_return, 10); 51
51 return ptr_return + 2; 52 s += strchr(&input[offset + s], ';') - &input[offset];
52 } 53 if (s + offset > length)
53 54 return s;
54 char* get_double(char* input, double* double_return) { 55
55 char* ptr_return; 56 s += strspn(&input[offset + s], ";\t ");
56 *double_return = strtod(input, &ptr_return); 57 if (s + offset > length)
57 return ptr_return + 2; 58 return s;
58 } 59
59 60 return s;
60 char* get_bool(char* input, bool* bool_return) { 61 }
61 switch (input[0]) { 62
62 case 'T': 63 static size_t EDL_internal_get_int(const char* input, size_t offset, size_t length, int* int_return) {
63 *bool_return = true; 64 if (offset > length)
64 return input + 6; 65 return 0;
65 case 'F': 66
66 *bool_return = false; 67 {
67 return input + 7; 68 char* ptr_return;
68 default: 69 *int_return = strtol(&input[offset], &ptr_return, 10);
69 /* what */ 70
70 return NULL; 71 if (!ptr_return)
71 } 72 return 0;
72 } 73 }
73 74
74 char* get_media_type(char* input, MediaType* media_return) { 75 return EDL_internal_append_offset(input, offset, length);
75 switch (input[0]) { 76 }
76 case 'V': 77
77 *media_return = video; 78 static size_t EDL_internal_get_double(const char* input, size_t offset, size_t length, double* double_return) {
78 return input + 7; 79 if (offset > length)
79 case 'A': 80 return 0;
80 *media_return = audio; 81
81 return input + 7; 82 {
82 default: 83 char* ptr_return;
83 /* what */ 84 *double_return = strtod(&input[offset], &ptr_return);
84 return NULL; 85
85 } 86 if (!ptr_return)
86 } 87 return 0;
87 88 }
88 /* lenptr is optional */ 89
89 char* cut_string_at_match(char* input, char* match, int* lenptr) { 90 return EDL_internal_append_offset(input, offset, length);
90 int length = strcspn(&input[1], match); 91 }
91 char* string = calloc(length+1, sizeof(char)); 92
92 memcpy(string, &input[1], length); 93 static size_t EDL_internal_get_bool(const char* input, size_t offset, size_t length, bool* bool_return) {
93 string[length] = '\0'; 94 if (offset > length)
94 if (lenptr != NULL) 95 return 0;
95 *lenptr = length; 96
96 return string; 97 if (strncmp((input + offset), "TRUE", 4))
97 } 98 *bool_return = true;
98 99 else if (strncmp((input + offset), "FALSE", 5))
99 char* get_string(char* input, char** string_return) { 100 *bool_return = false;
101
102 return EDL_internal_append_offset(input, offset, length);
103 }
104
105 static size_t EDL_internal_get_media_type(const char* input, size_t offset, size_t length, MediaType* media_return) {
106 if (offset > length)
107 return 0;
108
109 if (strncmp(input + offset, "VIDEO", 5))
110 *media_return = MEDIATYPE_VIDEO;
111 else if (strncmp(input + offset, "AUDIO", 5))
112 *media_return = MEDIATYPE_AUDIO;
113
114 return EDL_internal_append_offset(input, offset, length);
115 }
116
117 static size_t EDL_internal_get_string(const char* input, size_t offset, size_t length, char** string_return) {
100 /* Windows filenames will *NEVER* include double quotes. 118 /* Windows filenames will *NEVER* include double quotes.
101 This might break with EDL files created with Reaper on Linux. */ 119 This might break with EDL files created with Reaper on Linux. */
102 int length; 120 if (offset > length)
103 char* string = cut_string_at_match(input, "\"", &length); 121 return 0;
104 *string_return = string; 122
105 return input + length + 4; 123 size_t start = strchr(&input[offset], '\"') - &input[offset] + 1;
106 } 124 if (start + offset > length)
107 125 return 0;
108 /* The memory manager for EDL_file. */ 126
109 void allocate_memory(EDL_file* input) { 127 size_t s_length = strchr(&input[offset + start], '\"') - &input[offset] - start;
110 if (input->size == 0) input->size = 16; 128 if (start + s_length + offset > length)
111 input->edl = realloc(input->edl, (input->size*2) * sizeof(EDL_line)); 129 return 0;
112 input->size *= 2; 130
113 } 131 *string_return = malloc((s_length + 1) * sizeof(char));
114 132 if (!*string_return)
115 /* This is the important function */ 133 return 0;
116 EDL_file* parse_EDL(char* text) { 134
117 EDL_file edl; 135 memcpy(*string_return, &input[offset + start], s_length);
118 memset(&edl, '\0', sizeof(edl)); 136 (*string_return)[s_length] = '\0';
119 allocate_memory(&edl); 137
120 uint32_t* order = parse_first_line(strtok_r(text, "\n", NULL)); 138 return EDL_internal_append_offset(input, offset, length);
121 char* line = strtok(text, "\n"); 139 }
122 while ((line = strtok(NULL, "\n")) != NULL) { 140
123 char* tmp_ptr = line; 141 /* memory management routines */
124 for (int i = 0; i < strings_amt; i++) { 142
143 static int EDL_internal_reallocate(EDL_file* input, size_t new_capacity) {
144 input->arr = realloc(input->arr, new_capacity * sizeof(EDL_line));
145 if (!input->arr)
146 return 0;
147
148 if (new_capacity > input->capacity)
149 memset(input->arr + input->capacity, 0, new_capacity - input->capacity);
150
151 input->capacity = new_capacity;
152
153 return 1;
154 }
155
156 static int EDL_internal_allocate_memory(EDL_file* input) {
157 return EDL_internal_reallocate(input, input->capacity ? input->capacity * 2 : 16);
158 }
159
160 static int EDL_internal_shrink_memory_to_fit(EDL_file* input) {
161 return EDL_internal_reallocate(input, input->size);
162 }
163
164 /* the important function */
165 EDL_file EDL_parse(const char* data, size_t length) {
166 EDL_file edl = {0};
167 if (!EDL_internal_allocate_memory(&edl))
168 return edl;
169
170 size_t order_size = 0;
171 uint32_t* order = EDL_internal_parse_first_line(data, &order_size);
172
173 size_t offset = 0;
174 while ((offset = strnchr(data + offset + 1, '\n', length - offset) - (void*)data) < length) {
175 size_t local_offset = offset; // this is so our original offset stays intact
176
177 bool ok = true;
178
179 for (int i = 0; i < order_size; i++) {
180 #define ADD_TO_OFFSET(x, a) \
181 { \
182 size_t o = a(data, local_offset, length, &edl.arr[edl.size].x); \
183 if (!o) \
184 ok = false; \
185 local_offset += o; \
186 }
125 switch (order[i]) { 187 switch (order[i]) {
126 case 0x1541c503: /* ID */ 188 case 0x1541c503: /* ID */
127 tmp_ptr = get_int(tmp_ptr, &edl.edl[edl.current].id); 189 ADD_TO_OFFSET(id, EDL_internal_get_int);
128 break; 190 break;
129 case 0x4b211812: /* Track */ 191 case 0x4b211812: /* Track */
130 tmp_ptr = get_int(tmp_ptr, &edl.edl[edl.current].track); 192 ADD_TO_OFFSET(track, EDL_internal_get_int);
131 break; 193 break;
132 case 0xbb46516f: /* StartTime */ 194 case 0xbb46516f: /* StartTime */
133 tmp_ptr = get_double(tmp_ptr, &edl.edl[edl.current].start_time); 195 ADD_TO_OFFSET(start_time, EDL_internal_get_double);
134 break; 196 break;
135 case 0xaeac5df7: /* Length */ 197 case 0xaeac5df7: /* Length */
136 tmp_ptr = get_double(tmp_ptr, &edl.edl[edl.current].length); 198 ADD_TO_OFFSET(length, EDL_internal_get_double);
137 break; 199 break;
138 case 0x03834606: /* PlayRate */ 200 case 0x03834606: /* PlayRate */
139 tmp_ptr = get_double(tmp_ptr, &edl.edl[edl.current].play_rate); 201 ADD_TO_OFFSET(play_rate, EDL_internal_get_double);
140 break; 202 break;
141 case 0x0c2083d3: /* Locked */ 203 case 0x0c2083d3: /* Locked */
142 tmp_ptr = get_bool(tmp_ptr, &edl.edl[edl.current].locked); 204 ADD_TO_OFFSET(locked, EDL_internal_get_bool);
143 break; 205 break;
144 case 0xe60c8b1d: /* Normalized */ 206 case 0xe60c8b1d: /* Normalized */
145 tmp_ptr = get_bool(tmp_ptr, &edl.edl[edl.current].normalized); 207 ADD_TO_OFFSET(normalized, EDL_internal_get_bool);
146 break; 208 break;
147 case 0xbe5802c9: /* StretchMethod */ 209 case 0xbe5802c9: /* StretchMethod */
148 tmp_ptr = get_int(tmp_ptr, &edl.edl[edl.current].stretch_method); 210 ADD_TO_OFFSET(stretch_method, EDL_internal_get_int);
149 break; 211 break;
150 case 0x4ec8be4c: /* Looped */ 212 case 0x4ec8be4c: /* Looped */
151 tmp_ptr = get_bool(tmp_ptr, &edl.edl[edl.current].looped); 213 ADD_TO_OFFSET(looped, EDL_internal_get_bool);
152 break; 214 break;
153 case 0xe6cb84d1: /* OnRuler */ 215 case 0xe6cb84d1: /* OnRuler */
154 tmp_ptr = get_bool(tmp_ptr, &edl.edl[edl.current].on_ruler); 216 ADD_TO_OFFSET(on_ruler, EDL_internal_get_bool);
155 break; 217 break;
156 case 0x035f84c2: /* MediaType */ 218 case 0x035f84c2: /* MediaType */
157 tmp_ptr = get_media_type(tmp_ptr, &edl.edl[edl.current].media_type); 219 ADD_TO_OFFSET(media_type, EDL_internal_get_media_type);
158 break; 220 break;
159 case 0x379d32c5: /* FileName */ 221 case 0x379d32c5: /* FileName */
160 tmp_ptr = get_string(tmp_ptr, &edl.edl[edl.current].file_name); 222 ADD_TO_OFFSET(filename, EDL_internal_get_string);
161 break; 223 break;
162 case 0x9f334738: /* Stream */ 224 case 0x9f334738: /* Stream */
163 tmp_ptr = get_int(tmp_ptr, &edl.edl[edl.current].stream); 225 ADD_TO_OFFSET(stream, EDL_internal_get_int);
164 break; 226 break;
165 case 0x07b4e0e7: /* StreamStart */ 227 case 0x07b4e0e7: /* StreamStart */
166 tmp_ptr = get_double(tmp_ptr, &edl.edl[edl.current].stream_start); 228 ADD_TO_OFFSET(stream_start, EDL_internal_get_double);
167 break; 229 break;
168 case 0x8f16c7b8: /* StreamLength */ 230 case 0x8f16c7b8: /* StreamLength */
169 tmp_ptr = get_double(tmp_ptr, &edl.edl[edl.current].stream_length); 231 ADD_TO_OFFSET(stream_length, EDL_internal_get_double);
170 break; 232 break;
171 case 0xd2edd7e4: /* FadeTimeIn */ 233 case 0xd2edd7e4: /* FadeTimeIn */
172 tmp_ptr = get_double(tmp_ptr, &edl.edl[edl.current].fade_time_in); 234 ADD_TO_OFFSET(fade_time_in, EDL_internal_get_double);
173 break; 235 break;
174 case 0x792e8c40: /* FadeTimeOut */ 236 case 0x792e8c40: /* FadeTimeOut */
175 tmp_ptr = get_double(tmp_ptr, &edl.edl[edl.current].fade_time_out); 237 ADD_TO_OFFSET(fade_time_out, EDL_internal_get_double);
176 break; 238 break;
177 case 0x98374657: /* SustainGain */ 239 case 0x98374657: /* SustainGain */
178 tmp_ptr = get_double(tmp_ptr, &edl.edl[edl.current].sustain_gain); 240 ADD_TO_OFFSET(sustain_gain, EDL_internal_get_double);
179 break; 241 break;
180 case 0x3e998b1f: /* CurveIn */ 242 case 0x3e998b1f: /* CurveIn */
181 tmp_ptr = get_int(tmp_ptr, &edl.edl[edl.current].curve_in); 243 ADD_TO_OFFSET(curve_in, EDL_internal_get_int);
182 break; 244 break;
183 case 0x82fb09c4: /* GainIn */ 245 case 0x82fb09c4: /* GainIn */
184 tmp_ptr = get_double(tmp_ptr, &edl.edl[edl.current].gain_in); 246 ADD_TO_OFFSET(gain_in, EDL_internal_get_double);
185 break; 247 break;
186 case 0x53add388: /* CurveOut */ 248 case 0x53add388: /* CurveOut */
187 tmp_ptr = get_int(tmp_ptr, &edl.edl[edl.current].curve_out); 249 ADD_TO_OFFSET(curve_out, EDL_internal_get_int);
188 break; 250 break;
189 case 0x4210ba56: /* GainOut */ 251 case 0x4210ba56: /* GainOut */
190 tmp_ptr = get_double(tmp_ptr, &edl.edl[edl.current].gain_out); 252 ADD_TO_OFFSET(gain_out, EDL_internal_get_double);
191 break; 253 break;
192 case 0x89c4df6c: /* Layer */ 254 case 0x89c4df6c: /* Layer */
193 tmp_ptr = get_int(tmp_ptr, &edl.edl[edl.current].layer); 255 ADD_TO_OFFSET(layer, EDL_internal_get_int);
194 break; 256 break;
195 case 0xadf2f1a3: /* Color */ 257 case 0xadf2f1a3: /* Color */
196 tmp_ptr = get_int(tmp_ptr, &edl.edl[edl.current].color); 258 ADD_TO_OFFSET(color, EDL_internal_get_int);
197 break; 259 break;
198 case 0xa56b43e1: /* CurveInR */ 260 case 0xa56b43e1: /* CurveInR */
199 tmp_ptr = get_int(tmp_ptr, &edl.edl[edl.current].curve_in_r); 261 ADD_TO_OFFSET(curve_in_r, EDL_internal_get_int);
200 break; 262 break;
201 case 0xcb6d715e: /* CurveOutR */ 263 case 0xcb6d715e: /* CurveOutR */
202 tmp_ptr = get_int(tmp_ptr, &edl.edl[edl.current].curve_out_r); 264 ADD_TO_OFFSET(curve_out_r, EDL_internal_get_int);
203 break; 265 break;
204 case 0x9da1b9ed: /* PlayPitch */ 266 case 0x9da1b9ed: /* PlayPitch */
205 tmp_ptr = get_double(tmp_ptr, &edl.edl[edl.current].play_pitch); 267 ADD_TO_OFFSET(play_pitch, EDL_internal_get_double);
206 break; 268 break;
207 case 0x2bda6ed4: /* LockPitch */ 269 case 0x2bda6ed4: /* LockPitch */
208 tmp_ptr = get_bool(tmp_ptr, &edl.edl[edl.current].lock_pitch); 270 ADD_TO_OFFSET(lock_pitch, EDL_internal_get_bool);
209 break; 271 break;
210 case 0x3071a4c6: /* FirstChannel */ 272 case 0x3071a4c6: /* FirstChannel */
211 tmp_ptr = get_int(tmp_ptr, &edl.edl[edl.current].first_channel); 273 ADD_TO_OFFSET(first_channel, EDL_internal_get_int);
212 break; 274 break;
213 case 0x7de1bd40: /* Channels */ 275 case 0x7de1bd40: /* Channels */
214 tmp_ptr = get_int(tmp_ptr, &edl.edl[edl.current].channels); 276 ADD_TO_OFFSET(channels, EDL_internal_get_int);
215 break; 277 break;
216 default: 278 default:
217 /* ... what */ 279 /* ... what */
218 break; 280 break;
219 } 281 }
220 } 282 }
221 edl.current++; 283
222 if (edl.current >= edl.size) 284 if (!ok)
223 allocate_memory(&edl); 285 break;
224 } 286
225 } 287 if (++edl.size >= edl.capacity)
288 EDL_internal_allocate_memory(&edl);
289 }
290
291 EDL_internal_shrink_memory_to_fit(&edl);
292
293 free(order);
294
295 return edl;
296 }
297
298 void EDL_free(EDL_file file) {
299 size_t i;
300 for (i = 0; i < file.size; i++) {
301 if (file.arr[i].filename)
302 free(file.arr[i].filename);
303 }
304 free(file.arr);
305 }