options: Allow a larger range for -aspect
[mplayer.git] / codec-cfg.c
blob5237d7fadd753e57426b080cea1153bcc5e936a9
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 <libavutil/avutil.h>
58 #include "libmpcodecs/img_format.h"
59 #include "codec-cfg.h"
61 #ifdef CODECS2HTML
62 #define CODEC_CFG_MIN 20100000
63 #else
64 #include "codecs.conf.h"
65 #endif
67 #define mmioFOURCC( ch0, ch1, ch2, ch3 ) \
68 ( (uint32_t)(uint8_t)(ch0) | ( (uint32_t)(uint8_t)(ch1) << 8 ) | \
69 ( (uint32_t)(uint8_t)(ch2) << 16 ) | ( (uint32_t)(uint8_t)(ch3) << 24 ) )
71 #define PRINT_LINENUM mp_msg(MSGT_CODECCFG,MSGL_ERR," at line %d\n", line_num)
73 #define MAX_NR_TOKEN 16
75 #define MAX_LINE_LEN 1000
77 #define RET_EOF -1
78 #define RET_EOL -2
80 #define TYPE_VIDEO 0
81 #define TYPE_AUDIO 1
83 static int codecs_conf_release;
84 char * codecs_file = NULL;
86 static int add_to_fourcc(char *s, char *alias, unsigned int *fourcc,
87 unsigned int *map)
89 int i, j, freeslots;
90 unsigned int tmp;
92 /* find first unused slot */
93 for (i = 0; i < CODECS_MAX_FOURCC && fourcc[i] != 0xffffffff; i++)
94 /* NOTHING */;
95 freeslots = CODECS_MAX_FOURCC - i;
96 if (!freeslots)
97 goto err_out_too_many;
99 do {
100 tmp = MKTAG(s[0], s[1], s[2], s[3]);
101 for (j = 0; j < i; j++)
102 if (tmp == fourcc[j])
103 goto err_out_duplicated;
104 fourcc[i] = tmp;
105 map[i] = alias ? MKTAG(alias[0], alias[1], alias[2], alias[3]) : tmp;
106 s += 4;
107 i++;
108 } while ((*(s++) == ',') && --freeslots);
110 if (!freeslots)
111 goto err_out_too_many;
112 if (*(--s) != '\0')
113 goto err_out_parse_error;
114 return 1;
115 err_out_duplicated:
116 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"duplicated FourCC");
117 return 0;
118 err_out_too_many:
119 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"too many FourCCs/formats...");
120 return 0;
121 err_out_parse_error:
122 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"parse error");
123 return 0;
126 static int add_to_format(char *s, char *alias,unsigned int *fourcc, unsigned int *fourccmap)
128 int i, j;
129 char *endptr;
131 /* find first unused slot */
132 for (i = 0; i < CODECS_MAX_FOURCC && fourcc[i] != 0xffffffff; i++)
133 /* NOTHING */;
134 if (i == CODECS_MAX_FOURCC) {
135 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"too many FourCCs/formats...");
136 return 0;
139 fourcc[i]=strtoul(s,&endptr,0);
140 if (*endptr != '\0') {
141 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"parse error (format ID not a number?)");
142 return 0;
145 if(alias){
146 fourccmap[i]=strtoul(alias,&endptr,0);
147 if (*endptr != '\0') {
148 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"parse error (format ID alias not a number?)");
149 return 0;
151 } else
152 fourccmap[i]=fourcc[i];
154 for (j = 0; j < i; j++)
155 if (fourcc[j] == fourcc[i]) {
156 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"duplicated format ID");
157 return 0;
160 return 1;
163 static const struct {
164 const char *name;
165 const unsigned int num;
166 } fmt_table[] = {
167 // note: due to parser deficiencies/simplicity, if one format
168 // name matches the beginning of another, the longer one _must_
169 // come first in this list.
170 {"YV12", IMGFMT_YV12},
171 {"I420", IMGFMT_I420},
172 {"IYUV", IMGFMT_IYUV},
173 {"NV12", IMGFMT_NV12},
174 {"NV21", IMGFMT_NV21},
175 {"YVU9", IMGFMT_YVU9},
176 {"IF09", IMGFMT_IF09},
177 {"444P16LE", IMGFMT_444P16_LE},
178 {"444P16BE", IMGFMT_444P16_BE},
179 {"422P16LE", IMGFMT_422P16_LE},
180 {"422P16BE", IMGFMT_422P16_BE},
181 {"420P16LE", IMGFMT_420P16_LE},
182 {"420P16BE", IMGFMT_420P16_BE},
183 {"444P16", IMGFMT_444P16},
184 {"444P10", IMGFMT_444P10},
185 {"444P9", IMGFMT_444P9},
186 {"422P16", IMGFMT_422P16},
187 {"422P10", IMGFMT_422P10},
188 {"420P16", IMGFMT_420P16},
189 {"420P10", IMGFMT_420P10},
190 {"420P9", IMGFMT_420P9},
191 {"420A", IMGFMT_420A},
192 {"444P", IMGFMT_444P},
193 {"422P", IMGFMT_422P},
194 {"411P", IMGFMT_411P},
195 {"440P", IMGFMT_440P},
196 {"Y800", IMGFMT_Y800},
197 {"Y8", IMGFMT_Y8},
199 {"YUY2", IMGFMT_YUY2},
200 {"UYVY", IMGFMT_UYVY},
201 {"YVYU", IMGFMT_YVYU},
203 {"RGB48LE", IMGFMT_RGB48LE},
204 {"RGB48BE", IMGFMT_RGB48BE},
205 {"RGB4", IMGFMT_RGB4},
206 {"RGB8", IMGFMT_RGB8},
207 {"RGB15", IMGFMT_RGB15},
208 {"RGB16", IMGFMT_RGB16},
209 {"RGB24", IMGFMT_RGB24},
210 {"RGB32", IMGFMT_RGB32},
211 {"BGR4", IMGFMT_BGR4},
212 {"BGR8", IMGFMT_BGR8},
213 {"BGR15", IMGFMT_BGR15},
214 {"BGR16", IMGFMT_BGR16},
215 {"BGR24", IMGFMT_BGR24},
216 {"BGR32", IMGFMT_BGR32},
217 {"RGB1", IMGFMT_RGB1},
218 {"BGR1", IMGFMT_BGR1},
220 {"MPES", IMGFMT_MPEGPES},
222 {"IDCT_MPEG2",IMGFMT_XVMC_IDCT_MPEG2},
223 {"MOCO_MPEG2",IMGFMT_XVMC_MOCO_MPEG2},
225 {"VDPAU_MPEG1",IMGFMT_VDPAU_MPEG1},
226 {"VDPAU_MPEG2",IMGFMT_VDPAU_MPEG2},
227 {"VDPAU_H264",IMGFMT_VDPAU_H264},
228 {"VDPAU_WMV3",IMGFMT_VDPAU_WMV3},
229 {"VDPAU_VC1",IMGFMT_VDPAU_VC1},
230 {"VDPAU_MPEG4",IMGFMT_VDPAU_MPEG4},
232 {NULL, 0}
236 static int add_to_inout(char *sfmt, char *sflags, unsigned int *outfmt,
237 unsigned char *outflags)
240 static char *flagstr[] = {
241 "flip",
242 "noflip",
243 "yuvhack",
244 "query",
245 "static",
246 NULL
249 int i, j, freeslots;
250 unsigned char flags;
252 for (i = 0; i < CODECS_MAX_OUTFMT && outfmt[i] != 0xffffffff; i++)
253 /* NOTHING */;
254 freeslots = CODECS_MAX_OUTFMT - i;
255 if (!freeslots)
256 goto err_out_too_many;
258 flags = 0;
259 if(sflags) {
260 do {
261 for (j = 0; flagstr[j] != NULL; j++)
262 if (!strncmp(sflags, flagstr[j],
263 strlen(flagstr[j])))
264 break;
265 if (flagstr[j] == NULL)
266 goto err_out_parse_error;
267 flags|=(1<<j);
268 sflags+=strlen(flagstr[j]);
269 } while (*(sflags++) == ',');
271 if (*(--sflags) != '\0')
272 goto err_out_parse_error;
275 do {
276 for (j = 0; fmt_table[j].name != NULL; j++)
277 if (!strncmp(sfmt, fmt_table[j].name, strlen(fmt_table[j].name)))
278 break;
279 if (fmt_table[j].name == NULL)
280 goto err_out_parse_error;
281 outfmt[i] = fmt_table[j].num;
282 outflags[i] = flags;
283 ++i;
284 sfmt+=strlen(fmt_table[j].name);
285 } while ((*(sfmt++) == ',') && --freeslots);
287 if (!freeslots)
288 goto err_out_too_many;
290 if (*(--sfmt) != '\0')
291 goto err_out_parse_error;
293 return 1;
294 err_out_too_many:
295 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"too many out...");
296 return 0;
297 err_out_parse_error:
298 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"parse error");
299 return 0;
302 static int validate_codec(codecs_t *c, int type)
304 unsigned int i;
305 char *tmp_name = c->name;
307 for (i = 0; i < strlen(tmp_name) && isalnum(tmp_name[i]); i++)
308 /* NOTHING */;
310 if (i < strlen(tmp_name)) {
311 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"\ncodec(%s) name is not valid!\n", c->name);
312 return 0;
315 if (!c->info)
316 c->info = strdup(c->name);
318 #if 0
319 if (c->fourcc[0] == 0xffffffff) {
320 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"\ncodec(%s) does not have FourCC/format!\n", c->name);
321 return 0;
323 #endif
325 if (!c->drv) {
326 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"\ncodec(%s) does not have a driver!\n", c->name);
327 return 0;
330 #if 0
331 //FIXME: codec->driver == 4;... <- this should not be put in here...
332 //FIXME: Where are they defined ????????????
333 if (!c->dll && (c->driver == 4 ||
334 (c->driver == 2 && type == TYPE_VIDEO))) {
335 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"\ncodec(%s) needs a 'dll'!\n", c->name);
336 return 0;
338 // FIXME: Can guid.f1 be 0? How does one know that it was not given?
339 // if (!(codec->flags & CODECS_FLAG_AUDIO) && codec->driver == 4)
341 if (type == TYPE_VIDEO)
342 if (c->outfmt[0] == 0xffffffff) {
343 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"\ncodec(%s) needs an 'outfmt'!\n", c->name);
344 return 0;
346 #endif
347 return 1;
350 static int add_comment(char *s, char **d)
352 int pos;
354 if (!*d)
355 pos = 0;
356 else {
357 pos = strlen(*d);
358 (*d)[pos++] = '\n';
360 if (!(*d = realloc(*d, pos + strlen(s) + 1))) {
361 mp_tmsg(MSGT_CODECCFG,MSGL_FATAL,"Can't allocate memory for comment. ");
362 return 0;
364 strcpy(*d + pos, s);
365 return 1;
368 static short get_cpuflags(char *s)
370 static char *flagstr[] = {
371 "mmx",
372 "sse",
373 "3dnow",
374 NULL
376 int i;
377 short flags = 0;
379 do {
380 for (i = 0; flagstr[i]; i++)
381 if (!strncmp(s, flagstr[i], strlen(flagstr[i])))
382 break;
383 if (!flagstr[i])
384 goto err_out_parse_error;
385 flags |= 1<<i;
386 s += strlen(flagstr[i]);
387 } while (*(s++) == ',');
389 if (*(--s) != '\0')
390 goto err_out_parse_error;
392 return flags;
393 err_out_parse_error:
394 return 0;
397 static FILE *fp;
398 static int line_num = 0;
399 static char *line;
400 static char *token[MAX_NR_TOKEN];
401 static int read_nextline = 1;
403 static int get_token(int min, int max)
405 static int line_pos;
406 int i;
407 char c;
409 if (max >= MAX_NR_TOKEN) {
410 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"get_token(): max >= MAX_MR_TOKEN!");
411 goto out_eof;
414 memset(token, 0x00, sizeof(*token) * max);
416 if (read_nextline) {
417 if (!fgets(line, MAX_LINE_LEN, fp))
418 goto out_eof;
419 line_pos = 0;
420 ++line_num;
421 read_nextline = 0;
423 for (i = 0; i < max; i++) {
424 while (isspace(line[line_pos]))
425 ++line_pos;
426 if (line[line_pos] == '\0' || line[line_pos] == '#' ||
427 line[line_pos] == ';') {
428 read_nextline = 1;
429 if (i >= min)
430 goto out_ok;
431 goto out_eol;
433 token[i] = line + line_pos;
434 c = line[line_pos];
435 if (c == '"' || c == '\'') {
436 token[i]++;
437 while (line[++line_pos] != c && line[line_pos])
438 /* NOTHING */;
439 } else {
440 for (/* NOTHING */; !isspace(line[line_pos]) &&
441 line[line_pos]; line_pos++)
442 /* NOTHING */;
444 if (!line[line_pos]) {
445 read_nextline = 1;
446 if (i >= min - 1)
447 goto out_ok;
448 goto out_eol;
450 line[line_pos] = '\0';
451 line_pos++;
453 out_ok:
454 return i;
455 out_eof:
456 read_nextline = 1;
457 return RET_EOF;
458 out_eol:
459 return RET_EOL;
462 static codecs_t *video_codecs=NULL;
463 static codecs_t *audio_codecs=NULL;
464 static int nr_vcodecs = 0;
465 static int nr_acodecs = 0;
467 int parse_codec_cfg(const char *cfgfile)
469 codecs_t *codec = NULL; // current codec
470 codecs_t **codecsp = NULL;// points to audio_codecs or to video_codecs
471 char *endptr; // strtoul()...
472 int *nr_codecsp;
473 int codec_type; /* TYPE_VIDEO/TYPE_AUDIO */
474 int tmp, i;
476 // in case we call it a second time
477 codecs_uninit_free();
479 nr_vcodecs = 0;
480 nr_acodecs = 0;
482 if(cfgfile==NULL) {
483 #ifdef CODECS2HTML
484 return 0;
485 #else
486 /* following casts are harmless since {video,audio}_codecs will stay
487 * untouched in this case */
488 video_codecs = (codecs_t *)builtin_video_codecs;
489 audio_codecs = (codecs_t *)builtin_audio_codecs;
490 nr_vcodecs = sizeof(builtin_video_codecs)/sizeof(codecs_t);
491 nr_acodecs = sizeof(builtin_audio_codecs)/sizeof(codecs_t);
492 return 1;
493 #endif
496 mp_tmsg(MSGT_CODECCFG,MSGL_V,"Reading %s: ", cfgfile);
498 if ((fp = fopen(cfgfile, "r")) == NULL) {
499 mp_tmsg(MSGT_CODECCFG,MSGL_V,"Can't open '%s': %s\n", cfgfile, strerror(errno));
500 return 0;
503 if ((line = malloc(MAX_LINE_LEN + 1)) == NULL) {
504 mp_tmsg(MSGT_CODECCFG,MSGL_FATAL,"Can't get memory for 'line': %s\n", strerror(errno));
505 return 0;
507 read_nextline = 1;
510 * this only catches release lines at the start of
511 * codecs.conf, before audiocodecs and videocodecs.
513 while ((tmp = get_token(1, 1)) == RET_EOL)
514 /* NOTHING */;
515 if (tmp == RET_EOF)
516 goto out;
517 if (!strcmp(token[0], "release")) {
518 if (get_token(1, 2) < 0)
519 goto err_out_parse_error;
520 tmp = atoi(token[0]);
521 if (tmp < CODEC_CFG_MIN)
522 goto err_out_release_num;
523 codecs_conf_release = tmp;
524 while ((tmp = get_token(1, 1)) == RET_EOL)
525 /* NOTHING */;
526 if (tmp == RET_EOF)
527 goto out;
528 } else
529 goto err_out_release_num;
532 * check if the next block starts with 'audiocodec' or
533 * with 'videocodec'
535 if (!strcmp(token[0], "audiocodec") || !strcmp(token[0], "videocodec"))
536 goto loop_enter;
537 goto err_out_parse_error;
539 while ((tmp = get_token(1, 1)) != RET_EOF) {
540 if (tmp == RET_EOL)
541 continue;
542 if (!strcmp(token[0], "audiocodec") ||
543 !strcmp(token[0], "videocodec")) {
544 if (!validate_codec(codec, codec_type))
545 goto err_out_not_valid;
546 loop_enter:
547 if (*token[0] == 'v') {
548 codec_type = TYPE_VIDEO;
549 nr_codecsp = &nr_vcodecs;
550 codecsp = &video_codecs;
551 } else if (*token[0] == 'a') {
552 codec_type = TYPE_AUDIO;
553 nr_codecsp = &nr_acodecs;
554 codecsp = &audio_codecs;
555 #ifdef DEBUG
556 } else {
557 mp_msg(MSGT_CODECCFG,MSGL_ERR,"picsba\n");
558 goto err_out;
559 #endif
561 if (!(*codecsp = realloc(*codecsp,
562 sizeof(codecs_t) * (*nr_codecsp + 2)))) {
563 mp_tmsg(MSGT_CODECCFG,MSGL_FATAL,"Can't realloc '*codecsp': %s\n", strerror(errno));
564 goto err_out;
566 codec=*codecsp + *nr_codecsp;
567 ++*nr_codecsp;
568 memset(codec,0,sizeof(codecs_t));
569 memset(codec->fourcc, 0xff, sizeof(codec->fourcc));
570 memset(codec->outfmt, 0xff, sizeof(codec->outfmt));
571 memset(codec->infmt, 0xff, sizeof(codec->infmt));
573 if (get_token(1, 1) < 0)
574 goto err_out_parse_error;
575 for (i = 0; i < *nr_codecsp - 1; i++) {
576 if(( (*codecsp)[i].name!=NULL) &&
577 (!strcmp(token[0], (*codecsp)[i].name)) ) {
578 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"Codec name '%s' isn't unique.", token[0]);
579 goto err_out_print_linenum;
582 if (!(codec->name = strdup(token[0]))) {
583 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"Can't strdup -> 'name': %s\n", strerror(errno));
584 goto err_out;
586 } else if (!strcmp(token[0], "info")) {
587 if (codec->info || get_token(1, 1) < 0)
588 goto err_out_parse_error;
589 if (!(codec->info = strdup(token[0]))) {
590 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"Can't strdup -> 'info': %s\n", strerror(errno));
591 goto err_out;
593 } else if (!strcmp(token[0], "comment")) {
594 if (get_token(1, 1) < 0)
595 goto err_out_parse_error;
596 add_comment(token[0], &codec->comment);
597 } else if (!strcmp(token[0], "fourcc")) {
598 if (get_token(1, 2) < 0)
599 goto err_out_parse_error;
600 if (!add_to_fourcc(token[0], token[1],
601 codec->fourcc,
602 codec->fourccmap))
603 goto err_out_print_linenum;
604 } else if (!strcmp(token[0], "format")) {
605 if (get_token(1, 2) < 0)
606 goto err_out_parse_error;
607 if (!add_to_format(token[0], token[1],
608 codec->fourcc,codec->fourccmap))
609 goto err_out_print_linenum;
610 } else if (!strcmp(token[0], "driver")) {
611 if (get_token(1, 1) < 0)
612 goto err_out_parse_error;
613 if (!(codec->drv = strdup(token[0]))) {
614 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"Can't strdup -> 'driver': %s\n", strerror(errno));
615 goto err_out;
617 } else if (!strcmp(token[0], "dll")) {
618 if (get_token(1, 1) < 0)
619 goto err_out_parse_error;
620 if (!(codec->dll = strdup(token[0]))) {
621 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"Can't strdup -> 'dll': %s", strerror(errno));
622 goto err_out;
624 } else if (!strcmp(token[0], "guid")) {
625 if (get_token(11, 11) < 0)
626 goto err_out_parse_error;
627 codec->guid.f1=strtoul(token[0],&endptr,0);
628 if ((*endptr != ',' || *(endptr + 1) != '\0') &&
629 *endptr != '\0')
630 goto err_out_parse_error;
631 codec->guid.f2=strtoul(token[1],&endptr,0);
632 if ((*endptr != ',' || *(endptr + 1) != '\0') &&
633 *endptr != '\0')
634 goto err_out_parse_error;
635 codec->guid.f3=strtoul(token[2],&endptr,0);
636 if ((*endptr != ',' || *(endptr + 1) != '\0') &&
637 *endptr != '\0')
638 goto err_out_parse_error;
639 for (i = 0; i < 8; i++) {
640 codec->guid.f4[i]=strtoul(token[i + 3],&endptr,0);
641 if ((*endptr != ',' || *(endptr + 1) != '\0') &&
642 *endptr != '\0')
643 goto err_out_parse_error;
645 } else if (!strcmp(token[0], "out")) {
646 if (get_token(1, 2) < 0)
647 goto err_out_parse_error;
648 if (!add_to_inout(token[0], token[1], codec->outfmt,
649 codec->outflags))
650 goto err_out_print_linenum;
651 } else if (!strcmp(token[0], "in")) {
652 if (get_token(1, 2) < 0)
653 goto err_out_parse_error;
654 if (!add_to_inout(token[0], token[1], codec->infmt,
655 codec->inflags))
656 goto err_out_print_linenum;
657 } else if (!strcmp(token[0], "flags")) {
658 if (get_token(1, 1) < 0)
659 goto err_out_parse_error;
660 if (!strcmp(token[0], "seekable"))
661 codec->flags |= CODECS_FLAG_SEEKABLE;
662 else if (!strcmp(token[0], "align16"))
663 codec->flags |= CODECS_FLAG_ALIGN16;
664 else
665 goto err_out_parse_error;
666 } else if (!strcmp(token[0], "status")) {
667 if (get_token(1, 1) < 0)
668 goto err_out_parse_error;
669 if (!strcasecmp(token[0], "working"))
670 codec->status = CODECS_STATUS_WORKING;
671 else if (!strcasecmp(token[0], "crashing"))
672 codec->status = CODECS_STATUS_NOT_WORKING;
673 else if (!strcasecmp(token[0], "untested"))
674 codec->status = CODECS_STATUS_UNTESTED;
675 else if (!strcasecmp(token[0], "buggy"))
676 codec->status = CODECS_STATUS_PROBLEMS;
677 else
678 goto err_out_parse_error;
679 } else if (!strcmp(token[0], "cpuflags")) {
680 if (get_token(1, 1) < 0)
681 goto err_out_parse_error;
682 if (!(codec->cpuflags = get_cpuflags(token[0])))
683 goto err_out_parse_error;
684 } else
685 goto err_out_parse_error;
687 if (!validate_codec(codec, codec_type))
688 goto err_out_not_valid;
689 mp_tmsg(MSGT_CODECCFG,MSGL_INFO,"%d audio & %d video codecs\n", nr_acodecs, nr_vcodecs);
690 if(video_codecs) video_codecs[nr_vcodecs].name = NULL;
691 if(audio_codecs) audio_codecs[nr_acodecs].name = NULL;
692 out:
693 free(line);
694 line=NULL;
695 fclose(fp);
696 return 1;
698 err_out_parse_error:
699 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"parse error");
700 err_out_print_linenum:
701 PRINT_LINENUM;
702 err_out:
703 codecs_uninit_free();
705 free(line);
706 line=NULL;
707 line_num = 0;
708 fclose(fp);
709 return 0;
710 err_out_not_valid:
711 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"Codec is not defined correctly.");
712 goto err_out_print_linenum;
713 err_out_release_num:
714 mp_tmsg(MSGT_CODECCFG,MSGL_ERR,"This codecs.conf is too old and incompatible with this MPlayer release!");
715 goto err_out_print_linenum;
718 static void codecs_free(codecs_t* codecs,int count) {
719 int i;
720 for ( i = 0; i < count; i++)
721 if ( codecs[i].name ) {
722 free(codecs[i].name);
723 free(codecs[i].info);
724 free(codecs[i].comment);
725 free(codecs[i].dll);
726 free(codecs[i].drv);
728 free(codecs);
731 void codecs_uninit_free(void) {
732 if (video_codecs)
733 codecs_free(video_codecs,nr_vcodecs);
734 video_codecs=NULL;
735 if (audio_codecs)
736 codecs_free(audio_codecs,nr_acodecs);
737 audio_codecs=NULL;
740 codecs_t *find_audio_codec(unsigned int fourcc, unsigned int *fourccmap,
741 codecs_t *start, int force)
743 return find_codec(fourcc, fourccmap, start, 1, force);
746 codecs_t *find_video_codec(unsigned int fourcc, unsigned int *fourccmap,
747 codecs_t *start, int force)
749 return find_codec(fourcc, fourccmap, start, 0, force);
752 codecs_t* find_codec(unsigned int fourcc,unsigned int *fourccmap,
753 codecs_t *start, int audioflag, int force)
755 int i, j;
756 codecs_t *c;
758 #if 0
759 if (start) {
760 for (/* NOTHING */; start->name; start++) {
761 for (j = 0; j < CODECS_MAX_FOURCC; j++) {
762 if (start->fourcc[j] == fourcc) {
763 if (fourccmap)
764 *fourccmap = start->fourccmap[j];
765 return start;
769 } else
770 #endif
772 if (audioflag) {
773 i = nr_acodecs;
774 c = audio_codecs;
775 } else {
776 i = nr_vcodecs;
777 c = video_codecs;
779 if(!i) return NULL;
780 for (/* NOTHING */; i--; c++) {
781 if(start && c<=start) continue;
782 for (j = 0; j < CODECS_MAX_FOURCC; j++) {
783 // FIXME: do NOT hardwire 'null' name here:
784 if (c->fourcc[j]==fourcc || !strcmp(c->drv,"null")) {
785 if (fourccmap)
786 *fourccmap = c->fourccmap[j];
787 return c;
790 if (force) return c;
793 return NULL;
796 void stringset_init(stringset_t *set) {
797 *set = calloc(1, sizeof(char *));
800 void stringset_free(stringset_t *set) {
801 int count = 0;
802 while ((*set)[count]) free((*set)[count++]);
803 free(*set);
804 *set = NULL;
807 void stringset_add(stringset_t *set, const char *str) {
808 int count = 0;
809 while ((*set)[count]) count++;
810 count++;
811 *set = realloc(*set, sizeof(char *) * (count + 1));
812 (*set)[count - 1] = strdup(str);
813 (*set)[count] = NULL;
816 int stringset_test(stringset_t *set, const char *str) {
817 stringset_t s;
818 for (s = *set; *s; s++)
819 if (strcmp(*s, str) == 0)
820 return 1;
821 return 0;
824 void list_codecs(int audioflag){
825 int i;
826 codecs_t *c;
828 if (audioflag) {
829 i = nr_acodecs;
830 c = audio_codecs;
831 mp_msg(MSGT_CODECCFG,MSGL_INFO,"ac: afm: status: info: [lib/dll]\n");
832 } else {
833 i = nr_vcodecs;
834 c = video_codecs;
835 mp_msg(MSGT_CODECCFG,MSGL_INFO,"vc: vfm: status: info: [lib/dll]\n");
837 if(!i) return;
838 for (/* NOTHING */; i--; c++) {
839 char* s="unknown ";
840 switch(c->status){
841 case CODECS_STATUS_WORKING: s="working ";break;
842 case CODECS_STATUS_PROBLEMS: s="problems";break;
843 case CODECS_STATUS_NOT_WORKING: s="crashing";break;
844 case CODECS_STATUS_UNTESTED: s="untested";break;
846 if(c->dll)
847 mp_msg(MSGT_CODECCFG,MSGL_INFO,"%-11s %-9s %s %s [%s]\n",c->name,c->drv,s,c->info,c->dll);
848 else
849 mp_msg(MSGT_CODECCFG,MSGL_INFO,"%-11s %-9s %s %s\n",c->name,c->drv,s,c->info);
854 #ifdef CODECS2HTML
855 static void wrapline(FILE *f2,char *s){
856 int c;
857 if(!s){
858 fprintf(f2,"-");
859 return;
861 while((c=*s++)){
862 if(c==',') fprintf(f2,"<br>"); else fputc(c,f2);
866 static void parsehtml(FILE *f1,FILE *f2,codecs_t *codec){
867 int c,d;
868 while((c=fgetc(f1))>=0){
869 if(c!='%'){
870 fputc(c,f2);
871 continue;
873 d=fgetc(f1);
875 switch(d){
876 case '.':
877 return; // end of section
878 case 'n':
879 wrapline(f2,codec->name); break;
880 case 'i':
881 wrapline(f2,codec->info); break;
882 case 'c':
883 wrapline(f2,codec->comment); break;
884 case 'd':
885 wrapline(f2,codec->dll); break;
886 case 'D':
887 fprintf(f2,"%c",!strcmp(codec->drv,"dshow")?'+':'-'); break;
888 case 'F':
889 for(d=0;d<CODECS_MAX_FOURCC;d++)
890 if(!d || codec->fourcc[d]!=0xFFFFFFFF)
891 fprintf(f2,"%s%.4s",d?"<br>":"",(codec->fourcc[d]==0xFFFFFFFF || codec->fourcc[d]<0x20202020)?!d?"-":"":(char*) &codec->fourcc[d]);
892 break;
893 case 'f':
894 for(d=0;d<CODECS_MAX_FOURCC;d++)
895 if(codec->fourcc[d]!=0xFFFFFFFF)
896 fprintf(f2,"%s0x%X",d?"<br>":"",codec->fourcc[d]);
897 break;
898 case 'Y':
899 for(d=0;d<CODECS_MAX_OUTFMT;d++)
900 if(codec->outfmt[d]!=0xFFFFFFFF){
901 for (c=0; fmt_table[c].name; c++)
902 if(fmt_table[c].num==codec->outfmt[d]) break;
903 if(fmt_table[c].name)
904 fprintf(f2,"%s%s",d?"<br>":"",fmt_table[c].name);
906 break;
907 default:
908 fputc(c,f2);
909 fputc(d,f2);
914 void skiphtml(FILE *f1){
915 int c,d;
916 while((c=fgetc(f1))>=0){
917 if(c!='%'){
918 continue;
920 d=fgetc(f1);
921 if(d=='.') return; // end of section
925 static void print_int_array(const unsigned int* a, int size)
927 printf("{ ");
928 while (size--)
929 if(abs(*a)<256)
930 printf("%d%s", *a++, size?", ":"");
931 else
932 printf("0x%X%s", *a++, size?", ":"");
933 printf(" }");
936 static void print_char_array(const unsigned char* a, int size)
938 printf("{ ");
939 while (size--)
940 if((*a)<10)
941 printf("%d%s", *a++, size?", ":"");
942 else
943 printf("0x%02x%s", *a++, size?", ":"");
944 printf(" }");
947 static void print_string(const char* s)
949 if (!s) printf("NULL");
950 else printf("\"%s\"", s);
953 int main(int argc, char* argv[])
955 codecs_t *cl;
956 FILE *f1;
957 FILE *f2;
958 int c,d,i;
959 int pos;
960 int section=-1;
961 int nr_codecs;
962 int win32=-1;
963 int dshow=-1;
964 int win32ex=-1;
967 * Take path to codecs.conf from command line, or fall back on
968 * etc/codecs.conf
970 if (!(nr_codecs = parse_codec_cfg((argc>1)?argv[1]:"etc/codecs.conf")))
971 exit(1);
972 if (codecs_conf_release < CODEC_CFG_MIN)
973 exit(1);
975 if (argc > 1) {
976 int i, j;
977 const char* nm[2];
978 codecs_t* cod[2];
979 int nr[2];
981 nm[0] = "builtin_video_codecs";
982 cod[0] = video_codecs;
983 nr[0] = nr_vcodecs;
985 nm[1] = "builtin_audio_codecs";
986 cod[1] = audio_codecs;
987 nr[1] = nr_acodecs;
989 printf("/* GENERATED FROM %s, DO NOT EDIT! */\n\n",argv[1]);
990 printf("#include <stddef.h>\n");
991 printf("#include \"codec-cfg.h\"\n\n");
992 printf("#define CODEC_CFG_MIN %i\n\n", codecs_conf_release);
994 for (i=0; i<2; i++) {
995 printf("const codecs_t %s[] = {\n", nm[i]);
996 for (j = 0; j < nr[i]; j++) {
997 printf("{");
999 print_int_array(cod[i][j].fourcc, CODECS_MAX_FOURCC);
1000 printf(", /* fourcc */\n");
1002 print_int_array(cod[i][j].fourccmap, CODECS_MAX_FOURCC);
1003 printf(", /* fourccmap */\n");
1005 print_int_array(cod[i][j].outfmt, CODECS_MAX_OUTFMT);
1006 printf(", /* outfmt */\n");
1008 print_char_array(cod[i][j].outflags, CODECS_MAX_OUTFMT);
1009 printf(", /* outflags */\n");
1011 print_int_array(cod[i][j].infmt, CODECS_MAX_INFMT);
1012 printf(", /* infmt */\n");
1014 print_char_array(cod[i][j].inflags, CODECS_MAX_INFMT);
1015 printf(", /* inflags */\n");
1017 print_string(cod[i][j].name); printf(", /* name */\n");
1018 print_string(cod[i][j].info); printf(", /* info */\n");
1019 print_string(cod[i][j].comment); printf(", /* comment */\n");
1020 print_string(cod[i][j].dll); printf(", /* dll */\n");
1021 print_string(cod[i][j].drv); printf(", /* drv */\n");
1023 printf("{ 0x%08lx, %hu, %hu,",
1024 cod[i][j].guid.f1,
1025 cod[i][j].guid.f2,
1026 cod[i][j].guid.f3);
1027 print_char_array(cod[i][j].guid.f4, sizeof(cod[i][j].guid.f4));
1028 printf(" }, /* GUID */\n");
1029 printf("%hd /* flags */, %hd /* status */, %hd /* cpuflags */ }\n",
1030 cod[i][j].flags,
1031 cod[i][j].status,
1032 cod[i][j].cpuflags);
1033 if (j < nr[i]) printf(",\n");
1035 printf("};\n\n");
1037 exit(0);
1040 f1=fopen("DOCS/tech/codecs-in.html","rb"); if(!f1) exit(1);
1041 f2=fopen("DOCS/codecs-status.html","wb"); if(!f2) exit(1);
1043 while((c=fgetc(f1))>=0){
1044 if(c!='%'){
1045 fputc(c,f2);
1046 continue;
1048 d=fgetc(f1);
1049 if(d>='0' && d<='9'){
1050 // begin section
1051 section=d-'0';
1052 //printf("BEGIN %d\n",section);
1053 if(section>=5){
1054 // audio
1055 cl = audio_codecs;
1056 nr_codecs = nr_acodecs;
1057 dshow=7;win32=4;
1058 } else {
1059 // video
1060 cl = video_codecs;
1061 nr_codecs = nr_vcodecs;
1062 dshow=4;win32=2;win32ex=6;
1064 pos=ftell(f1);
1065 for(i=0;i<nr_codecs;i++){
1066 fseek(f1,pos,SEEK_SET);
1067 switch(section){
1068 case 0:
1069 case 5:
1070 if(cl[i].status==CODECS_STATUS_WORKING)
1071 // if(!(!strcmp(cl[i].drv,"vfw") || !strcmp(cl[i].drv,"dshow") || !strcmp(cl[i].drv,"vfwex") || !strcmp(cl[i].drv,"acm")))
1072 parsehtml(f1,f2,&cl[i]);
1073 break;
1074 #if 0
1075 case 1:
1076 case 6:
1077 if(cl[i].status==CODECS_STATUS_WORKING)
1078 if((!strcmp(cl[i].drv,"vfw") || !strcmp(cl[i].drv,"dshow") || !strcmp(cl[i].drv,"vfwex") || !strcmp(cl[i].drv,"acm")))
1079 parsehtml(f1,f2,&cl[i]);
1080 break;
1081 #endif
1082 case 2:
1083 case 7:
1084 if(cl[i].status==CODECS_STATUS_PROBLEMS)
1085 parsehtml(f1,f2,&cl[i]);
1086 break;
1087 case 3:
1088 case 8:
1089 if(cl[i].status==CODECS_STATUS_NOT_WORKING)
1090 parsehtml(f1,f2,&cl[i]);
1091 break;
1092 case 4:
1093 case 9:
1094 if(cl[i].status==CODECS_STATUS_UNTESTED)
1095 parsehtml(f1,f2,&cl[i]);
1096 break;
1097 default:
1098 printf("Warning! unimplemented section: %d\n",section);
1101 fseek(f1,pos,SEEK_SET);
1102 skiphtml(f1);
1104 continue;
1106 fputc(c,f2);
1107 fputc(d,f2);
1110 fclose(f2);
1111 fclose(f1);
1112 return 0;
1115 #endif
1117 #ifdef TESTING
1118 int main(void)
1120 codecs_t *c;
1121 int i,j, nr_codecs, state;
1123 if (!(parse_codec_cfg("etc/codecs.conf")))
1124 return 0;
1125 if (!video_codecs)
1126 printf("no videoconfig.\n");
1127 if (!audio_codecs)
1128 printf("no audioconfig.\n");
1130 printf("videocodecs:\n");
1131 c = video_codecs;
1132 nr_codecs = nr_vcodecs;
1133 state = 0;
1134 next:
1135 if (c) {
1136 printf("number of %scodecs: %d\n", state==0?"video":"audio",
1137 nr_codecs);
1138 for(i=0;i<nr_codecs;i++, c++){
1139 printf("\n============== %scodec %02d ===============\n",
1140 state==0?"video":"audio",i);
1141 printf("name='%s'\n",c->name);
1142 printf("info='%s'\n",c->info);
1143 printf("comment='%s'\n",c->comment);
1144 printf("dll='%s'\n",c->dll);
1145 /* printf("flags=%X driver=%d status=%d cpuflags=%d\n",
1146 c->flags, c->driver, c->status, c->cpuflags); */
1147 printf("flags=%X status=%d cpuflags=%d\n",
1148 c->flags, c->status, c->cpuflags);
1150 for(j=0;j<CODECS_MAX_FOURCC;j++){
1151 if(c->fourcc[j]!=0xFFFFFFFF){
1152 printf("fourcc %02d: %08X (%.4s) ===> %08X (%.4s)\n",j,c->fourcc[j],(char *) &c->fourcc[j],c->fourccmap[j],(char *) &c->fourccmap[j]);
1156 for(j=0;j<CODECS_MAX_OUTFMT;j++){
1157 if(c->outfmt[j]!=0xFFFFFFFF){
1158 printf("outfmt %02d: %08X (%.4s) flags: %d\n",j,c->outfmt[j],(char *) &c->outfmt[j],c->outflags[j]);
1162 for(j=0;j<CODECS_MAX_INFMT;j++){
1163 if(c->infmt[j]!=0xFFFFFFFF){
1164 printf("infmt %02d: %08X (%.4s) flags: %d\n",j,c->infmt[j],(char *) &c->infmt[j],c->inflags[j]);
1168 printf("GUID: %08lX %04X %04X",c->guid.f1,c->guid.f2,c->guid.f3);
1169 for(j=0;j<8;j++) printf(" %02X",c->guid.f4[j]);
1170 printf("\n");
1175 if (!state) {
1176 printf("audiocodecs:\n");
1177 c = audio_codecs;
1178 nr_codecs = nr_acodecs;
1179 state = 1;
1180 goto next;
1182 return 0;
1185 #endif