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
|
301
|
232 short get_driver(char *s,int audioflag)
|
297
|
233 {
|
301
|
234 static char *audiodrv[] = {
|
|
235 "mp3lib",
|
|
236 "pcm",
|
|
237 "libac3",
|
|
238 "acm",
|
|
239 "alaw",
|
|
240 "msgsm",
|
|
241 "dshow",
|
|
242 NULL
|
|
243 };
|
|
244 static char *videodrv[] = {
|
|
245 "libmpeg2",
|
|
246 "vfw",
|
|
247 "odivx",
|
|
248 "dshow",
|
|
249 NULL
|
|
250 };
|
|
251 char **drv=audioflag?audiodrv:videodrv;
|
|
252 int i;
|
|
253
|
|
254 for(i=0;drv[i];i++) if(!strcmp(s,drv[i])) return i+1;
|
|
255
|
297
|
256 return 0;
|
|
257 }
|
|
258
|
301
|
259 //#define DEBUG
|
297
|
260
|
|
261 codecs_t *parse_codec_cfg(char *cfgfile)
|
|
262 {
|
|
263 #define PRINT_LINENUM printf("%s(%d): ", cfgfile, line_num)
|
|
264
|
300
|
265 codecs_t *codecs = NULL; // array of codecs
|
|
266 codecs_t *codec = NULL; // currect codec
|
297
|
267 int free_slots = 0;
|
|
268 int tmp, i;
|
|
269 int state = 0;
|
|
270 char *param1;
|
|
271
|
|
272 #ifdef DEBUG
|
|
273 assert(cfgfile != NULL);
|
|
274 #endif
|
|
275
|
|
276 printf("Reading codec config file: %s\n", cfgfile);
|
|
277
|
301
|
278 if ((fp = fopen(cfgfile, "r")) == NULL) {
|
|
279 printf("parse_codec_cfg: can't open '%s': %s\n", cfgfile, strerror(errno));
|
297
|
280 return NULL;
|
|
281 }
|
|
282
|
301
|
283 if ((line = (char *) malloc(MAX_LINE_LEN + 1)) == NULL) {
|
|
284 perror("parse_codec_cfg: can't get memory for 'line'");
|
297
|
285 return NULL;
|
|
286 }
|
|
287 line_pos = 0;
|
|
288 line[0] = '\0'; /* forces get_token to read next line */
|
|
289
|
|
290 for (;;) {
|
|
291 tmp = get_token();
|
|
292 if (tmp == RET_EOF)
|
|
293 goto eof_out;
|
|
294 if (tmp == RET_EOL)
|
|
295 continue;
|
|
296 if (!strcmp(token, "audiocodec") || !strcmp(token, "videocodec")) {
|
|
297 PRINT_LINENUM;
|
300
|
298
|
|
299 if (!(codecs = (codecs_t *) realloc(codecs,
|
|
300 sizeof(codecs_t) * (nr_codecs + 1)))) {
|
|
301 perror("parse_codec_cfg: can't realloc 'codecs'");
|
|
302 goto err_out;
|
|
303 }
|
|
304 codec=&codecs[nr_codecs];
|
|
305 nr_codecs++;
|
|
306 memset(codec,0,sizeof(codecs_t));
|
|
307 memset(codec->fourcc, 0xff, sizeof(codec->fourcc));
|
|
308 memset(codec->outfmt, 0xff, sizeof(codec->outfmt));
|
297
|
309 state = 0;
|
300
|
310
|
297
|
311 if (*token == 'a') { /* audiocodec */
|
|
312 printf("audio");
|
300
|
313 codec->flags |= CODECS_FLAG_AUDIO;
|
297
|
314 } else if (*token == 'v') { /* videocodec */
|
|
315 printf("video");
|
300
|
316 codec->flags &= !CODECS_FLAG_AUDIO;
|
297
|
317 } else {
|
|
318 printf("itt valami nagyon el van baszva\n");
|
|
319 goto err_out;
|
|
320 }
|
|
321 if (get_token() < 0)
|
|
322 goto parse_error_out;
|
300
|
323 codec->name = strdup(token);
|
297
|
324 state |= GOT_NAME;
|
300
|
325 printf(" %s\n", codec->name);
|
297
|
326 } else if (!strcmp(token, "info")) {
|
|
327 if (!(state & GOT_NAME))
|
|
328 goto parse_error_out;
|
|
329 PRINT_LINENUM;
|
|
330 printf("info");
|
|
331 if (state & GOT_INFO || get_token() < 0)
|
|
332 goto parse_error_out;
|
300
|
333 codec->info = strdup(token);
|
297
|
334 state |= GOT_INFO;
|
300
|
335 printf(" %s\n", codec->info);
|
297
|
336 } else if (!strcmp(token, "comment")) {
|
|
337 if (!(state & GOT_NAME))
|
|
338 goto parse_error_out;
|
|
339 PRINT_LINENUM;
|
|
340 printf("comment");
|
|
341 if (get_token() < 0)
|
|
342 goto parse_error_out;
|
|
343 #if 1
|
300
|
344 if (!codec->comment)
|
|
345 codec->comment = strdup(token);
|
|
346 printf(" %s\n", codec->comment);
|
297
|
347 #else
|
300
|
348 add_comment(token, &codec->comment);
|
297
|
349 printf(" FIXMEEEEEEEEEEEEEEE\n");
|
|
350 #endif
|
|
351 } else if (!strcmp(token, "fourcc")) {
|
|
352 if (!(state & GOT_NAME))
|
|
353 goto parse_error_out;
|
|
354 PRINT_LINENUM;
|
|
355 printf("fourcc");
|
300
|
356 if (codec->flags & CODECS_FLAG_AUDIO) {
|
297
|
357 printf("\n'fourcc' in audiocodec definition!\n");
|
|
358 goto err_out;
|
|
359 }
|
|
360 if (get_token() < 0)
|
|
361 goto parse_error_out;
|
|
362 param1 = strdup(token);
|
|
363 get_token();
|
|
364 if (!add_to_fourcc(param1, token,
|
300
|
365 codec->fourcc,
|
|
366 codec->fourccmap))
|
297
|
367 goto err_out;
|
|
368 state |= GOT_FOURCC;
|
|
369 printf(" %s: %s\n", param1, token);
|
|
370 free(param1);
|
|
371 } else if (!strcmp(token, "format")) {
|
|
372 if (!(state & GOT_NAME))
|
|
373 goto parse_error_out;
|
|
374 PRINT_LINENUM;
|
|
375 printf("format");
|
300
|
376 if (!(codec->flags & CODECS_FLAG_AUDIO)) {
|
297
|
377 printf("\n'format' in videocodec definition!\n");
|
|
378 goto err_out;
|
|
379 }
|
|
380 if (get_token() < 0)
|
|
381 goto parse_error_out;
|
300
|
382 if (!add_to_format(token, codec->fourcc,codec->fourccmap))
|
297
|
383 goto err_out;
|
|
384 state |= GOT_FORMAT;
|
|
385 printf(" %s\n", token);
|
|
386 } else if (!strcmp(token, "driver")) {
|
|
387 if (!(state & GOT_NAME))
|
|
388 goto parse_error_out;
|
|
389 PRINT_LINENUM;
|
|
390 printf("driver");
|
|
391 if (get_token() < 0)
|
|
392 goto parse_error_out;
|
301
|
393 if ((codec->driver = get_driver(token,codec->flags&CODECS_FLAG_AUDIO)) == -1)
|
297
|
394 goto err_out;
|
|
395 printf(" %s\n", token);
|
|
396 } else if (!strcmp(token, "dll")) {
|
|
397 if (!(state & GOT_NAME))
|
|
398 goto parse_error_out;
|
|
399 PRINT_LINENUM;
|
|
400 printf("dll");
|
|
401 if (get_token() < 0)
|
|
402 goto parse_error_out;
|
300
|
403 codec->dll = strdup(token);
|
|
404 printf(" %s\n", codec->dll);
|
297
|
405 } else if (!strcmp(token, "guid")) {
|
|
406 if (!(state & GOT_NAME))
|
|
407 goto parse_error_out;
|
|
408 PRINT_LINENUM;
|
|
409 printf("guid");
|
300
|
410 if (get_token() < 0) goto parse_error_out;
|
|
411 printf("'%s'",token);
|
|
412 codec->guid.f1=strtoul(token,NULL,0);
|
|
413 if (get_token() < 0) goto parse_error_out;
|
|
414 codec->guid.f2=strtoul(token,NULL,0);
|
|
415 if (get_token() < 0) goto parse_error_out;
|
|
416 codec->guid.f3=strtoul(token,NULL,0);
|
|
417 for (i = 0; i < 8; i++) {
|
|
418 if (get_token() < 0) goto parse_error_out;
|
|
419 codec->guid.f4[i]=strtoul(token,NULL,0);
|
297
|
420 }
|
|
421 } else if (!strcmp(token, "out")) {
|
|
422 if (!(state & GOT_NAME))
|
|
423 goto parse_error_out;
|
|
424 PRINT_LINENUM;
|
|
425 printf("out");
|
|
426 if (get_token() < 0)
|
|
427 goto parse_error_out;
|
|
428 param1 = strdup(token);
|
|
429 get_token();
|
300
|
430 if (!add_to_out(param1, token, codec->outfmt,
|
|
431 codec->outflags))
|
297
|
432 goto err_out;
|
|
433 printf(" %s: %s\n", param1, token);
|
|
434 free(param1);
|
|
435 } else if (!strcmp(token, "flags")) {
|
|
436 if (!(state & GOT_NAME))
|
|
437 goto parse_error_out;
|
|
438 PRINT_LINENUM;
|
|
439 printf("flags");
|
|
440 if (get_token() < 0)
|
|
441 goto parse_error_out;
|
|
442 printf(" %s\n", token);
|
|
443 } else
|
|
444 goto parse_error_out;
|
|
445 }
|
|
446 out:
|
|
447 free(line);
|
|
448 fclose(fp);
|
|
449 return codecs;
|
|
450 parse_error_out:
|
|
451 PRINT_LINENUM;
|
|
452 printf("parse error\n");
|
|
453 err_out:
|
|
454 printf("\nOops\n");
|
|
455 if (codecs)
|
|
456 free(codecs);
|
|
457 codecs = NULL;
|
|
458 goto out;
|
|
459 eof_out:
|
|
460 /* FIXME teljes az utolso config?? */
|
|
461 goto out;
|
|
462 }
|
|
463
|
|
464 #ifdef TESTING
|
|
465 int main(void)
|
|
466 {
|
|
467 codecs_t *codecs;
|
|
468 int i,j;
|
|
469
|
298
|
470 codecs = parse_codec_cfg("DOCS/codecs.conf");
|
297
|
471
|
|
472 printf("total %d codecs parsed\n",nr_codecs);
|
|
473 for(i=0;i<nr_codecs;i++){
|
|
474 codecs_t *c=&codecs[i];
|
|
475 printf("\n============== codec %02d ===============\n",i);
|
|
476 printf("name='%s'\n",c->name);
|
|
477 printf("info='%s'\n",c->info);
|
|
478 printf("comment='%s'\n",c->comment);
|
|
479 printf("dll='%s'\n",c->dll);
|
|
480 printf("flags=%X driver=%d\n",c->flags,c->driver);
|
300
|
481
|
297
|
482 for(j=0;j<CODECS_MAX_FOURCC;j++){
|
|
483 if(c->fourcc[j]!=0xFFFFFFFF){
|
|
484 printf("fourcc %02d: %08X (%.4s) ===> %08X (%.4s)\n",j,c->fourcc[j],&c->fourcc[j],c->fourccmap[j],&c->fourccmap[j]);
|
|
485 }
|
|
486 }
|
300
|
487
|
299
|
488 for(j=0;j<CODECS_MAX_OUTFMT;j++){
|
|
489 if(c->outfmt[j]!=0xFFFFFFFF){
|
|
490 printf("outfmt %02d: %08X (%.4s) flags: %d\n",j,c->outfmt[j],&c->outfmt[j],c->outflags[j]);
|
|
491 }
|
|
492 }
|
300
|
493
|
|
494 printf("GUID: %08X %04X %04X",c->guid.f1,c->guid.f2,c->guid.f3);
|
|
495 for(j=0;j<8;j++) printf(" %02X",c->guid.f4[j]);
|
|
496 printf("\n");
|
|
497
|
|
498
|
297
|
499 }
|
|
500
|
|
501 return 0;
|
|
502 }
|
|
503
|
|
504 #endif
|