Mercurial > mplayer.hg
comparison codec-cfg.c @ 297:9b00ddddc0b2
imported codec-cfg, small fixes
author | arpi_esp |
---|---|
date | Fri, 06 Apr 2001 23:20:46 +0000 |
parents | |
children | a11ea84cc1ad |
comparison
equal
deleted
inserted
replaced
296:c3d7a28a0d1a | 297:9b00ddddc0b2 |
---|---|
1 #include <stdio.h> | |
2 #include <stdlib.h> | |
3 #include <fcntl.h> | |
4 #include <unistd.h> | |
5 #include <errno.h> | |
6 #include <ctype.h> | |
7 #include <assert.h> | |
8 #include <string.h> | |
9 | |
10 #include "libvo/video_out.h" | |
11 #include "codec-cfg.h" | |
12 | |
13 #define MALLOC_ADD 10 | |
14 | |
15 #define MAX_LINE_LEN 1000 | |
16 | |
17 #define STATE_MASK ((1<<7)-1) | |
18 | |
19 #define GOT_NAME (1<<0) | |
20 #define GOT_INFO (1<<1) | |
21 #define GOT_FOURCC (1<<2) | |
22 #define GOT_FORMAT (1<<3) | |
23 #define GOT_DRIVER (1<<4) | |
24 #define GOT_DLL (1<<5) | |
25 #define GOT_OUT (1<<6) | |
26 | |
27 #define RET_EOF -1 | |
28 #define RET_EOL -2 | |
29 #define RET_OK 0 | |
30 | |
31 FILE *fp; | |
32 int line_num = 0; | |
33 int line_pos; /* line pos */ | |
34 int firstdef = 1; | |
35 char *line; | |
36 char *token; | |
37 | |
38 int nr_codecs = 0; | |
39 | |
40 int get_token(void) | |
41 { | |
42 static int read_nextline = 1; | |
43 | |
44 if (read_nextline) { | |
45 if (!fgets(line, MAX_LINE_LEN, fp)) | |
46 goto ret_eof; | |
47 line_pos = 0; | |
48 ++line_num; | |
49 read_nextline = 0; | |
50 } | |
51 while (isspace(line[line_pos])) | |
52 ++line_pos; | |
53 if (line[line_pos] == '\0' || line[line_pos] == '#' || | |
54 line[line_pos] == ';') { | |
55 read_nextline = 1; | |
56 goto ret_eol; | |
57 } | |
58 token = line + line_pos; | |
59 if (line[line_pos] == '"') { | |
60 token++; | |
61 for (/* NOTHING */; line[++line_pos] != '"' && line[line_pos];) | |
62 /* NOTHING */; | |
63 if (!line[line_pos]) { | |
64 read_nextline = 1; | |
65 goto ret_eol; | |
66 } | |
67 } else { | |
68 for (/* NOTHING */; !isspace(line[line_pos]); line_pos++) | |
69 /* NOTHING */; | |
70 } | |
71 line[line_pos] = '\0'; | |
72 line_pos++; | |
73 #ifdef DEBUG | |
74 printf("get_token ok\n"); | |
75 #endif | |
76 return RET_OK; | |
77 ret_eof: | |
78 #ifdef DEBUG | |
79 printf("get_token EOF\n"); | |
80 #endif | |
81 token = NULL; | |
82 return RET_EOF; | |
83 ret_eol: | |
84 #ifdef DEBUG | |
85 printf("get_token EOL\n"); | |
86 #endif | |
87 token = NULL; | |
88 return RET_EOL; | |
89 } | |
90 | |
91 int add_to_fourcc(char *s, char *alias, unsigned int *fourcc, | |
92 unsigned int *map) | |
93 { | |
94 int i; | |
95 char **aliasp; | |
96 | |
97 /* find first unused slot */ | |
98 for (i = 0; i < CODECS_MAX_FOURCC && fourcc[i] != 0xffffffff; i++) | |
99 /* NOTHING */; | |
100 if (i == CODECS_MAX_FOURCC) { | |
101 printf("too many fourcc...\n"); | |
102 return 0; | |
103 } | |
104 #if 1 | |
105 if (alias) { | |
106 do { | |
107 fourcc[i] = *((unsigned int *) s); | |
108 map[i] = *((unsigned int *) alias); | |
109 s += 4; | |
110 i++; | |
111 } while (*(s++) == ','); | |
112 } else { | |
113 do { | |
114 fourcc[i] = *((unsigned int *) s); | |
115 map[i] = *((unsigned int *) s); | |
116 s += 4; | |
117 i++; | |
118 } while (*(s++) == ','); | |
119 } | |
120 #else | |
121 if (alias) | |
122 aliasp = &alias; | |
123 else | |
124 aliasp = &s; | |
125 do { | |
126 fourcc[i] = *((unsigned int *) s); | |
127 map[i++] = *((unsigned int *) (*aliasp)); | |
128 s += 4; | |
129 } while (*(s++) == ','); | |
130 #endif | |
131 if (*(--s) != '\0') | |
132 return 0; | |
133 return 1; | |
134 } | |
135 | |
136 int add_to_format(char *s, unsigned int *format) | |
137 { | |
138 return 1; | |
139 } | |
140 | |
141 short get_flags(char *s) | |
142 { | |
143 if (!s) | |
144 return 0; | |
145 return 1; | |
146 } | |
147 | |
148 int add_to_out(char *sfmt, char *sflags, unsigned int *outfmt, | |
149 unsigned char *outflags) | |
150 { | |
151 static char *fmtstr[] = { | |
152 "YUY2", | |
153 "YV12", | |
154 "RGB8", | |
155 "RGB15", | |
156 "RGB16", | |
157 "RGB24", | |
158 "RGB32", | |
159 "BGR8", | |
160 "BGR15", | |
161 "BGR16", | |
162 "BGR24", | |
163 "BGR32", | |
164 NULL | |
165 }; | |
166 static unsigned int fmtnum[] = { | |
167 IMGFMT_YUY2, | |
168 IMGFMT_YV12, | |
169 IMGFMT_RGB|8, | |
170 IMGFMT_RGB|15, | |
171 IMGFMT_RGB|16, | |
172 IMGFMT_RGB|24, | |
173 IMGFMT_RGB|32, | |
174 IMGFMT_BGR|8, | |
175 IMGFMT_BGR|15, | |
176 IMGFMT_BGR|16, | |
177 IMGFMT_BGR|24, | |
178 IMGFMT_BGR|32 | |
179 }; | |
180 int i, j; | |
181 unsigned char flags; | |
182 | |
183 for (i = 0; i < CODECS_MAX_OUTFMT && outfmt[i] != 0xffffffff; i++) | |
184 /* NOTHING */; | |
185 if (i == CODECS_MAX_FOURCC) { | |
186 printf("too many out...\n"); | |
187 return 0; | |
188 } | |
189 | |
190 flags = get_flags(sflags); | |
191 | |
192 do { | |
193 for (j = 0; fmtstr[i] != NULL; j++) | |
194 if (!strncmp(sfmt, fmtstr[j], strlen(fmtstr[j]))) | |
195 break; | |
196 if (fmtstr[j] == NULL) | |
197 return 0; | |
198 outfmt[i] = fmtnum[j]; | |
199 outflags[i] = flags; | |
200 sfmt+=strlen(fmtstr[j]); | |
201 } while (*(sfmt++) == ','); | |
202 if (*(--sfmt) != '\0') | |
203 return 0; | |
204 return 1; | |
205 } | |
206 | |
207 short get_driver(char *s) | |
208 { | |
209 return 0; | |
210 } | |
211 | |
212 #define DEBUG | |
213 | |
214 codecs_t *parse_codec_cfg(char *cfgfile) | |
215 { | |
216 #define PRINT_LINENUM printf("%s(%d): ", cfgfile, line_num) | |
217 #define GET_MEM\ | |
218 do {\ | |
219 if (!(codecs = (codecs_t *) realloc(codecs,\ | |
220 sizeof(codecs_t) * (nr_codecs + 1)))) {\ | |
221 perror("parse_codec_cfg: can't realloc 'codecs'");\ | |
222 goto err_out;\ | |
223 }\ | |
224 } while (0) | |
225 | |
226 codecs_t *codecs = NULL; | |
227 int free_slots = 0; | |
228 int tmp, i; | |
229 int state = 0; | |
230 char *param1; | |
231 | |
232 #ifdef DEBUG | |
233 assert(cfgfile != NULL); | |
234 #endif | |
235 | |
236 printf("Reading codec config file: %s\n", cfgfile); | |
237 | |
238 if ((line = (char *) malloc(MAX_LINE_LEN + 1)) == NULL) { | |
239 perror("parse_codec_cfg: can't get memory for 'line'"); | |
240 return NULL; | |
241 } | |
242 | |
243 if ((fp = fopen(cfgfile, "r")) == NULL) { | |
244 printf("parse_codec_cfg: can't open '%s': %s\n", cfgfile, strerror(errno)); | |
245 free(line); | |
246 return NULL; | |
247 } | |
248 line_pos = 0; | |
249 line[0] = '\0'; /* forces get_token to read next line */ | |
250 | |
251 for (;;) { | |
252 tmp = get_token(); | |
253 if (tmp == RET_EOF) | |
254 goto eof_out; | |
255 if (tmp == RET_EOL) | |
256 continue; | |
257 if (!strcmp(token, "audiocodec") || !strcmp(token, "videocodec")) { | |
258 if (codecs) { | |
259 // tmp = ~state & STATE_MASK; | |
260 // if (tmp != GOT_FOURCC && tmp != GOT_FORMAT) | |
261 // goto parse_error_out; | |
262 nr_codecs++; | |
263 } | |
264 PRINT_LINENUM; | |
265 GET_MEM; | |
266 state = 0; | |
267 codecs[nr_codecs].comment = NULL; | |
268 memset(codecs[nr_codecs].fourcc, 0xff, | |
269 sizeof(codecs[nr_codecs].fourcc) ); | |
270 /* | |
271 memset(codecs[nr_codecs].fourccmap, 0xff, | |
272 sizeof(codecs[nr_codecs].fourccmap) * | |
273 CODECS_MAX_FOURCC); | |
274 */ | |
275 memset(codecs[nr_codecs].outfmt, 0xff, | |
276 sizeof(codecs[nr_codecs].outfmt) ); | |
277 if (*token == 'a') { /* audiocodec */ | |
278 printf("audio"); | |
279 codecs[nr_codecs].flags |= CODECS_FLAG_AUDIO; | |
280 } else if (*token == 'v') { /* videocodec */ | |
281 printf("video"); | |
282 codecs[nr_codecs].flags &= !CODECS_FLAG_AUDIO; | |
283 } else { | |
284 printf("itt valami nagyon el van baszva\n"); | |
285 goto err_out; | |
286 } | |
287 if (get_token() < 0) | |
288 goto parse_error_out; | |
289 codecs[nr_codecs].name = strdup(token); | |
290 state |= GOT_NAME; | |
291 printf(" %s\n", codecs[nr_codecs].name); | |
292 } else if (!strcmp(token, "info")) { | |
293 if (!(state & GOT_NAME)) | |
294 goto parse_error_out; | |
295 PRINT_LINENUM; | |
296 printf("info"); | |
297 if (state & GOT_INFO || get_token() < 0) | |
298 goto parse_error_out; | |
299 codecs[nr_codecs].info = strdup(token); | |
300 state |= GOT_INFO; | |
301 printf(" %s\n", codecs[nr_codecs].info); | |
302 } else if (!strcmp(token, "comment")) { | |
303 if (!(state & GOT_NAME)) | |
304 goto parse_error_out; | |
305 PRINT_LINENUM; | |
306 printf("comment"); | |
307 if (get_token() < 0) | |
308 goto parse_error_out; | |
309 #if 1 | |
310 if (!codecs[nr_codecs].comment) | |
311 codecs[nr_codecs].comment = strdup(token); | |
312 printf(" %s\n", codecs[nr_codecs].comment); | |
313 #else | |
314 add_comment(token, &codecs[nr_codecs].comment); | |
315 printf(" FIXMEEEEEEEEEEEEEEE\n"); | |
316 #endif | |
317 } else if (!strcmp(token, "fourcc")) { | |
318 if (!(state & GOT_NAME)) | |
319 goto parse_error_out; | |
320 PRINT_LINENUM; | |
321 printf("fourcc"); | |
322 if (codecs[nr_codecs].flags & CODECS_FLAG_AUDIO) { | |
323 printf("\n'fourcc' in audiocodec definition!\n"); | |
324 goto err_out; | |
325 } | |
326 if (get_token() < 0) | |
327 goto parse_error_out; | |
328 param1 = strdup(token); | |
329 get_token(); | |
330 if (!add_to_fourcc(param1, token, | |
331 codecs[nr_codecs].fourcc, | |
332 codecs[nr_codecs].fourccmap)) | |
333 goto err_out; | |
334 state |= GOT_FOURCC; | |
335 printf(" %s: %s\n", param1, token); | |
336 free(param1); | |
337 } else if (!strcmp(token, "format")) { | |
338 if (!(state & GOT_NAME)) | |
339 goto parse_error_out; | |
340 PRINT_LINENUM; | |
341 printf("format"); | |
342 if (!(codecs[nr_codecs].flags & CODECS_FLAG_AUDIO)) { | |
343 printf("\n'format' in videocodec definition!\n"); | |
344 goto err_out; | |
345 } | |
346 if (get_token() < 0) | |
347 goto parse_error_out; | |
348 if (!add_to_format(token, codecs[nr_codecs].fourcc)) | |
349 goto err_out; | |
350 state |= GOT_FORMAT; | |
351 printf(" %s\n", token); | |
352 } else if (!strcmp(token, "driver")) { | |
353 if (!(state & GOT_NAME)) | |
354 goto parse_error_out; | |
355 PRINT_LINENUM; | |
356 printf("driver"); | |
357 if (get_token() < 0) | |
358 goto parse_error_out; | |
359 if ((codecs[nr_codecs].driver = get_driver(token)) == -1) | |
360 goto err_out; | |
361 printf(" %s\n", token); | |
362 } else if (!strcmp(token, "dll")) { | |
363 if (!(state & GOT_NAME)) | |
364 goto parse_error_out; | |
365 PRINT_LINENUM; | |
366 printf("dll"); | |
367 if (get_token() < 0) | |
368 goto parse_error_out; | |
369 codecs[nr_codecs].dll = strdup(token); | |
370 printf(" %s\n", codecs[nr_codecs].dll); | |
371 } else if (!strcmp(token, "guid")) { | |
372 if (!(state & GOT_NAME)) | |
373 goto parse_error_out; | |
374 PRINT_LINENUM; | |
375 printf("guid"); | |
376 if (get_token() < 0) | |
377 goto parse_error_out; | |
378 sscanf(token, "%ld,", &codecs[nr_codecs].guid.f1); | |
379 if (get_token() < 0) | |
380 goto parse_error_out; | |
381 sscanf(token, "%d,", &tmp); | |
382 codecs[nr_codecs].guid.f2 = tmp; | |
383 if (get_token() < 0) | |
384 goto parse_error_out; | |
385 sscanf(token, "%d,", &tmp); | |
386 codecs[nr_codecs].guid.f3 = tmp; | |
387 for (i = 0; i < 7; i++) { | |
388 if (get_token() < 0) | |
389 goto parse_error_out; | |
390 sscanf(token, "%d,", &tmp); | |
391 codecs[nr_codecs].guid.f4[i] = tmp; | |
392 } | |
393 if (get_token() < 0) | |
394 goto parse_error_out; | |
395 sscanf(token, "%d", &tmp); | |
396 codecs[nr_codecs].guid.f4[7] = tmp; | |
397 printf(" %s\n", token); | |
398 } else if (!strcmp(token, "out")) { | |
399 if (!(state & GOT_NAME)) | |
400 goto parse_error_out; | |
401 PRINT_LINENUM; | |
402 printf("out"); | |
403 if (get_token() < 0) | |
404 goto parse_error_out; | |
405 param1 = strdup(token); | |
406 get_token(); | |
407 if (!add_to_out(param1, token, codecs[nr_codecs].outfmt, | |
408 codecs[nr_codecs].outflags)) | |
409 goto err_out; | |
410 printf(" %s: %s\n", param1, token); | |
411 free(param1); | |
412 } else if (!strcmp(token, "flags")) { | |
413 if (!(state & GOT_NAME)) | |
414 goto parse_error_out; | |
415 PRINT_LINENUM; | |
416 printf("flags"); | |
417 if (get_token() < 0) | |
418 goto parse_error_out; | |
419 printf(" %s\n", token); | |
420 } else | |
421 goto parse_error_out; | |
422 } | |
423 out: | |
424 free(line); | |
425 fclose(fp); | |
426 return codecs; | |
427 parse_error_out: | |
428 PRINT_LINENUM; | |
429 printf("parse error\n"); | |
430 err_out: | |
431 printf("\nOops\n"); | |
432 if (codecs) | |
433 free(codecs); | |
434 codecs = NULL; | |
435 goto out; | |
436 eof_out: | |
437 /* FIXME teljes az utolso config?? */ | |
438 goto out; | |
439 } | |
440 | |
441 #ifdef TESTING | |
442 int main(void) | |
443 { | |
444 codecs_t *codecs; | |
445 int i,j; | |
446 | |
447 codecs = parse_codec_cfg("codecs.conf"); | |
448 | |
449 printf("total %d codecs parsed\n",nr_codecs); | |
450 for(i=0;i<nr_codecs;i++){ | |
451 codecs_t *c=&codecs[i]; | |
452 printf("\n============== codec %02d ===============\n",i); | |
453 printf("name='%s'\n",c->name); | |
454 printf("info='%s'\n",c->info); | |
455 printf("comment='%s'\n",c->comment); | |
456 printf("dll='%s'\n",c->dll); | |
457 printf("flags=%X driver=%d\n",c->flags,c->driver); | |
458 for(j=0;j<CODECS_MAX_FOURCC;j++){ | |
459 if(c->fourcc[j]!=0xFFFFFFFF){ | |
460 printf("fourcc %02d: %08X (%.4s) ===> %08X (%.4s)\n",j,c->fourcc[j],&c->fourcc[j],c->fourccmap[j],&c->fourccmap[j]); | |
461 } | |
462 } | |
463 } | |
464 | |
465 return 0; | |
466 } | |
467 | |
468 #endif |