297
|
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
|
299
|
141 /*
|
297
|
142 short get_flags(char *s)
|
|
143 {
|
299
|
144 static char *flagstr[] = {
|
|
145 "flip",
|
|
146 "noflip",
|
|
147 "yuvhack",
|
|
148 NULL
|
|
149 };
|
|
150 int j;
|
|
151
|
|
152 printf("flags='%s'\n",s);
|
|
153
|
297
|
154 if (!s)
|
|
155 return 0;
|
|
156 return 1;
|
|
157 }
|
299
|
158 */
|
297
|
159
|
|
160 int add_to_out(char *sfmt, char *sflags, unsigned int *outfmt,
|
|
161 unsigned char *outflags)
|
|
162 {
|
|
163 static char *fmtstr[] = {
|
|
164 "YUY2",
|
|
165 "YV12",
|
|
166 "RGB8",
|
|
167 "RGB15",
|
|
168 "RGB16",
|
|
169 "RGB24",
|
|
170 "RGB32",
|
|
171 "BGR8",
|
|
172 "BGR15",
|
|
173 "BGR16",
|
|
174 "BGR24",
|
|
175 "BGR32",
|
|
176 NULL
|
|
177 };
|
|
178 static unsigned int fmtnum[] = {
|
|
179 IMGFMT_YUY2,
|
|
180 IMGFMT_YV12,
|
|
181 IMGFMT_RGB|8,
|
|
182 IMGFMT_RGB|15,
|
|
183 IMGFMT_RGB|16,
|
|
184 IMGFMT_RGB|24,
|
|
185 IMGFMT_RGB|32,
|
|
186 IMGFMT_BGR|8,
|
|
187 IMGFMT_BGR|15,
|
|
188 IMGFMT_BGR|16,
|
|
189 IMGFMT_BGR|24,
|
|
190 IMGFMT_BGR|32
|
|
191 };
|
299
|
192 static char *flagstr[] = {
|
|
193 "flip",
|
|
194 "noflip",
|
|
195 "yuvhack",
|
|
196 NULL
|
|
197 };
|
|
198
|
297
|
199 int i, j;
|
|
200 unsigned char flags;
|
|
201
|
|
202 for (i = 0; i < CODECS_MAX_OUTFMT && outfmt[i] != 0xffffffff; i++)
|
|
203 /* NOTHING */;
|
|
204 if (i == CODECS_MAX_FOURCC) {
|
|
205 printf("too many out...\n");
|
|
206 return 0;
|
|
207 }
|
|
208
|
299
|
209 flags = 0; //get_flags(sflags);
|
|
210 if(sflags) do {
|
|
211 for (j = 0; flagstr[j] != NULL; j++)
|
|
212 if (!strncmp(sflags, flagstr[j], strlen(flagstr[j])))
|
|
213 break;
|
|
214 if (flagstr[j] == NULL) return 0; // error!
|
|
215 flags|=(1<<j);
|
|
216 sflags+=strlen(flagstr[j]);
|
|
217 } while (*(sflags++) == ',');
|
|
218
|
297
|
219
|
|
220 do {
|
299
|
221 for (j = 0; fmtstr[j] != NULL; j++)
|
297
|
222 if (!strncmp(sfmt, fmtstr[j], strlen(fmtstr[j])))
|
|
223 break;
|
|
224 if (fmtstr[j] == NULL)
|
|
225 return 0;
|
|
226 outfmt[i] = fmtnum[j];
|
|
227 outflags[i] = flags;
|
299
|
228 ++i;
|
297
|
229 sfmt+=strlen(fmtstr[j]);
|
|
230 } while (*(sfmt++) == ',');
|
299
|
231 if (*(--sfmt) != '\0') return 0;
|
|
232
|
297
|
233 return 1;
|
|
234 }
|
|
235
|
|
236 short get_driver(char *s)
|
|
237 {
|
|
238 return 0;
|
|
239 }
|
|
240
|
|
241 #define DEBUG
|
|
242
|
|
243 codecs_t *parse_codec_cfg(char *cfgfile)
|
|
244 {
|
|
245 #define PRINT_LINENUM printf("%s(%d): ", cfgfile, line_num)
|
|
246 #define GET_MEM\
|
|
247 do {\
|
|
248 if (!(codecs = (codecs_t *) realloc(codecs,\
|
|
249 sizeof(codecs_t) * (nr_codecs + 1)))) {\
|
|
250 perror("parse_codec_cfg: can't realloc 'codecs'");\
|
|
251 goto err_out;\
|
|
252 }\
|
|
253 } while (0)
|
|
254
|
|
255 codecs_t *codecs = NULL;
|
|
256 int free_slots = 0;
|
|
257 int tmp, i;
|
|
258 int state = 0;
|
|
259 char *param1;
|
|
260
|
|
261 #ifdef DEBUG
|
|
262 assert(cfgfile != NULL);
|
|
263 #endif
|
|
264
|
|
265 printf("Reading codec config file: %s\n", cfgfile);
|
|
266
|
|
267 if ((line = (char *) malloc(MAX_LINE_LEN + 1)) == NULL) {
|
|
268 perror("parse_codec_cfg: can't get memory for 'line'");
|
|
269 return NULL;
|
|
270 }
|
|
271
|
|
272 if ((fp = fopen(cfgfile, "r")) == NULL) {
|
|
273 printf("parse_codec_cfg: can't open '%s': %s\n", cfgfile, strerror(errno));
|
|
274 free(line);
|
|
275 return NULL;
|
|
276 }
|
|
277 line_pos = 0;
|
|
278 line[0] = '\0'; /* forces get_token to read next line */
|
|
279
|
|
280 for (;;) {
|
|
281 tmp = get_token();
|
|
282 if (tmp == RET_EOF)
|
|
283 goto eof_out;
|
|
284 if (tmp == RET_EOL)
|
|
285 continue;
|
|
286 if (!strcmp(token, "audiocodec") || !strcmp(token, "videocodec")) {
|
|
287 if (codecs) {
|
|
288 // tmp = ~state & STATE_MASK;
|
|
289 // if (tmp != GOT_FOURCC && tmp != GOT_FORMAT)
|
|
290 // goto parse_error_out;
|
|
291 nr_codecs++;
|
|
292 }
|
|
293 PRINT_LINENUM;
|
|
294 GET_MEM;
|
|
295 state = 0;
|
|
296 codecs[nr_codecs].comment = NULL;
|
|
297 memset(codecs[nr_codecs].fourcc, 0xff,
|
|
298 sizeof(codecs[nr_codecs].fourcc) );
|
|
299 /*
|
|
300 memset(codecs[nr_codecs].fourccmap, 0xff,
|
|
301 sizeof(codecs[nr_codecs].fourccmap) *
|
|
302 CODECS_MAX_FOURCC);
|
|
303 */
|
|
304 memset(codecs[nr_codecs].outfmt, 0xff,
|
|
305 sizeof(codecs[nr_codecs].outfmt) );
|
|
306 if (*token == 'a') { /* audiocodec */
|
|
307 printf("audio");
|
|
308 codecs[nr_codecs].flags |= CODECS_FLAG_AUDIO;
|
|
309 } else if (*token == 'v') { /* videocodec */
|
|
310 printf("video");
|
|
311 codecs[nr_codecs].flags &= !CODECS_FLAG_AUDIO;
|
|
312 } else {
|
|
313 printf("itt valami nagyon el van baszva\n");
|
|
314 goto err_out;
|
|
315 }
|
|
316 if (get_token() < 0)
|
|
317 goto parse_error_out;
|
|
318 codecs[nr_codecs].name = strdup(token);
|
|
319 state |= GOT_NAME;
|
|
320 printf(" %s\n", codecs[nr_codecs].name);
|
|
321 } else if (!strcmp(token, "info")) {
|
|
322 if (!(state & GOT_NAME))
|
|
323 goto parse_error_out;
|
|
324 PRINT_LINENUM;
|
|
325 printf("info");
|
|
326 if (state & GOT_INFO || get_token() < 0)
|
|
327 goto parse_error_out;
|
|
328 codecs[nr_codecs].info = strdup(token);
|
|
329 state |= GOT_INFO;
|
|
330 printf(" %s\n", codecs[nr_codecs].info);
|
|
331 } else if (!strcmp(token, "comment")) {
|
|
332 if (!(state & GOT_NAME))
|
|
333 goto parse_error_out;
|
|
334 PRINT_LINENUM;
|
|
335 printf("comment");
|
|
336 if (get_token() < 0)
|
|
337 goto parse_error_out;
|
|
338 #if 1
|
|
339 if (!codecs[nr_codecs].comment)
|
|
340 codecs[nr_codecs].comment = strdup(token);
|
|
341 printf(" %s\n", codecs[nr_codecs].comment);
|
|
342 #else
|
|
343 add_comment(token, &codecs[nr_codecs].comment);
|
|
344 printf(" FIXMEEEEEEEEEEEEEEE\n");
|
|
345 #endif
|
|
346 } else if (!strcmp(token, "fourcc")) {
|
|
347 if (!(state & GOT_NAME))
|
|
348 goto parse_error_out;
|
|
349 PRINT_LINENUM;
|
|
350 printf("fourcc");
|
|
351 if (codecs[nr_codecs].flags & CODECS_FLAG_AUDIO) {
|
|
352 printf("\n'fourcc' in audiocodec definition!\n");
|
|
353 goto err_out;
|
|
354 }
|
|
355 if (get_token() < 0)
|
|
356 goto parse_error_out;
|
|
357 param1 = strdup(token);
|
|
358 get_token();
|
|
359 if (!add_to_fourcc(param1, token,
|
|
360 codecs[nr_codecs].fourcc,
|
|
361 codecs[nr_codecs].fourccmap))
|
|
362 goto err_out;
|
|
363 state |= GOT_FOURCC;
|
|
364 printf(" %s: %s\n", param1, token);
|
|
365 free(param1);
|
|
366 } else if (!strcmp(token, "format")) {
|
|
367 if (!(state & GOT_NAME))
|
|
368 goto parse_error_out;
|
|
369 PRINT_LINENUM;
|
|
370 printf("format");
|
|
371 if (!(codecs[nr_codecs].flags & CODECS_FLAG_AUDIO)) {
|
|
372 printf("\n'format' in videocodec definition!\n");
|
|
373 goto err_out;
|
|
374 }
|
|
375 if (get_token() < 0)
|
|
376 goto parse_error_out;
|
|
377 if (!add_to_format(token, codecs[nr_codecs].fourcc))
|
|
378 goto err_out;
|
|
379 state |= GOT_FORMAT;
|
|
380 printf(" %s\n", token);
|
|
381 } else if (!strcmp(token, "driver")) {
|
|
382 if (!(state & GOT_NAME))
|
|
383 goto parse_error_out;
|
|
384 PRINT_LINENUM;
|
|
385 printf("driver");
|
|
386 if (get_token() < 0)
|
|
387 goto parse_error_out;
|
|
388 if ((codecs[nr_codecs].driver = get_driver(token)) == -1)
|
|
389 goto err_out;
|
|
390 printf(" %s\n", token);
|
|
391 } else if (!strcmp(token, "dll")) {
|
|
392 if (!(state & GOT_NAME))
|
|
393 goto parse_error_out;
|
|
394 PRINT_LINENUM;
|
|
395 printf("dll");
|
|
396 if (get_token() < 0)
|
|
397 goto parse_error_out;
|
|
398 codecs[nr_codecs].dll = strdup(token);
|
|
399 printf(" %s\n", codecs[nr_codecs].dll);
|
|
400 } else if (!strcmp(token, "guid")) {
|
|
401 if (!(state & GOT_NAME))
|
|
402 goto parse_error_out;
|
|
403 PRINT_LINENUM;
|
|
404 printf("guid");
|
|
405 if (get_token() < 0)
|
|
406 goto parse_error_out;
|
|
407 sscanf(token, "%ld,", &codecs[nr_codecs].guid.f1);
|
|
408 if (get_token() < 0)
|
|
409 goto parse_error_out;
|
|
410 sscanf(token, "%d,", &tmp);
|
|
411 codecs[nr_codecs].guid.f2 = tmp;
|
|
412 if (get_token() < 0)
|
|
413 goto parse_error_out;
|
|
414 sscanf(token, "%d,", &tmp);
|
|
415 codecs[nr_codecs].guid.f3 = tmp;
|
|
416 for (i = 0; i < 7; i++) {
|
|
417 if (get_token() < 0)
|
|
418 goto parse_error_out;
|
|
419 sscanf(token, "%d,", &tmp);
|
|
420 codecs[nr_codecs].guid.f4[i] = tmp;
|
|
421 }
|
|
422 if (get_token() < 0)
|
|
423 goto parse_error_out;
|
|
424 sscanf(token, "%d", &tmp);
|
|
425 codecs[nr_codecs].guid.f4[7] = tmp;
|
|
426 printf(" %s\n", token);
|
|
427 } else if (!strcmp(token, "out")) {
|
|
428 if (!(state & GOT_NAME))
|
|
429 goto parse_error_out;
|
|
430 PRINT_LINENUM;
|
|
431 printf("out");
|
|
432 if (get_token() < 0)
|
|
433 goto parse_error_out;
|
|
434 param1 = strdup(token);
|
|
435 get_token();
|
|
436 if (!add_to_out(param1, token, codecs[nr_codecs].outfmt,
|
|
437 codecs[nr_codecs].outflags))
|
|
438 goto err_out;
|
|
439 printf(" %s: %s\n", param1, token);
|
|
440 free(param1);
|
|
441 } else if (!strcmp(token, "flags")) {
|
|
442 if (!(state & GOT_NAME))
|
|
443 goto parse_error_out;
|
|
444 PRINT_LINENUM;
|
|
445 printf("flags");
|
|
446 if (get_token() < 0)
|
|
447 goto parse_error_out;
|
|
448 printf(" %s\n", token);
|
|
449 } else
|
|
450 goto parse_error_out;
|
|
451 }
|
|
452 out:
|
|
453 free(line);
|
|
454 fclose(fp);
|
|
455 return codecs;
|
|
456 parse_error_out:
|
|
457 PRINT_LINENUM;
|
|
458 printf("parse error\n");
|
|
459 err_out:
|
|
460 printf("\nOops\n");
|
|
461 if (codecs)
|
|
462 free(codecs);
|
|
463 codecs = NULL;
|
|
464 goto out;
|
|
465 eof_out:
|
|
466 /* FIXME teljes az utolso config?? */
|
|
467 goto out;
|
|
468 }
|
|
469
|
|
470 #ifdef TESTING
|
|
471 int main(void)
|
|
472 {
|
|
473 codecs_t *codecs;
|
|
474 int i,j;
|
|
475
|
298
|
476 codecs = parse_codec_cfg("DOCS/codecs.conf");
|
297
|
477
|
|
478 printf("total %d codecs parsed\n",nr_codecs);
|
|
479 for(i=0;i<nr_codecs;i++){
|
|
480 codecs_t *c=&codecs[i];
|
|
481 printf("\n============== codec %02d ===============\n",i);
|
|
482 printf("name='%s'\n",c->name);
|
|
483 printf("info='%s'\n",c->info);
|
|
484 printf("comment='%s'\n",c->comment);
|
|
485 printf("dll='%s'\n",c->dll);
|
|
486 printf("flags=%X driver=%d\n",c->flags,c->driver);
|
|
487 for(j=0;j<CODECS_MAX_FOURCC;j++){
|
|
488 if(c->fourcc[j]!=0xFFFFFFFF){
|
|
489 printf("fourcc %02d: %08X (%.4s) ===> %08X (%.4s)\n",j,c->fourcc[j],&c->fourcc[j],c->fourccmap[j],&c->fourccmap[j]);
|
|
490 }
|
|
491 }
|
299
|
492 for(j=0;j<CODECS_MAX_OUTFMT;j++){
|
|
493 if(c->outfmt[j]!=0xFFFFFFFF){
|
|
494 printf("outfmt %02d: %08X (%.4s) flags: %d\n",j,c->outfmt[j],&c->outfmt[j],c->outflags[j]);
|
|
495 }
|
|
496 }
|
297
|
497 }
|
|
498
|
|
499 return 0;
|
|
500 }
|
|
501
|
|
502 #endif
|