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