ao: fix crash after ao init failure (from recent 3a5fd15fa2)
[mplayer/greg.git] / codec-cfg.c
blob792972fa3e9aa13bb367523b3aab6b76cde57224
1 /*
2 * codec.conf parser
4 * to compile test application:
5 * cc -I. -DTESTING -o codec-cfg-test codec-cfg.c mp_msg.o osdep/getch2.o -ltermcap
6 * to compile CODECS2HTML:
7 * gcc -DCODECS2HTML -o codecs2html codec-cfg.c mp_msg.o
9 * TODO: implement informat in CODECS2HTML too
11 * Copyright (C) 2001 Szabolcs Berecz <szabi@inf.elte.hu>
13 * This file is part of MPlayer.
15 * MPlayer is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * MPlayer is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License along
26 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #define DEBUG
32 //disable asserts
33 #define NDEBUG
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <errno.h>
40 #include <ctype.h>
41 #include <assert.h>
42 #include <string.h>
43 #include <stdint.h>
45 #include "config.h"
46 #include "mp_msg.h"
47 #ifdef CODECS2HTML
48 #define mp_tmsg mp_msg
49 #ifdef __GNUC__
50 #define mp_msg(t, l, m, args...) fprintf(stderr, m, ##args)
51 #else
52 #define mp_msg(t, l, ...) fprintf(stderr, __VA_ARGS__)
53 #endif
54 #endif
57 #include "libmpcodecs/img_format.h"
58 #include "codec-cfg.h"
60 #ifdef CODECS2HTML
61 #define CODEC_CFG_MIN 20100000
62 #else
63 #include "codecs.conf.h"
64 #endif
66 #define mmioFOURCC( ch0, ch1, ch2, ch3 ) \
67 ( (uint32_t)(uint8_t)(ch0) | ( (uint32_t)(uint8_t)(ch1) << 8 ) | \
68 ( (uint32_t)(uint8_t)(ch2) << 16 ) | ( (uint32_t)(uint8_t)(ch3) << 24 ) )
70 #define PRINT_LINENUM mp_msg(MSGT_CODECCFG,MSGL_ERR," at line %d\n", line_num)
72 #define MAX_NR_TOKEN 16
74 #define MAX_LINE_LEN 1000
76 #define RET_EOF -1
77 #define RET_EOL -2
79 #define TYPE_VIDEO 0
80 #define TYPE_AUDIO 1
82 static int codecs_conf_release;
83 char * codecs_file = NULL;
85 static int add_to_fourcc(char *s, char *alias, unsigned int *fourcc,
86 unsigned int *map)
88 int i, j, freeslots;
89 unsigned int tmp;
91 /* find first unused slot */
92 for (i = 0; i < CODECS_MAX_FOURCC && fourcc[i] != 0xffffffff; i++)
93 /* NOTHING */;
94 freeslots = CODECS_MAX_FOURCC - i;
95 if (!freeslots)
96 goto err_out_too_many;
98 do {
99 tmp = mmioFOURCC(s[0], s[1], s[2], s[3]);
100 for (j = 0; j < i; j++)
101 if (tmp == fourcc[j])
102 goto err_out_duplicated;
103 fourcc[i] = tmp;
104 map[i] = alias ? mmioFOURCC(alias[0], alias[1], alias[2], alias[3]) : tmp;
105 s += 4;
106 i++;
107 } while ((*(s++) == ',') && --freeslots);
109 if (!freeslots)
110 goto err_out_too_many;
111 if (*(--s) != '\0')
112 goto err_out_parse_error;
113 return 1;
114 err_out_duplicated:
115 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"duplicated FourCC");
116 return 0;
117 err_out_too_many:
118 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"too many FourCCs/formats...");
119 return 0;
120 err_out_parse_error:
121 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"parse error");
122 return 0;
125 static int add_to_format(char *s, char *alias,unsigned int *fourcc, unsigned int *fourccmap)
127 int i, j;
128 char *endptr;
130 /* find first unused slot */
131 for (i = 0; i < CODECS_MAX_FOURCC && fourcc[i] != 0xffffffff; i++)
132 /* NOTHING */;
133 if (i == CODECS_MAX_FOURCC) {
134 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"too many FourCCs/formats...");
135 return 0;
138 fourcc[i]=strtoul(s,&endptr,0);
139 if (*endptr != '\0') {
140 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"parse error (format ID not a number?)");
141 return 0;
144 if(alias){
145 fourccmap[i]=strtoul(alias,&endptr,0);
146 if (*endptr != '\0') {
147 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"parse error (format ID alias not a number?)");
148 return 0;
150 } else
151 fourccmap[i]=fourcc[i];
153 for (j = 0; j < i; j++)
154 if (fourcc[j] == fourcc[i]) {
155 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"duplicated format ID");
156 return 0;
159 return 1;
162 static const struct {
163 const char *name;
164 const unsigned int num;
165 } fmt_table[] = {
166 // note: due to parser deficiencies/simplicity, if one format
167 // name matches the beginning of another, the longer one _must_
168 // come first in this list.
169 {"YV12", IMGFMT_YV12},
170 {"I420", IMGFMT_I420},
171 {"IYUV", IMGFMT_IYUV},
172 {"NV12", IMGFMT_NV12},
173 {"NV21", IMGFMT_NV21},
174 {"YVU9", IMGFMT_YVU9},
175 {"IF09", IMGFMT_IF09},
176 {"444P16LE", IMGFMT_444P16_LE},
177 {"444P16BE", IMGFMT_444P16_BE},
178 {"422P16LE", IMGFMT_422P16_LE},
179 {"422P16BE", IMGFMT_422P16_BE},
180 {"420P16LE", IMGFMT_420P16_LE},
181 {"420P16BE", IMGFMT_420P16_BE},
182 {"444P16", IMGFMT_444P16},
183 {"422P16", IMGFMT_422P16},
184 {"420P16", IMGFMT_420P16},
185 {"420A", IMGFMT_420A},
186 {"444P", IMGFMT_444P},
187 {"422P", IMGFMT_422P},
188 {"411P", IMGFMT_411P},
189 {"440P", IMGFMT_440P},
190 {"Y800", IMGFMT_Y800},
191 {"Y8", IMGFMT_Y8},
193 {"YUY2", IMGFMT_YUY2},
194 {"UYVY", IMGFMT_UYVY},
195 {"YVYU", IMGFMT_YVYU},
197 {"RGB48LE", IMGFMT_RGB48LE},
198 {"RGB48BE", IMGFMT_RGB48BE},
199 {"RGB4", IMGFMT_RGB4},
200 {"RGB8", IMGFMT_RGB8},
201 {"RGB15", IMGFMT_RGB15},
202 {"RGB16", IMGFMT_RGB16},
203 {"RGB24", IMGFMT_RGB24},
204 {"RGB32", IMGFMT_RGB32},
205 {"BGR4", IMGFMT_BGR4},
206 {"BGR8", IMGFMT_BGR8},
207 {"BGR15", IMGFMT_BGR15},
208 {"BGR16", IMGFMT_BGR16},
209 {"BGR24", IMGFMT_BGR24},
210 {"BGR32", IMGFMT_BGR32},
211 {"RGB1", IMGFMT_RGB1},
212 {"BGR1", IMGFMT_BGR1},
214 {"MPES", IMGFMT_MPEGPES},
216 {"IDCT_MPEG2",IMGFMT_XVMC_IDCT_MPEG2},
217 {"MOCO_MPEG2",IMGFMT_XVMC_MOCO_MPEG2},
219 {"VDPAU_MPEG1",IMGFMT_VDPAU_MPEG1},
220 {"VDPAU_MPEG2",IMGFMT_VDPAU_MPEG2},
221 {"VDPAU_H264",IMGFMT_VDPAU_H264},
222 {"VDPAU_WMV3",IMGFMT_VDPAU_WMV3},
223 {"VDPAU_VC1",IMGFMT_VDPAU_VC1},
224 {"VDPAU_MPEG4",IMGFMT_VDPAU_MPEG4},
226 {NULL, 0}
230 static int add_to_inout(char *sfmt, char *sflags, unsigned int *outfmt,
231 unsigned char *outflags)
234 static char *flagstr[] = {
235 "flip",
236 "noflip",
237 "yuvhack",
238 "query",
239 "static",
240 NULL
243 int i, j, freeslots;
244 unsigned char flags;
246 for (i = 0; i < CODECS_MAX_OUTFMT && outfmt[i] != 0xffffffff; i++)
247 /* NOTHING */;
248 freeslots = CODECS_MAX_OUTFMT - i;
249 if (!freeslots)
250 goto err_out_too_many;
252 flags = 0;
253 if(sflags) {
254 do {
255 for (j = 0; flagstr[j] != NULL; j++)
256 if (!strncmp(sflags, flagstr[j],
257 strlen(flagstr[j])))
258 break;
259 if (flagstr[j] == NULL)
260 goto err_out_parse_error;
261 flags|=(1<<j);
262 sflags+=strlen(flagstr[j]);
263 } while (*(sflags++) == ',');
265 if (*(--sflags) != '\0')
266 goto err_out_parse_error;
269 do {
270 for (j = 0; fmt_table[j].name != NULL; j++)
271 if (!strncmp(sfmt, fmt_table[j].name, strlen(fmt_table[j].name)))
272 break;
273 if (fmt_table[j].name == NULL)
274 goto err_out_parse_error;
275 outfmt[i] = fmt_table[j].num;
276 outflags[i] = flags;
277 ++i;
278 sfmt+=strlen(fmt_table[j].name);
279 } while ((*(sfmt++) == ',') && --freeslots);
281 if (!freeslots)
282 goto err_out_too_many;
284 if (*(--sfmt) != '\0')
285 goto err_out_parse_error;
287 return 1;
288 err_out_too_many:
289 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"too many out...");
290 return 0;
291 err_out_parse_error:
292 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"parse error");
293 return 0;
296 static int validate_codec(codecs_t *c, int type)
298 unsigned int i;
299 char *tmp_name = c->name;
301 for (i = 0; i < strlen(tmp_name) && isalnum(tmp_name[i]); i++)
302 /* NOTHING */;
304 if (i < strlen(tmp_name)) {
305 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"\ncodec(%s) name is not valid!\n", c->name);
306 return 0;
309 if (!c->info)
310 c->info = strdup(c->name);
312 #if 0
313 if (c->fourcc[0] == 0xffffffff) {
314 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"\ncodec(%s) does not have FourCC/format!\n", c->name);
315 return 0;
317 #endif
319 if (!c->drv) {
320 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"\ncodec(%s) does not have a driver!\n", c->name);
321 return 0;
324 #if 0
325 //FIXME: codec->driver == 4;... <- this should not be put in here...
326 //FIXME: Where are they defined ????????????
327 if (!c->dll && (c->driver == 4 ||
328 (c->driver == 2 && type == TYPE_VIDEO))) {
329 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"\ncodec(%s) needs a 'dll'!\n", c->name);
330 return 0;
332 // FIXME: Can guid.f1 be 0? How does one know that it was not given?
333 // if (!(codec->flags & CODECS_FLAG_AUDIO) && codec->driver == 4)
335 if (type == TYPE_VIDEO)
336 if (c->outfmt[0] == 0xffffffff) {
337 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"\ncodec(%s) needs an 'outfmt'!\n", c->name);
338 return 0;
340 #endif
341 return 1;
344 static int add_comment(char *s, char **d)
346 int pos;
348 if (!*d)
349 pos = 0;
350 else {
351 pos = strlen(*d);
352 (*d)[pos++] = '\n';
354 if (!(*d = realloc(*d, pos + strlen(s) + 1))) {
355 mp_tmsg(MSGT_CODECCFG,MSGL_FATAL,"Can't allocate memory for comment. ");
356 return 0;
358 strcpy(*d + pos, s);
359 return 1;
362 static short get_cpuflags(char *s)
364 static char *flagstr[] = {
365 "mmx",
366 "sse",
367 "3dnow",
368 NULL
370 int i;
371 short flags = 0;
373 do {
374 for (i = 0; flagstr[i]; i++)
375 if (!strncmp(s, flagstr[i], strlen(flagstr[i])))
376 break;
377 if (!flagstr[i])
378 goto err_out_parse_error;
379 flags |= 1<<i;
380 s += strlen(flagstr[i]);
381 } while (*(s++) == ',');
383 if (*(--s) != '\0')
384 goto err_out_parse_error;
386 return flags;
387 err_out_parse_error:
388 return 0;
391 static FILE *fp;
392 static int line_num = 0;
393 static char *line;
394 static char *token[MAX_NR_TOKEN];
395 static int read_nextline = 1;
397 static int get_token(int min, int max)
399 static int line_pos;
400 int i;
401 char c;
403 if (max >= MAX_NR_TOKEN) {
404 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"get_token(): max >= MAX_MR_TOKEN!");
405 goto out_eof;
408 memset(token, 0x00, sizeof(*token) * max);
410 if (read_nextline) {
411 if (!fgets(line, MAX_LINE_LEN, fp))
412 goto out_eof;
413 line_pos = 0;
414 ++line_num;
415 read_nextline = 0;
417 for (i = 0; i < max; i++) {
418 while (isspace(line[line_pos]))
419 ++line_pos;
420 if (line[line_pos] == '\0' || line[line_pos] == '#' ||
421 line[line_pos] == ';') {
422 read_nextline = 1;
423 if (i >= min)
424 goto out_ok;
425 goto out_eol;
427 token[i] = line + line_pos;
428 c = line[line_pos];
429 if (c == '"' || c == '\'') {
430 token[i]++;
431 while (line[++line_pos] != c && line[line_pos])
432 /* NOTHING */;
433 } else {
434 for (/* NOTHING */; !isspace(line[line_pos]) &&
435 line[line_pos]; line_pos++)
436 /* NOTHING */;
438 if (!line[line_pos]) {
439 read_nextline = 1;
440 if (i >= min - 1)
441 goto out_ok;
442 goto out_eol;
444 line[line_pos] = '\0';
445 line_pos++;
447 out_ok:
448 return i;
449 out_eof:
450 read_nextline = 1;
451 return RET_EOF;
452 out_eol:
453 return RET_EOL;
456 static codecs_t *video_codecs=NULL;
457 static codecs_t *audio_codecs=NULL;
458 static int nr_vcodecs = 0;
459 static int nr_acodecs = 0;
461 int parse_codec_cfg(const char *cfgfile)
463 codecs_t *codec = NULL; // current codec
464 codecs_t **codecsp = NULL;// points to audio_codecs or to video_codecs
465 char *endptr; // strtoul()...
466 int *nr_codecsp;
467 int codec_type; /* TYPE_VIDEO/TYPE_AUDIO */
468 int tmp, i;
470 // in case we call it a second time
471 codecs_uninit_free();
473 nr_vcodecs = 0;
474 nr_acodecs = 0;
476 if(cfgfile==NULL) {
477 #ifdef CODECS2HTML
478 return 0;
479 #else
480 /* following casts are harmless since {video,audio}_codecs will stay
481 * untouched in this case */
482 video_codecs = (codecs_t *)builtin_video_codecs;
483 audio_codecs = (codecs_t *)builtin_audio_codecs;
484 nr_vcodecs = sizeof(builtin_video_codecs)/sizeof(codecs_t);
485 nr_acodecs = sizeof(builtin_audio_codecs)/sizeof(codecs_t);
486 return 1;
487 #endif
490 mp_tmsg(MSGT_CODECCFG,MSGL_V,"Reading %s: ", cfgfile);
492 if ((fp = fopen(cfgfile, "r")) == NULL) {
493 mp_tmsg(MSGT_CODECCFG,MSGL_V,"Can't open '%s': %s\n", cfgfile, strerror(errno));
494 return 0;
497 if ((line = malloc(MAX_LINE_LEN + 1)) == NULL) {
498 mp_tmsg(MSGT_CODECCFG,MSGL_FATAL,"Can't get memory for 'line': %s\n", strerror(errno));
499 return 0;
501 read_nextline = 1;
504 * this only catches release lines at the start of
505 * codecs.conf, before audiocodecs and videocodecs.
507 while ((tmp = get_token(1, 1)) == RET_EOL)
508 /* NOTHING */;
509 if (tmp == RET_EOF)
510 goto out;
511 if (!strcmp(token[0], "release")) {
512 if (get_token(1, 2) < 0)
513 goto err_out_parse_error;
514 tmp = atoi(token[0]);
515 if (tmp < CODEC_CFG_MIN)
516 goto err_out_release_num;
517 codecs_conf_release = tmp;
518 while ((tmp = get_token(1, 1)) == RET_EOL)
519 /* NOTHING */;
520 if (tmp == RET_EOF)
521 goto out;
522 } else
523 goto err_out_release_num;
526 * check if the next block starts with 'audiocodec' or
527 * with 'videocodec'
529 if (!strcmp(token[0], "audiocodec") || !strcmp(token[0], "videocodec"))
530 goto loop_enter;
531 goto err_out_parse_error;
533 while ((tmp = get_token(1, 1)) != RET_EOF) {
534 if (tmp == RET_EOL)
535 continue;
536 if (!strcmp(token[0], "audiocodec") ||
537 !strcmp(token[0], "videocodec")) {
538 if (!validate_codec(codec, codec_type))
539 goto err_out_not_valid;
540 loop_enter:
541 if (*token[0] == 'v') {
542 codec_type = TYPE_VIDEO;
543 nr_codecsp = &nr_vcodecs;
544 codecsp = &video_codecs;
545 } else if (*token[0] == 'a') {
546 codec_type = TYPE_AUDIO;
547 nr_codecsp = &nr_acodecs;
548 codecsp = &audio_codecs;
549 #ifdef DEBUG
550 } else {
551 mp_msg(MSGT_CODECCFG,MSGL_ERR,"picsba\n");
552 goto err_out;
553 #endif
555 if (!(*codecsp = realloc(*codecsp,
556 sizeof(codecs_t) * (*nr_codecsp + 2)))) {
557 mp_tmsg(MSGT_CODECCFG,MSGL_FATAL,"Can't realloc '*codecsp': %s\n", strerror(errno));
558 goto err_out;
560 codec=*codecsp + *nr_codecsp;
561 ++*nr_codecsp;
562 memset(codec,0,sizeof(codecs_t));
563 memset(codec->fourcc, 0xff, sizeof(codec->fourcc));
564 memset(codec->outfmt, 0xff, sizeof(codec->outfmt));
565 memset(codec->infmt, 0xff, sizeof(codec->infmt));
567 if (get_token(1, 1) < 0)
568 goto err_out_parse_error;
569 for (i = 0; i < *nr_codecsp - 1; i++) {
570 if(( (*codecsp)[i].name!=NULL) &&
571 (!strcmp(token[0], (*codecsp)[i].name)) ) {
572 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"Codec name '%s' isn't unique.", token[0]);
573 goto err_out_print_linenum;
576 if (!(codec->name = strdup(token[0]))) {
577 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"Can't strdup -> 'name': %s\n", strerror(errno));
578 goto err_out;
580 } else if (!strcmp(token[0], "info")) {
581 if (codec->info || get_token(1, 1) < 0)
582 goto err_out_parse_error;
583 if (!(codec->info = strdup(token[0]))) {
584 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"Can't strdup -> 'info': %s\n", strerror(errno));
585 goto err_out;
587 } else if (!strcmp(token[0], "comment")) {
588 if (get_token(1, 1) < 0)
589 goto err_out_parse_error;
590 add_comment(token[0], &codec->comment);
591 } else if (!strcmp(token[0], "fourcc")) {
592 if (get_token(1, 2) < 0)
593 goto err_out_parse_error;
594 if (!add_to_fourcc(token[0], token[1],
595 codec->fourcc,
596 codec->fourccmap))
597 goto err_out_print_linenum;
598 } else if (!strcmp(token[0], "format")) {
599 if (get_token(1, 2) < 0)
600 goto err_out_parse_error;
601 if (!add_to_format(token[0], token[1],
602 codec->fourcc,codec->fourccmap))
603 goto err_out_print_linenum;
604 } else if (!strcmp(token[0], "driver")) {
605 if (get_token(1, 1) < 0)
606 goto err_out_parse_error;
607 if (!(codec->drv = strdup(token[0]))) {
608 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"Can't strdup -> 'driver': %s\n", strerror(errno));
609 goto err_out;
611 } else if (!strcmp(token[0], "dll")) {
612 if (get_token(1, 1) < 0)
613 goto err_out_parse_error;
614 if (!(codec->dll = strdup(token[0]))) {
615 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"Can't strdup -> 'dll': %s", strerror(errno));
616 goto err_out;
618 } else if (!strcmp(token[0], "guid")) {
619 if (get_token(11, 11) < 0)
620 goto err_out_parse_error;
621 codec->guid.f1=strtoul(token[0],&endptr,0);
622 if ((*endptr != ',' || *(endptr + 1) != '\0') &&
623 *endptr != '\0')
624 goto err_out_parse_error;
625 codec->guid.f2=strtoul(token[1],&endptr,0);
626 if ((*endptr != ',' || *(endptr + 1) != '\0') &&
627 *endptr != '\0')
628 goto err_out_parse_error;
629 codec->guid.f3=strtoul(token[2],&endptr,0);
630 if ((*endptr != ',' || *(endptr + 1) != '\0') &&
631 *endptr != '\0')
632 goto err_out_parse_error;
633 for (i = 0; i < 8; i++) {
634 codec->guid.f4[i]=strtoul(token[i + 3],&endptr,0);
635 if ((*endptr != ',' || *(endptr + 1) != '\0') &&
636 *endptr != '\0')
637 goto err_out_parse_error;
639 } else if (!strcmp(token[0], "out")) {
640 if (get_token(1, 2) < 0)
641 goto err_out_parse_error;
642 if (!add_to_inout(token[0], token[1], codec->outfmt,
643 codec->outflags))
644 goto err_out_print_linenum;
645 } else if (!strcmp(token[0], "in")) {
646 if (get_token(1, 2) < 0)
647 goto err_out_parse_error;
648 if (!add_to_inout(token[0], token[1], codec->infmt,
649 codec->inflags))
650 goto err_out_print_linenum;
651 } else if (!strcmp(token[0], "flags")) {
652 if (get_token(1, 1) < 0)
653 goto err_out_parse_error;
654 if (!strcmp(token[0], "seekable"))
655 codec->flags |= CODECS_FLAG_SEEKABLE;
656 else if (!strcmp(token[0], "align16"))
657 codec->flags |= CODECS_FLAG_ALIGN16;
658 else
659 goto err_out_parse_error;
660 } else if (!strcmp(token[0], "status")) {
661 if (get_token(1, 1) < 0)
662 goto err_out_parse_error;
663 if (!strcasecmp(token[0], "working"))
664 codec->status = CODECS_STATUS_WORKING;
665 else if (!strcasecmp(token[0], "crashing"))
666 codec->status = CODECS_STATUS_NOT_WORKING;
667 else if (!strcasecmp(token[0], "untested"))
668 codec->status = CODECS_STATUS_UNTESTED;
669 else if (!strcasecmp(token[0], "buggy"))
670 codec->status = CODECS_STATUS_PROBLEMS;
671 else
672 goto err_out_parse_error;
673 } else if (!strcmp(token[0], "cpuflags")) {
674 if (get_token(1, 1) < 0)
675 goto err_out_parse_error;
676 if (!(codec->cpuflags = get_cpuflags(token[0])))
677 goto err_out_parse_error;
678 } else
679 goto err_out_parse_error;
681 if (!validate_codec(codec, codec_type))
682 goto err_out_not_valid;
683 mp_tmsg(MSGT_CODECCFG,MSGL_INFO,"%d audio & %d video codecs\n", nr_acodecs, nr_vcodecs);
684 if(video_codecs) video_codecs[nr_vcodecs].name = NULL;
685 if(audio_codecs) audio_codecs[nr_acodecs].name = NULL;
686 out:
687 free(line);
688 line=NULL;
689 fclose(fp);
690 return 1;
692 err_out_parse_error:
693 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"parse error");
694 err_out_print_linenum:
695 PRINT_LINENUM;
696 err_out:
697 codecs_uninit_free();
699 free(line);
700 line=NULL;
701 line_num = 0;
702 fclose(fp);
703 return 0;
704 err_out_not_valid:
705 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"Codec is not defined correctly.");
706 goto err_out_print_linenum;
707 err_out_release_num:
708 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"This codecs.conf is too old and incompatible with this MPlayer release!");
709 goto err_out_print_linenum;
712 static void codecs_free(codecs_t* codecs,int count) {
713 int i;
714 for ( i = 0; i < count; i++)
715 if ( codecs[i].name ) {
716 free(codecs[i].name);
717 free(codecs[i].info);
718 free(codecs[i].comment);
719 free(codecs[i].dll);
720 free(codecs[i].drv);
722 free(codecs);
725 void codecs_uninit_free(void) {
726 if (video_codecs)
727 codecs_free(video_codecs,nr_vcodecs);
728 video_codecs=NULL;
729 if (audio_codecs)
730 codecs_free(audio_codecs,nr_acodecs);
731 audio_codecs=NULL;
734 codecs_t *find_audio_codec(unsigned int fourcc, unsigned int *fourccmap,
735 codecs_t *start, int force)
737 return find_codec(fourcc, fourccmap, start, 1, force);
740 codecs_t *find_video_codec(unsigned int fourcc, unsigned int *fourccmap,
741 codecs_t *start, int force)
743 return find_codec(fourcc, fourccmap, start, 0, force);
746 codecs_t* find_codec(unsigned int fourcc,unsigned int *fourccmap,
747 codecs_t *start, int audioflag, int force)
749 int i, j;
750 codecs_t *c;
752 #if 0
753 if (start) {
754 for (/* NOTHING */; start->name; start++) {
755 for (j = 0; j < CODECS_MAX_FOURCC; j++) {
756 if (start->fourcc[j] == fourcc) {
757 if (fourccmap)
758 *fourccmap = start->fourccmap[j];
759 return start;
763 } else
764 #endif
766 if (audioflag) {
767 i = nr_acodecs;
768 c = audio_codecs;
769 } else {
770 i = nr_vcodecs;
771 c = video_codecs;
773 if(!i) return NULL;
774 for (/* NOTHING */; i--; c++) {
775 if(start && c<=start) continue;
776 for (j = 0; j < CODECS_MAX_FOURCC; j++) {
777 // FIXME: do NOT hardwire 'null' name here:
778 if (c->fourcc[j]==fourcc || !strcmp(c->drv,"null")) {
779 if (fourccmap)
780 *fourccmap = c->fourccmap[j];
781 return c;
784 if (force) return c;
787 return NULL;
790 void stringset_init(stringset_t *set) {
791 *set = calloc(1, sizeof(char *));
794 void stringset_free(stringset_t *set) {
795 int count = 0;
796 while ((*set)[count]) free((*set)[count++]);
797 free(*set);
798 *set = NULL;
801 void stringset_add(stringset_t *set, const char *str) {
802 int count = 0;
803 while ((*set)[count]) count++;
804 count++;
805 *set = realloc(*set, sizeof(char *) * (count + 1));
806 (*set)[count - 1] = strdup(str);
807 (*set)[count] = NULL;
810 int stringset_test(stringset_t *set, const char *str) {
811 stringset_t s;
812 for (s = *set; *s; s++)
813 if (strcmp(*s, str) == 0)
814 return 1;
815 return 0;
818 void list_codecs(int audioflag){
819 int i;
820 codecs_t *c;
822 if (audioflag) {
823 i = nr_acodecs;
824 c = audio_codecs;
825 mp_msg(MSGT_CODECCFG,MSGL_INFO,"ac: afm: status: info: [lib/dll]\n");
826 } else {
827 i = nr_vcodecs;
828 c = video_codecs;
829 mp_msg(MSGT_CODECCFG,MSGL_INFO,"vc: vfm: status: info: [lib/dll]\n");
831 if(!i) return;
832 for (/* NOTHING */; i--; c++) {
833 char* s="unknown ";
834 switch(c->status){
835 case CODECS_STATUS_WORKING: s="working ";break;
836 case CODECS_STATUS_PROBLEMS: s="problems";break;
837 case CODECS_STATUS_NOT_WORKING: s="crashing";break;
838 case CODECS_STATUS_UNTESTED: s="untested";break;
840 if(c->dll)
841 mp_msg(MSGT_CODECCFG,MSGL_INFO,"%-11s %-9s %s %s [%s]\n",c->name,c->drv,s,c->info,c->dll);
842 else
843 mp_msg(MSGT_CODECCFG,MSGL_INFO,"%-11s %-9s %s %s\n",c->name,c->drv,s,c->info);
848 #ifdef CODECS2HTML
849 static void wrapline(FILE *f2,char *s){
850 int c;
851 if(!s){
852 fprintf(f2,"-");
853 return;
855 while((c=*s++)){
856 if(c==',') fprintf(f2,"<br>"); else fputc(c,f2);
860 static void parsehtml(FILE *f1,FILE *f2,codecs_t *codec){
861 int c,d;
862 while((c=fgetc(f1))>=0){
863 if(c!='%'){
864 fputc(c,f2);
865 continue;
867 d=fgetc(f1);
869 switch(d){
870 case '.':
871 return; // end of section
872 case 'n':
873 wrapline(f2,codec->name); break;
874 case 'i':
875 wrapline(f2,codec->info); break;
876 case 'c':
877 wrapline(f2,codec->comment); break;
878 case 'd':
879 wrapline(f2,codec->dll); break;
880 case 'D':
881 fprintf(f2,"%c",!strcmp(codec->drv,"dshow")?'+':'-'); break;
882 case 'F':
883 for(d=0;d<CODECS_MAX_FOURCC;d++)
884 if(!d || codec->fourcc[d]!=0xFFFFFFFF)
885 fprintf(f2,"%s%.4s",d?"<br>":"",(codec->fourcc[d]==0xFFFFFFFF || codec->fourcc[d]<0x20202020)?!d?"-":"":(char*) &codec->fourcc[d]);
886 break;
887 case 'f':
888 for(d=0;d<CODECS_MAX_FOURCC;d++)
889 if(codec->fourcc[d]!=0xFFFFFFFF)
890 fprintf(f2,"%s0x%X",d?"<br>":"",codec->fourcc[d]);
891 break;
892 case 'Y':
893 for(d=0;d<CODECS_MAX_OUTFMT;d++)
894 if(codec->outfmt[d]!=0xFFFFFFFF){
895 for (c=0; fmt_table[c].name; c++)
896 if(fmt_table[c].num==codec->outfmt[d]) break;
897 if(fmt_table[c].name)
898 fprintf(f2,"%s%s",d?"<br>":"",fmt_table[c].name);
900 break;
901 default:
902 fputc(c,f2);
903 fputc(d,f2);
908 void skiphtml(FILE *f1){
909 int c,d;
910 while((c=fgetc(f1))>=0){
911 if(c!='%'){
912 continue;
914 d=fgetc(f1);
915 if(d=='.') return; // end of section
919 static void print_int_array(const unsigned int* a, int size)
921 printf("{ ");
922 while (size--)
923 if(abs(*a)<256)
924 printf("%d%s", *a++, size?", ":"");
925 else
926 printf("0x%X%s", *a++, size?", ":"");
927 printf(" }");
930 static void print_char_array(const unsigned char* a, int size)
932 printf("{ ");
933 while (size--)
934 if((*a)<10)
935 printf("%d%s", *a++, size?", ":"");
936 else
937 printf("0x%02x%s", *a++, size?", ":"");
938 printf(" }");
941 static void print_string(const char* s)
943 if (!s) printf("NULL");
944 else printf("\"%s\"", s);
947 int main(int argc, char* argv[])
949 codecs_t *cl;
950 FILE *f1;
951 FILE *f2;
952 int c,d,i;
953 int pos;
954 int section=-1;
955 int nr_codecs;
956 int win32=-1;
957 int dshow=-1;
958 int win32ex=-1;
961 * Take path to codecs.conf from command line, or fall back on
962 * etc/codecs.conf
964 if (!(nr_codecs = parse_codec_cfg((argc>1)?argv[1]:"etc/codecs.conf")))
965 exit(1);
966 if (codecs_conf_release < CODEC_CFG_MIN)
967 exit(1);
969 if (argc > 1) {
970 int i, j;
971 const char* nm[2];
972 codecs_t* cod[2];
973 int nr[2];
975 nm[0] = "builtin_video_codecs";
976 cod[0] = video_codecs;
977 nr[0] = nr_vcodecs;
979 nm[1] = "builtin_audio_codecs";
980 cod[1] = audio_codecs;
981 nr[1] = nr_acodecs;
983 printf("/* GENERATED FROM %s, DO NOT EDIT! */\n\n",argv[1]);
984 printf("#include <stddef.h>\n");
985 printf("#include \"codec-cfg.h\"\n\n");
986 printf("#define CODEC_CFG_MIN %i\n\n", codecs_conf_release);
988 for (i=0; i<2; i++) {
989 printf("const codecs_t %s[] = {\n", nm[i]);
990 for (j = 0; j < nr[i]; j++) {
991 printf("{");
993 print_int_array(cod[i][j].fourcc, CODECS_MAX_FOURCC);
994 printf(", /* fourcc */\n");
996 print_int_array(cod[i][j].fourccmap, CODECS_MAX_FOURCC);
997 printf(", /* fourccmap */\n");
999 print_int_array(cod[i][j].outfmt, CODECS_MAX_OUTFMT);
1000 printf(", /* outfmt */\n");
1002 print_char_array(cod[i][j].outflags, CODECS_MAX_OUTFMT);
1003 printf(", /* outflags */\n");
1005 print_int_array(cod[i][j].infmt, CODECS_MAX_INFMT);
1006 printf(", /* infmt */\n");
1008 print_char_array(cod[i][j].inflags, CODECS_MAX_INFMT);
1009 printf(", /* inflags */\n");
1011 print_string(cod[i][j].name); printf(", /* name */\n");
1012 print_string(cod[i][j].info); printf(", /* info */\n");
1013 print_string(cod[i][j].comment); printf(", /* comment */\n");
1014 print_string(cod[i][j].dll); printf(", /* dll */\n");
1015 print_string(cod[i][j].drv); printf(", /* drv */\n");
1017 printf("{ 0x%08lx, %hu, %hu,",
1018 cod[i][j].guid.f1,
1019 cod[i][j].guid.f2,
1020 cod[i][j].guid.f3);
1021 print_char_array(cod[i][j].guid.f4, sizeof(cod[i][j].guid.f4));
1022 printf(" }, /* GUID */\n");
1023 printf("%hd /* flags */, %hd /* status */, %hd /* cpuflags */ }\n",
1024 cod[i][j].flags,
1025 cod[i][j].status,
1026 cod[i][j].cpuflags);
1027 if (j < nr[i]) printf(",\n");
1029 printf("};\n\n");
1031 exit(0);
1034 f1=fopen("DOCS/tech/codecs-in.html","rb"); if(!f1) exit(1);
1035 f2=fopen("DOCS/codecs-status.html","wb"); if(!f2) exit(1);
1037 while((c=fgetc(f1))>=0){
1038 if(c!='%'){
1039 fputc(c,f2);
1040 continue;
1042 d=fgetc(f1);
1043 if(d>='0' && d<='9'){
1044 // begin section
1045 section=d-'0';
1046 //printf("BEGIN %d\n",section);
1047 if(section>=5){
1048 // audio
1049 cl = audio_codecs;
1050 nr_codecs = nr_acodecs;
1051 dshow=7;win32=4;
1052 } else {
1053 // video
1054 cl = video_codecs;
1055 nr_codecs = nr_vcodecs;
1056 dshow=4;win32=2;win32ex=6;
1058 pos=ftell(f1);
1059 for(i=0;i<nr_codecs;i++){
1060 fseek(f1,pos,SEEK_SET);
1061 switch(section){
1062 case 0:
1063 case 5:
1064 if(cl[i].status==CODECS_STATUS_WORKING)
1065 // if(!(!strcmp(cl[i].drv,"vfw") || !strcmp(cl[i].drv,"dshow") || !strcmp(cl[i].drv,"vfwex") || !strcmp(cl[i].drv,"acm")))
1066 parsehtml(f1,f2,&cl[i]);
1067 break;
1068 #if 0
1069 case 1:
1070 case 6:
1071 if(cl[i].status==CODECS_STATUS_WORKING)
1072 if((!strcmp(cl[i].drv,"vfw") || !strcmp(cl[i].drv,"dshow") || !strcmp(cl[i].drv,"vfwex") || !strcmp(cl[i].drv,"acm")))
1073 parsehtml(f1,f2,&cl[i]);
1074 break;
1075 #endif
1076 case 2:
1077 case 7:
1078 if(cl[i].status==CODECS_STATUS_PROBLEMS)
1079 parsehtml(f1,f2,&cl[i]);
1080 break;
1081 case 3:
1082 case 8:
1083 if(cl[i].status==CODECS_STATUS_NOT_WORKING)
1084 parsehtml(f1,f2,&cl[i]);
1085 break;
1086 case 4:
1087 case 9:
1088 if(cl[i].status==CODECS_STATUS_UNTESTED)
1089 parsehtml(f1,f2,&cl[i]);
1090 break;
1091 default:
1092 printf("Warning! unimplemented section: %d\n",section);
1095 fseek(f1,pos,SEEK_SET);
1096 skiphtml(f1);
1098 continue;
1100 fputc(c,f2);
1101 fputc(d,f2);
1104 fclose(f2);
1105 fclose(f1);
1106 return 0;
1109 #endif
1111 #ifdef TESTING
1112 int main(void)
1114 codecs_t *c;
1115 int i,j, nr_codecs, state;
1117 if (!(parse_codec_cfg("etc/codecs.conf")))
1118 return 0;
1119 if (!video_codecs)
1120 printf("no videoconfig.\n");
1121 if (!audio_codecs)
1122 printf("no audioconfig.\n");
1124 printf("videocodecs:\n");
1125 c = video_codecs;
1126 nr_codecs = nr_vcodecs;
1127 state = 0;
1128 next:
1129 if (c) {
1130 printf("number of %scodecs: %d\n", state==0?"video":"audio",
1131 nr_codecs);
1132 for(i=0;i<nr_codecs;i++, c++){
1133 printf("\n============== %scodec %02d ===============\n",
1134 state==0?"video":"audio",i);
1135 printf("name='%s'\n",c->name);
1136 printf("info='%s'\n",c->info);
1137 printf("comment='%s'\n",c->comment);
1138 printf("dll='%s'\n",c->dll);
1139 /* printf("flags=%X driver=%d status=%d cpuflags=%d\n",
1140 c->flags, c->driver, c->status, c->cpuflags); */
1141 printf("flags=%X status=%d cpuflags=%d\n",
1142 c->flags, c->status, c->cpuflags);
1144 for(j=0;j<CODECS_MAX_FOURCC;j++){
1145 if(c->fourcc[j]!=0xFFFFFFFF){
1146 printf("fourcc %02d: %08X (%.4s) ===> %08X (%.4s)\n",j,c->fourcc[j],(char *) &c->fourcc[j],c->fourccmap[j],(char *) &c->fourccmap[j]);
1150 for(j=0;j<CODECS_MAX_OUTFMT;j++){
1151 if(c->outfmt[j]!=0xFFFFFFFF){
1152 printf("outfmt %02d: %08X (%.4s) flags: %d\n",j,c->outfmt[j],(char *) &c->outfmt[j],c->outflags[j]);
1156 for(j=0;j<CODECS_MAX_INFMT;j++){
1157 if(c->infmt[j]!=0xFFFFFFFF){
1158 printf("infmt %02d: %08X (%.4s) flags: %d\n",j,c->infmt[j],(char *) &c->infmt[j],c->inflags[j]);
1162 printf("GUID: %08lX %04X %04X",c->guid.f1,c->guid.f2,c->guid.f3);
1163 for(j=0;j<8;j++) printf(" %02X",c->guid.f4[j]);
1164 printf("\n");
1169 if (!state) {
1170 printf("audiocodecs:\n");
1171 c = audio_codecs;
1172 nr_codecs = nr_acodecs;
1173 state = 1;
1174 goto next;
1176 return 0;
1179 #endif