Copyright, LICENSE: change binary license to GPL 3
[mplayer.git] / codec-cfg.c
bloba7d958ebc15ea9ad005d94ce405c0a0cccf9f54b
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 video_codecs = builtin_video_codecs;
481 audio_codecs = builtin_audio_codecs;
482 nr_vcodecs = sizeof(builtin_video_codecs)/sizeof(codecs_t);
483 nr_acodecs = sizeof(builtin_audio_codecs)/sizeof(codecs_t);
484 return 1;
485 #endif
488 mp_tmsg(MSGT_CODECCFG,MSGL_V,"Reading %s: ", cfgfile);
490 if ((fp = fopen(cfgfile, "r")) == NULL) {
491 mp_tmsg(MSGT_CODECCFG,MSGL_V,"Can't open '%s': %s\n", cfgfile, strerror(errno));
492 return 0;
495 if ((line = malloc(MAX_LINE_LEN + 1)) == NULL) {
496 mp_tmsg(MSGT_CODECCFG,MSGL_FATAL,"Can't get memory for 'line': %s\n", strerror(errno));
497 return 0;
499 read_nextline = 1;
502 * this only catches release lines at the start of
503 * codecs.conf, before audiocodecs and videocodecs.
505 while ((tmp = get_token(1, 1)) == RET_EOL)
506 /* NOTHING */;
507 if (tmp == RET_EOF)
508 goto out;
509 if (!strcmp(token[0], "release")) {
510 if (get_token(1, 2) < 0)
511 goto err_out_parse_error;
512 tmp = atoi(token[0]);
513 if (tmp < CODEC_CFG_MIN)
514 goto err_out_release_num;
515 codecs_conf_release = tmp;
516 while ((tmp = get_token(1, 1)) == RET_EOL)
517 /* NOTHING */;
518 if (tmp == RET_EOF)
519 goto out;
520 } else
521 goto err_out_release_num;
524 * check if the next block starts with 'audiocodec' or
525 * with 'videocodec'
527 if (!strcmp(token[0], "audiocodec") || !strcmp(token[0], "videocodec"))
528 goto loop_enter;
529 goto err_out_parse_error;
531 while ((tmp = get_token(1, 1)) != RET_EOF) {
532 if (tmp == RET_EOL)
533 continue;
534 if (!strcmp(token[0], "audiocodec") ||
535 !strcmp(token[0], "videocodec")) {
536 if (!validate_codec(codec, codec_type))
537 goto err_out_not_valid;
538 loop_enter:
539 if (*token[0] == 'v') {
540 codec_type = TYPE_VIDEO;
541 nr_codecsp = &nr_vcodecs;
542 codecsp = &video_codecs;
543 } else if (*token[0] == 'a') {
544 codec_type = TYPE_AUDIO;
545 nr_codecsp = &nr_acodecs;
546 codecsp = &audio_codecs;
547 #ifdef DEBUG
548 } else {
549 mp_msg(MSGT_CODECCFG,MSGL_ERR,"picsba\n");
550 goto err_out;
551 #endif
553 if (!(*codecsp = realloc(*codecsp,
554 sizeof(codecs_t) * (*nr_codecsp + 2)))) {
555 mp_tmsg(MSGT_CODECCFG,MSGL_FATAL,"Can't realloc '*codecsp': %s\n", strerror(errno));
556 goto err_out;
558 codec=*codecsp + *nr_codecsp;
559 ++*nr_codecsp;
560 memset(codec,0,sizeof(codecs_t));
561 memset(codec->fourcc, 0xff, sizeof(codec->fourcc));
562 memset(codec->outfmt, 0xff, sizeof(codec->outfmt));
563 memset(codec->infmt, 0xff, sizeof(codec->infmt));
565 if (get_token(1, 1) < 0)
566 goto err_out_parse_error;
567 for (i = 0; i < *nr_codecsp - 1; i++) {
568 if(( (*codecsp)[i].name!=NULL) &&
569 (!strcmp(token[0], (*codecsp)[i].name)) ) {
570 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"Codec name '%s' isn't unique.", token[0]);
571 goto err_out_print_linenum;
574 if (!(codec->name = strdup(token[0]))) {
575 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"Can't strdup -> 'name': %s\n", strerror(errno));
576 goto err_out;
578 } else if (!strcmp(token[0], "info")) {
579 if (codec->info || get_token(1, 1) < 0)
580 goto err_out_parse_error;
581 if (!(codec->info = strdup(token[0]))) {
582 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"Can't strdup -> 'info': %s\n", strerror(errno));
583 goto err_out;
585 } else if (!strcmp(token[0], "comment")) {
586 if (get_token(1, 1) < 0)
587 goto err_out_parse_error;
588 add_comment(token[0], &codec->comment);
589 } else if (!strcmp(token[0], "fourcc")) {
590 if (get_token(1, 2) < 0)
591 goto err_out_parse_error;
592 if (!add_to_fourcc(token[0], token[1],
593 codec->fourcc,
594 codec->fourccmap))
595 goto err_out_print_linenum;
596 } else if (!strcmp(token[0], "format")) {
597 if (get_token(1, 2) < 0)
598 goto err_out_parse_error;
599 if (!add_to_format(token[0], token[1],
600 codec->fourcc,codec->fourccmap))
601 goto err_out_print_linenum;
602 } else if (!strcmp(token[0], "driver")) {
603 if (get_token(1, 1) < 0)
604 goto err_out_parse_error;
605 if (!(codec->drv = strdup(token[0]))) {
606 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"Can't strdup -> 'driver': %s\n", strerror(errno));
607 goto err_out;
609 } else if (!strcmp(token[0], "dll")) {
610 if (get_token(1, 1) < 0)
611 goto err_out_parse_error;
612 if (!(codec->dll = strdup(token[0]))) {
613 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"Can't strdup -> 'dll': %s", strerror(errno));
614 goto err_out;
616 } else if (!strcmp(token[0], "guid")) {
617 if (get_token(11, 11) < 0)
618 goto err_out_parse_error;
619 codec->guid.f1=strtoul(token[0],&endptr,0);
620 if ((*endptr != ',' || *(endptr + 1) != '\0') &&
621 *endptr != '\0')
622 goto err_out_parse_error;
623 codec->guid.f2=strtoul(token[1],&endptr,0);
624 if ((*endptr != ',' || *(endptr + 1) != '\0') &&
625 *endptr != '\0')
626 goto err_out_parse_error;
627 codec->guid.f3=strtoul(token[2],&endptr,0);
628 if ((*endptr != ',' || *(endptr + 1) != '\0') &&
629 *endptr != '\0')
630 goto err_out_parse_error;
631 for (i = 0; i < 8; i++) {
632 codec->guid.f4[i]=strtoul(token[i + 3],&endptr,0);
633 if ((*endptr != ',' || *(endptr + 1) != '\0') &&
634 *endptr != '\0')
635 goto err_out_parse_error;
637 } else if (!strcmp(token[0], "out")) {
638 if (get_token(1, 2) < 0)
639 goto err_out_parse_error;
640 if (!add_to_inout(token[0], token[1], codec->outfmt,
641 codec->outflags))
642 goto err_out_print_linenum;
643 } else if (!strcmp(token[0], "in")) {
644 if (get_token(1, 2) < 0)
645 goto err_out_parse_error;
646 if (!add_to_inout(token[0], token[1], codec->infmt,
647 codec->inflags))
648 goto err_out_print_linenum;
649 } else if (!strcmp(token[0], "flags")) {
650 if (get_token(1, 1) < 0)
651 goto err_out_parse_error;
652 if (!strcmp(token[0], "seekable"))
653 codec->flags |= CODECS_FLAG_SEEKABLE;
654 else if (!strcmp(token[0], "align16"))
655 codec->flags |= CODECS_FLAG_ALIGN16;
656 else
657 goto err_out_parse_error;
658 } else if (!strcmp(token[0], "status")) {
659 if (get_token(1, 1) < 0)
660 goto err_out_parse_error;
661 if (!strcasecmp(token[0], "working"))
662 codec->status = CODECS_STATUS_WORKING;
663 else if (!strcasecmp(token[0], "crashing"))
664 codec->status = CODECS_STATUS_NOT_WORKING;
665 else if (!strcasecmp(token[0], "untested"))
666 codec->status = CODECS_STATUS_UNTESTED;
667 else if (!strcasecmp(token[0], "buggy"))
668 codec->status = CODECS_STATUS_PROBLEMS;
669 else
670 goto err_out_parse_error;
671 } else if (!strcmp(token[0], "cpuflags")) {
672 if (get_token(1, 1) < 0)
673 goto err_out_parse_error;
674 if (!(codec->cpuflags = get_cpuflags(token[0])))
675 goto err_out_parse_error;
676 } else
677 goto err_out_parse_error;
679 if (!validate_codec(codec, codec_type))
680 goto err_out_not_valid;
681 mp_tmsg(MSGT_CODECCFG,MSGL_INFO,"%d audio & %d video codecs\n", nr_acodecs, nr_vcodecs);
682 if(video_codecs) video_codecs[nr_vcodecs].name = NULL;
683 if(audio_codecs) audio_codecs[nr_acodecs].name = NULL;
684 out:
685 free(line);
686 line=NULL;
687 fclose(fp);
688 return 1;
690 err_out_parse_error:
691 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"parse error");
692 err_out_print_linenum:
693 PRINT_LINENUM;
694 err_out:
695 codecs_uninit_free();
697 free(line);
698 line=NULL;
699 line_num = 0;
700 fclose(fp);
701 return 0;
702 err_out_not_valid:
703 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"Codec is not defined correctly.");
704 goto err_out_print_linenum;
705 err_out_release_num:
706 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"This codecs.conf is too old and incompatible with this MPlayer release!");
707 goto err_out_print_linenum;
710 static void codecs_free(codecs_t* codecs,int count) {
711 int i;
712 for ( i = 0; i < count; i++)
713 if ( codecs[i].name ) {
714 free(codecs[i].name);
715 free(codecs[i].info);
716 free(codecs[i].comment);
717 free(codecs[i].dll);
718 free(codecs[i].drv);
720 free(codecs);
723 void codecs_uninit_free(void) {
724 if (video_codecs)
725 codecs_free(video_codecs,nr_vcodecs);
726 video_codecs=NULL;
727 if (audio_codecs)
728 codecs_free(audio_codecs,nr_acodecs);
729 audio_codecs=NULL;
732 codecs_t *find_audio_codec(unsigned int fourcc, unsigned int *fourccmap,
733 codecs_t *start, int force)
735 return find_codec(fourcc, fourccmap, start, 1, force);
738 codecs_t *find_video_codec(unsigned int fourcc, unsigned int *fourccmap,
739 codecs_t *start, int force)
741 return find_codec(fourcc, fourccmap, start, 0, force);
744 codecs_t* find_codec(unsigned int fourcc,unsigned int *fourccmap,
745 codecs_t *start, int audioflag, int force)
747 int i, j;
748 codecs_t *c;
750 #if 0
751 if (start) {
752 for (/* NOTHING */; start->name; start++) {
753 for (j = 0; j < CODECS_MAX_FOURCC; j++) {
754 if (start->fourcc[j] == fourcc) {
755 if (fourccmap)
756 *fourccmap = start->fourccmap[j];
757 return start;
761 } else
762 #endif
764 if (audioflag) {
765 i = nr_acodecs;
766 c = audio_codecs;
767 } else {
768 i = nr_vcodecs;
769 c = video_codecs;
771 if(!i) return NULL;
772 for (/* NOTHING */; i--; c++) {
773 if(start && c<=start) continue;
774 for (j = 0; j < CODECS_MAX_FOURCC; j++) {
775 // FIXME: do NOT hardwire 'null' name here:
776 if (c->fourcc[j]==fourcc || !strcmp(c->drv,"null")) {
777 if (fourccmap)
778 *fourccmap = c->fourccmap[j];
779 return c;
782 if (force) return c;
785 return NULL;
788 void stringset_init(stringset_t *set) {
789 *set = calloc(1, sizeof(char *));
792 void stringset_free(stringset_t *set) {
793 int count = 0;
794 while ((*set)[count]) free((*set)[count++]);
795 free(*set);
796 *set = NULL;
799 void stringset_add(stringset_t *set, const char *str) {
800 int count = 0;
801 while ((*set)[count]) count++;
802 count++;
803 *set = realloc(*set, sizeof(char *) * (count + 1));
804 (*set)[count - 1] = strdup(str);
805 (*set)[count] = NULL;
808 int stringset_test(stringset_t *set, const char *str) {
809 stringset_t s;
810 for (s = *set; *s; s++)
811 if (strcmp(*s, str) == 0)
812 return 1;
813 return 0;
816 void list_codecs(int audioflag){
817 int i;
818 codecs_t *c;
820 if (audioflag) {
821 i = nr_acodecs;
822 c = audio_codecs;
823 mp_msg(MSGT_CODECCFG,MSGL_INFO,"ac: afm: status: info: [lib/dll]\n");
824 } else {
825 i = nr_vcodecs;
826 c = video_codecs;
827 mp_msg(MSGT_CODECCFG,MSGL_INFO,"vc: vfm: status: info: [lib/dll]\n");
829 if(!i) return;
830 for (/* NOTHING */; i--; c++) {
831 char* s="unknown ";
832 switch(c->status){
833 case CODECS_STATUS_WORKING: s="working ";break;
834 case CODECS_STATUS_PROBLEMS: s="problems";break;
835 case CODECS_STATUS_NOT_WORKING: s="crashing";break;
836 case CODECS_STATUS_UNTESTED: s="untested";break;
838 if(c->dll)
839 mp_msg(MSGT_CODECCFG,MSGL_INFO,"%-11s %-9s %s %s [%s]\n",c->name,c->drv,s,c->info,c->dll);
840 else
841 mp_msg(MSGT_CODECCFG,MSGL_INFO,"%-11s %-9s %s %s\n",c->name,c->drv,s,c->info);
846 #ifdef CODECS2HTML
847 static void wrapline(FILE *f2,char *s){
848 int c;
849 if(!s){
850 fprintf(f2,"-");
851 return;
853 while((c=*s++)){
854 if(c==',') fprintf(f2,"<br>"); else fputc(c,f2);
858 static void parsehtml(FILE *f1,FILE *f2,codecs_t *codec){
859 int c,d;
860 while((c=fgetc(f1))>=0){
861 if(c!='%'){
862 fputc(c,f2);
863 continue;
865 d=fgetc(f1);
867 switch(d){
868 case '.':
869 return; // end of section
870 case 'n':
871 wrapline(f2,codec->name); break;
872 case 'i':
873 wrapline(f2,codec->info); break;
874 case 'c':
875 wrapline(f2,codec->comment); break;
876 case 'd':
877 wrapline(f2,codec->dll); break;
878 case 'D':
879 fprintf(f2,"%c",!strcmp(codec->drv,"dshow")?'+':'-'); break;
880 case 'F':
881 for(d=0;d<CODECS_MAX_FOURCC;d++)
882 if(!d || codec->fourcc[d]!=0xFFFFFFFF)
883 fprintf(f2,"%s%.4s",d?"<br>":"",(codec->fourcc[d]==0xFFFFFFFF || codec->fourcc[d]<0x20202020)?!d?"-":"":(char*) &codec->fourcc[d]);
884 break;
885 case 'f':
886 for(d=0;d<CODECS_MAX_FOURCC;d++)
887 if(codec->fourcc[d]!=0xFFFFFFFF)
888 fprintf(f2,"%s0x%X",d?"<br>":"",codec->fourcc[d]);
889 break;
890 case 'Y':
891 for(d=0;d<CODECS_MAX_OUTFMT;d++)
892 if(codec->outfmt[d]!=0xFFFFFFFF){
893 for (c=0; fmt_table[c].name; c++)
894 if(fmt_table[c].num==codec->outfmt[d]) break;
895 if(fmt_table[c].name)
896 fprintf(f2,"%s%s",d?"<br>":"",fmt_table[c].name);
898 break;
899 default:
900 fputc(c,f2);
901 fputc(d,f2);
906 void skiphtml(FILE *f1){
907 int c,d;
908 while((c=fgetc(f1))>=0){
909 if(c!='%'){
910 continue;
912 d=fgetc(f1);
913 if(d=='.') return; // end of section
917 static void print_int_array(const unsigned int* a, int size)
919 printf("{ ");
920 while (size--)
921 if(abs(*a)<256)
922 printf("%d%s", *a++, size?", ":"");
923 else
924 printf("0x%X%s", *a++, size?", ":"");
925 printf(" }");
928 static void print_char_array(const unsigned char* a, int size)
930 printf("{ ");
931 while (size--)
932 if((*a)<10)
933 printf("%d%s", *a++, size?", ":"");
934 else
935 printf("0x%02x%s", *a++, size?", ":"");
936 printf(" }");
939 static void print_string(const char* s)
941 if (!s) printf("NULL");
942 else printf("\"%s\"", s);
945 int main(int argc, char* argv[])
947 codecs_t *cl;
948 FILE *f1;
949 FILE *f2;
950 int c,d,i;
951 int pos;
952 int section=-1;
953 int nr_codecs;
954 int win32=-1;
955 int dshow=-1;
956 int win32ex=-1;
959 * Take path to codecs.conf from command line, or fall back on
960 * etc/codecs.conf
962 if (!(nr_codecs = parse_codec_cfg((argc>1)?argv[1]:"etc/codecs.conf")))
963 exit(1);
964 if (codecs_conf_release < CODEC_CFG_MIN)
965 exit(1);
967 if (argc > 1) {
968 int i, j;
969 const char* nm[2];
970 codecs_t* cod[2];
971 int nr[2];
973 nm[0] = "builtin_video_codecs";
974 cod[0] = video_codecs;
975 nr[0] = nr_vcodecs;
977 nm[1] = "builtin_audio_codecs";
978 cod[1] = audio_codecs;
979 nr[1] = nr_acodecs;
981 printf("/* GENERATED FROM %s, DO NOT EDIT! */\n\n",argv[1]);
982 printf("#include <stddef.h>\n");
983 printf("#include \"codec-cfg.h\"\n\n");
984 printf("#define CODEC_CFG_MIN %i\n\n", codecs_conf_release);
986 for (i=0; i<2; i++) {
987 printf("const codecs_t %s[] = {\n", nm[i]);
988 for (j = 0; j < nr[i]; j++) {
989 printf("{");
991 print_int_array(cod[i][j].fourcc, CODECS_MAX_FOURCC);
992 printf(", /* fourcc */\n");
994 print_int_array(cod[i][j].fourccmap, CODECS_MAX_FOURCC);
995 printf(", /* fourccmap */\n");
997 print_int_array(cod[i][j].outfmt, CODECS_MAX_OUTFMT);
998 printf(", /* outfmt */\n");
1000 print_char_array(cod[i][j].outflags, CODECS_MAX_OUTFMT);
1001 printf(", /* outflags */\n");
1003 print_int_array(cod[i][j].infmt, CODECS_MAX_INFMT);
1004 printf(", /* infmt */\n");
1006 print_char_array(cod[i][j].inflags, CODECS_MAX_INFMT);
1007 printf(", /* inflags */\n");
1009 print_string(cod[i][j].name); printf(", /* name */\n");
1010 print_string(cod[i][j].info); printf(", /* info */\n");
1011 print_string(cod[i][j].comment); printf(", /* comment */\n");
1012 print_string(cod[i][j].dll); printf(", /* dll */\n");
1013 print_string(cod[i][j].drv); printf(", /* drv */\n");
1015 printf("{ 0x%08lx, %hu, %hu,",
1016 cod[i][j].guid.f1,
1017 cod[i][j].guid.f2,
1018 cod[i][j].guid.f3);
1019 print_char_array(cod[i][j].guid.f4, sizeof(cod[i][j].guid.f4));
1020 printf(" }, /* GUID */\n");
1021 printf("%hd /* flags */, %hd /* status */, %hd /* cpuflags */ }\n",
1022 cod[i][j].flags,
1023 cod[i][j].status,
1024 cod[i][j].cpuflags);
1025 if (j < nr[i]) printf(",\n");
1027 printf("};\n\n");
1029 exit(0);
1032 f1=fopen("DOCS/tech/codecs-in.html","rb"); if(!f1) exit(1);
1033 f2=fopen("DOCS/codecs-status.html","wb"); if(!f2) exit(1);
1035 while((c=fgetc(f1))>=0){
1036 if(c!='%'){
1037 fputc(c,f2);
1038 continue;
1040 d=fgetc(f1);
1041 if(d>='0' && d<='9'){
1042 // begin section
1043 section=d-'0';
1044 //printf("BEGIN %d\n",section);
1045 if(section>=5){
1046 // audio
1047 cl = audio_codecs;
1048 nr_codecs = nr_acodecs;
1049 dshow=7;win32=4;
1050 } else {
1051 // video
1052 cl = video_codecs;
1053 nr_codecs = nr_vcodecs;
1054 dshow=4;win32=2;win32ex=6;
1056 pos=ftell(f1);
1057 for(i=0;i<nr_codecs;i++){
1058 fseek(f1,pos,SEEK_SET);
1059 switch(section){
1060 case 0:
1061 case 5:
1062 if(cl[i].status==CODECS_STATUS_WORKING)
1063 // if(!(!strcmp(cl[i].drv,"vfw") || !strcmp(cl[i].drv,"dshow") || !strcmp(cl[i].drv,"vfwex") || !strcmp(cl[i].drv,"acm")))
1064 parsehtml(f1,f2,&cl[i]);
1065 break;
1066 #if 0
1067 case 1:
1068 case 6:
1069 if(cl[i].status==CODECS_STATUS_WORKING)
1070 if((!strcmp(cl[i].drv,"vfw") || !strcmp(cl[i].drv,"dshow") || !strcmp(cl[i].drv,"vfwex") || !strcmp(cl[i].drv,"acm")))
1071 parsehtml(f1,f2,&cl[i]);
1072 break;
1073 #endif
1074 case 2:
1075 case 7:
1076 if(cl[i].status==CODECS_STATUS_PROBLEMS)
1077 parsehtml(f1,f2,&cl[i]);
1078 break;
1079 case 3:
1080 case 8:
1081 if(cl[i].status==CODECS_STATUS_NOT_WORKING)
1082 parsehtml(f1,f2,&cl[i]);
1083 break;
1084 case 4:
1085 case 9:
1086 if(cl[i].status==CODECS_STATUS_UNTESTED)
1087 parsehtml(f1,f2,&cl[i]);
1088 break;
1089 default:
1090 printf("Warning! unimplemented section: %d\n",section);
1093 fseek(f1,pos,SEEK_SET);
1094 skiphtml(f1);
1096 continue;
1098 fputc(c,f2);
1099 fputc(d,f2);
1102 fclose(f2);
1103 fclose(f1);
1104 return 0;
1107 #endif
1109 #ifdef TESTING
1110 int main(void)
1112 codecs_t *c;
1113 int i,j, nr_codecs, state;
1115 if (!(parse_codec_cfg("etc/codecs.conf")))
1116 return 0;
1117 if (!video_codecs)
1118 printf("no videoconfig.\n");
1119 if (!audio_codecs)
1120 printf("no audioconfig.\n");
1122 printf("videocodecs:\n");
1123 c = video_codecs;
1124 nr_codecs = nr_vcodecs;
1125 state = 0;
1126 next:
1127 if (c) {
1128 printf("number of %scodecs: %d\n", state==0?"video":"audio",
1129 nr_codecs);
1130 for(i=0;i<nr_codecs;i++, c++){
1131 printf("\n============== %scodec %02d ===============\n",
1132 state==0?"video":"audio",i);
1133 printf("name='%s'\n",c->name);
1134 printf("info='%s'\n",c->info);
1135 printf("comment='%s'\n",c->comment);
1136 printf("dll='%s'\n",c->dll);
1137 /* printf("flags=%X driver=%d status=%d cpuflags=%d\n",
1138 c->flags, c->driver, c->status, c->cpuflags); */
1139 printf("flags=%X status=%d cpuflags=%d\n",
1140 c->flags, c->status, c->cpuflags);
1142 for(j=0;j<CODECS_MAX_FOURCC;j++){
1143 if(c->fourcc[j]!=0xFFFFFFFF){
1144 printf("fourcc %02d: %08X (%.4s) ===> %08X (%.4s)\n",j,c->fourcc[j],(char *) &c->fourcc[j],c->fourccmap[j],(char *) &c->fourccmap[j]);
1148 for(j=0;j<CODECS_MAX_OUTFMT;j++){
1149 if(c->outfmt[j]!=0xFFFFFFFF){
1150 printf("outfmt %02d: %08X (%.4s) flags: %d\n",j,c->outfmt[j],(char *) &c->outfmt[j],c->outflags[j]);
1154 for(j=0;j<CODECS_MAX_INFMT;j++){
1155 if(c->infmt[j]!=0xFFFFFFFF){
1156 printf("infmt %02d: %08X (%.4s) flags: %d\n",j,c->infmt[j],(char *) &c->infmt[j],c->inflags[j]);
1160 printf("GUID: %08lX %04X %04X",c->guid.f1,c->guid.f2,c->guid.f3);
1161 for(j=0;j<8;j++) printf(" %02X",c->guid.f4[j]);
1162 printf("\n");
1167 if (!state) {
1168 printf("audiocodecs:\n");
1169 c = audio_codecs;
1170 nr_codecs = nr_acodecs;
1171 state = 1;
1172 goto next;
1174 return 0;
1177 #endif