4 * Copyright (C) 2001 Szabolcs Berecz <szabi@inf.elte.hu>
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
35 #include "libmpcodecs/img_format.h"
36 #include "codec-cfg.h"
38 #include "stream/stream.h"
41 static const char embedded_file
[] =
42 #include "codecs.conf.h"
44 static const struct bstr builtin_codecs_conf
= {
45 .start
= (char *)embedded_file
, .len
= sizeof(embedded_file
) - 1
48 #define mmioFOURCC( ch0, ch1, ch2, ch3 ) \
49 ( (uint32_t)(uint8_t)(ch0) | ( (uint32_t)(uint8_t)(ch1) << 8 ) | \
50 ( (uint32_t)(uint8_t)(ch2) << 16 ) | ( (uint32_t)(uint8_t)(ch3) << 24 ) )
52 #define PRINT_LINENUM mp_msg(MSGT_CODECCFG,MSGL_ERR," at line %d\n", line_num)
54 #define MAX_NR_TOKEN 16
62 static int codecs_conf_release
;
63 char * codecs_file
= NULL
;
65 static int add_to_fourcc(char *s
, char *alias
, unsigned int *fourcc
,
71 /* find first unused slot */
72 for (i
= 0; i
< CODECS_MAX_FOURCC
&& fourcc
[i
] != 0xffffffff; i
++)
74 freeslots
= CODECS_MAX_FOURCC
- i
;
76 goto err_out_too_many
;
80 goto err_out_parse_error
;
81 tmp
= mmioFOURCC(s
[0], s
[1], s
[2], s
[3]);
82 for (j
= 0; j
< i
; j
++)
84 goto err_out_duplicated
;
86 map
[i
] = alias
? mmioFOURCC(alias
[0], alias
[1], alias
[2], alias
[3]) : tmp
;
89 } while ((*(s
++) == ',') && --freeslots
);
92 goto err_out_too_many
;
94 goto err_out_parse_error
;
97 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"duplicated FourCC");
100 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"too many FourCCs/formats...");
103 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"parse error");
107 static int add_to_format(char *s
, char *alias
,unsigned int *fourcc
, unsigned int *fourccmap
)
112 /* find first unused slot */
113 for (i
= 0; i
< CODECS_MAX_FOURCC
&& fourcc
[i
] != 0xffffffff; i
++)
115 if (i
== CODECS_MAX_FOURCC
) {
116 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"too many FourCCs/formats...");
120 fourcc
[i
]=strtoul(s
,&endptr
,0);
121 if (*endptr
!= '\0') {
122 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"parse error (format ID not a number?)");
127 fourccmap
[i
]=strtoul(alias
,&endptr
,0);
128 if (*endptr
!= '\0') {
129 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"parse error (format ID alias not a number?)");
133 fourccmap
[i
]=fourcc
[i
];
135 for (j
= 0; j
< i
; j
++)
136 if (fourcc
[j
] == fourcc
[i
]) {
137 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"duplicated format ID");
144 static const struct {
146 const unsigned int num
;
148 // note: due to parser deficiencies/simplicity, if one format
149 // name matches the beginning of another, the longer one _must_
150 // come first in this list.
151 {"YV12", IMGFMT_YV12
},
152 {"I420", IMGFMT_I420
},
153 {"IYUV", IMGFMT_IYUV
},
154 {"NV12", IMGFMT_NV12
},
155 {"NV21", IMGFMT_NV21
},
156 {"YVU9", IMGFMT_YVU9
},
157 {"IF09", IMGFMT_IF09
},
158 {"444P16LE", IMGFMT_444P16_LE
},
159 {"444P16BE", IMGFMT_444P16_BE
},
160 {"422P16LE", IMGFMT_422P16_LE
},
161 {"422P16BE", IMGFMT_422P16_BE
},
162 {"420P16LE", IMGFMT_420P16_LE
},
163 {"420P16BE", IMGFMT_420P16_BE
},
164 {"444P16", IMGFMT_444P16
},
165 {"444P10", IMGFMT_444P10
},
166 {"444P9", IMGFMT_444P9
},
167 {"422P16", IMGFMT_422P16
},
168 {"422P10", IMGFMT_422P10
},
169 {"420P16", IMGFMT_420P16
},
170 {"420P10", IMGFMT_420P10
},
171 {"420P9", IMGFMT_420P9
},
172 {"420A", IMGFMT_420A
},
173 {"444P", IMGFMT_444P
},
174 {"422P", IMGFMT_422P
},
175 {"411P", IMGFMT_411P
},
176 {"440P", IMGFMT_440P
},
177 {"Y800", IMGFMT_Y800
},
180 {"YUY2", IMGFMT_YUY2
},
181 {"UYVY", IMGFMT_UYVY
},
182 {"YVYU", IMGFMT_YVYU
},
184 {"RGB48LE", IMGFMT_RGB48LE
},
185 {"RGB48BE", IMGFMT_RGB48BE
},
186 {"RGB4", IMGFMT_RGB4
},
187 {"RGB8", IMGFMT_RGB8
},
188 {"RGB15", IMGFMT_RGB15
},
189 {"RGB16", IMGFMT_RGB16
},
190 {"RGB24", IMGFMT_RGB24
},
191 {"RGB32", IMGFMT_RGB32
},
192 {"BGR4", IMGFMT_BGR4
},
193 {"BGR8", IMGFMT_BGR8
},
194 {"BGR15", IMGFMT_BGR15
},
195 {"BGR16", IMGFMT_BGR16
},
196 {"BGR24", IMGFMT_BGR24
},
197 {"BGR32", IMGFMT_BGR32
},
198 {"RGB1", IMGFMT_RGB1
},
199 {"BGR1", IMGFMT_BGR1
},
201 {"MPES", IMGFMT_MPEGPES
},
203 {"VDPAU_MPEG1",IMGFMT_VDPAU_MPEG1
},
204 {"VDPAU_MPEG2",IMGFMT_VDPAU_MPEG2
},
205 {"VDPAU_H264",IMGFMT_VDPAU_H264
},
206 {"VDPAU_WMV3",IMGFMT_VDPAU_WMV3
},
207 {"VDPAU_VC1",IMGFMT_VDPAU_VC1
},
208 {"VDPAU_MPEG4",IMGFMT_VDPAU_MPEG4
},
214 static int add_to_inout(char *sfmt
, char *sflags
, unsigned int *outfmt
,
215 unsigned char *outflags
)
218 static char *flagstr
[] = {
230 for (i
= 0; i
< CODECS_MAX_OUTFMT
&& outfmt
[i
] != 0xffffffff; i
++)
232 freeslots
= CODECS_MAX_OUTFMT
- i
;
234 goto err_out_too_many
;
239 for (j
= 0; flagstr
[j
] != NULL
; j
++)
240 if (!strncmp(sflags
, flagstr
[j
],
243 if (flagstr
[j
] == NULL
)
244 goto err_out_parse_error
;
246 sflags
+=strlen(flagstr
[j
]);
247 } while (*(sflags
++) == ',');
249 if (*(--sflags
) != '\0')
250 goto err_out_parse_error
;
254 for (j
= 0; fmt_table
[j
].name
!= NULL
; j
++)
255 if (!strncmp(sfmt
, fmt_table
[j
].name
, strlen(fmt_table
[j
].name
)))
257 if (fmt_table
[j
].name
== NULL
)
258 goto err_out_parse_error
;
259 outfmt
[i
] = fmt_table
[j
].num
;
262 sfmt
+=strlen(fmt_table
[j
].name
);
263 } while ((*(sfmt
++) == ',') && --freeslots
);
266 goto err_out_too_many
;
268 if (*(--sfmt
) != '\0')
269 goto err_out_parse_error
;
273 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"too many out...");
276 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"parse error");
280 static int validate_codec(codecs_t
*c
, int type
)
283 char *tmp_name
= c
->name
;
285 for (i
= 0; i
< strlen(tmp_name
) && isalnum(tmp_name
[i
]); i
++)
288 if (i
< strlen(tmp_name
)) {
289 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"\ncodec(%s) name is not valid!\n", c
->name
);
294 c
->info
= strdup(c
->name
);
297 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"\ncodec(%s) does not have a driver!\n", c
->name
);
304 static int add_comment(char *s
, char **d
)
314 if (!(*d
= realloc(*d
, pos
+ strlen(s
) + 1))) {
315 mp_tmsg(MSGT_CODECCFG
,MSGL_FATAL
,"Can't allocate memory for comment. ");
322 static struct bstr filetext
;
323 static int line_num
= 0;
325 static char *token
[MAX_NR_TOKEN
];
326 static int read_nextline
= 1;
328 static int get_token(int min
, int max
)
334 if (max
>= MAX_NR_TOKEN
) {
335 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"get_token(): max >= MAX_MR_TOKEN!");
339 memset(token
, 0x00, sizeof(*token
) * max
);
344 struct bstr nextline
= bstr_getline(filetext
, &filetext
);
345 line
= nextline
.start
;
346 line
[nextline
.len
- 1] = 0;
351 for (i
= 0; i
< max
; i
++) {
352 while (isspace(line
[line_pos
]))
354 if (line
[line_pos
] == '\0' || line
[line_pos
] == '#' ||
355 line
[line_pos
] == ';') {
361 token
[i
] = line
+ line_pos
;
363 if (c
== '"' || c
== '\'') {
365 while (line
[++line_pos
] != c
&& line
[line_pos
])
368 for (/* NOTHING */; !isspace(line
[line_pos
]) &&
369 line
[line_pos
]; line_pos
++)
372 if (!line
[line_pos
]) {
378 line
[line_pos
] = '\0';
390 static codecs_t
*video_codecs
=NULL
;
391 static codecs_t
*audio_codecs
=NULL
;
392 static int nr_vcodecs
= 0;
393 static int nr_acodecs
= 0;
395 int parse_codec_cfg(const char *cfgfile
)
397 codecs_t
*codec
= NULL
; // current codec
398 codecs_t
**codecsp
= NULL
;// points to audio_codecs or to video_codecs
399 char *endptr
; // strtoul()...
401 int codec_type
; /* TYPE_VIDEO/TYPE_AUDIO */
405 for (struct bstr s
= builtin_codecs_conf
; ; bstr_getline(s
, &s
)) {
408 if (bstr_eatstart0(&s
, "release ")) {
409 codec_cfg_min
= atoi(s
.start
);
414 // in case we call it a second time
415 codecs_uninit_free();
421 // Avoid printing errors from open_stream when trying optional files
422 if (!mp_path_exists(cfgfile
)) {
423 mp_tmsg(MSGT_CODECCFG
, MSGL_V
,
424 "No optional codecs config file: %s\n", cfgfile
);
427 mp_msg(MSGT_CODECCFG
, MSGL_V
, "Reading codec config file: %s\n",
429 struct stream
*s
= open_stream(cfgfile
, NULL
, NULL
);
432 filetext
= stream_read_complete(s
, NULL
, 10000000, 1);
437 // Parsing modifies the data
438 filetext
= bstrdup(NULL
, builtin_codecs_conf
);
439 void *tmpmem
= filetext
.start
;
444 * this only catches release lines at the start of
445 * codecs.conf, before audiocodecs and videocodecs.
447 while ((tmp
= get_token(1, 1)) == RET_EOL
)
451 if (!strcmp(token
[0], "release")) {
452 if (get_token(1, 2) < 0)
453 goto err_out_parse_error
;
454 tmp
= atoi(token
[0]);
455 if (tmp
< codec_cfg_min
)
456 goto err_out_release_num
;
457 codecs_conf_release
= tmp
;
458 while ((tmp
= get_token(1, 1)) == RET_EOL
)
463 goto err_out_release_num
;
466 * check if the next block starts with 'audiocodec' or
469 if (!strcmp(token
[0], "audiocodec") || !strcmp(token
[0], "videocodec"))
471 goto err_out_parse_error
;
473 while ((tmp
= get_token(1, 1)) != RET_EOF
) {
476 if (!strcmp(token
[0], "audiocodec") ||
477 !strcmp(token
[0], "videocodec")) {
478 if (!validate_codec(codec
, codec_type
))
479 goto err_out_not_valid
;
481 if (*token
[0] == 'v') {
482 codec_type
= TYPE_VIDEO
;
483 nr_codecsp
= &nr_vcodecs
;
484 codecsp
= &video_codecs
;
486 assert(*token
[0] == 'a');
487 codec_type
= TYPE_AUDIO
;
488 nr_codecsp
= &nr_acodecs
;
489 codecsp
= &audio_codecs
;
491 if (!(*codecsp
= realloc(*codecsp
,
492 sizeof(codecs_t
) * (*nr_codecsp
+ 2)))) {
493 mp_tmsg(MSGT_CODECCFG
,MSGL_FATAL
,"Can't realloc '*codecsp': %s\n", strerror(errno
));
496 codec
=*codecsp
+ *nr_codecsp
;
498 memset(codec
,0,sizeof(codecs_t
));
499 memset(codec
->fourcc
, 0xff, sizeof(codec
->fourcc
));
500 memset(codec
->outfmt
, 0xff, sizeof(codec
->outfmt
));
501 memset(codec
->infmt
, 0xff, sizeof(codec
->infmt
));
503 if (get_token(1, 1) < 0)
504 goto err_out_parse_error
;
505 for (i
= 0; i
< *nr_codecsp
- 1; i
++) {
506 if(( (*codecsp
)[i
].name
!=NULL
) &&
507 (!strcmp(token
[0], (*codecsp
)[i
].name
)) ) {
508 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"Codec name '%s' isn't unique.", token
[0]);
509 goto err_out_print_linenum
;
512 if (!(codec
->name
= strdup(token
[0]))) {
513 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"Can't strdup -> 'name': %s\n", strerror(errno
));
516 } else if (!strcmp(token
[0], "info")) {
517 if (codec
->info
|| get_token(1, 1) < 0)
518 goto err_out_parse_error
;
519 if (!(codec
->info
= strdup(token
[0]))) {
520 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"Can't strdup -> 'info': %s\n", strerror(errno
));
523 } else if (!strcmp(token
[0], "comment")) {
524 if (get_token(1, 1) < 0)
525 goto err_out_parse_error
;
526 add_comment(token
[0], &codec
->comment
);
527 } else if (!strcmp(token
[0], "fourcc")) {
528 if (get_token(1, 2) < 0)
529 goto err_out_parse_error
;
530 if (!add_to_fourcc(token
[0], token
[1],
533 goto err_out_print_linenum
;
534 } else if (!strcmp(token
[0], "format")) {
535 if (get_token(1, 2) < 0)
536 goto err_out_parse_error
;
537 if (!add_to_format(token
[0], token
[1],
538 codec
->fourcc
,codec
->fourccmap
))
539 goto err_out_print_linenum
;
540 } else if (!strcmp(token
[0], "driver")) {
541 if (get_token(1, 1) < 0)
542 goto err_out_parse_error
;
543 if (!(codec
->drv
= strdup(token
[0]))) {
544 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"Can't strdup -> 'driver': %s\n", strerror(errno
));
547 } else if (!strcmp(token
[0], "dll")) {
548 if (get_token(1, 1) < 0)
549 goto err_out_parse_error
;
550 if (!(codec
->dll
= strdup(token
[0]))) {
551 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"Can't strdup -> 'dll': %s", strerror(errno
));
554 } else if (!strcmp(token
[0], "guid")) {
555 if (get_token(11, 11) < 0)
556 goto err_out_parse_error
;
557 codec
->guid
.f1
=strtoul(token
[0],&endptr
,0);
558 if ((*endptr
!= ',' || *(endptr
+ 1) != '\0') &&
560 goto err_out_parse_error
;
561 codec
->guid
.f2
=strtoul(token
[1],&endptr
,0);
562 if ((*endptr
!= ',' || *(endptr
+ 1) != '\0') &&
564 goto err_out_parse_error
;
565 codec
->guid
.f3
=strtoul(token
[2],&endptr
,0);
566 if ((*endptr
!= ',' || *(endptr
+ 1) != '\0') &&
568 goto err_out_parse_error
;
569 for (i
= 0; i
< 8; i
++) {
570 codec
->guid
.f4
[i
]=strtoul(token
[i
+ 3],&endptr
,0);
571 if ((*endptr
!= ',' || *(endptr
+ 1) != '\0') &&
573 goto err_out_parse_error
;
575 } else if (!strcmp(token
[0], "out")) {
576 if (get_token(1, 2) < 0)
577 goto err_out_parse_error
;
578 if (!add_to_inout(token
[0], token
[1], codec
->outfmt
,
580 goto err_out_print_linenum
;
581 } else if (!strcmp(token
[0], "in")) {
582 if (get_token(1, 2) < 0)
583 goto err_out_parse_error
;
584 if (!add_to_inout(token
[0], token
[1], codec
->infmt
,
586 goto err_out_print_linenum
;
587 } else if (!strcmp(token
[0], "flags")) {
588 if (get_token(1, 1) < 0)
589 goto err_out_parse_error
;
590 if (!strcmp(token
[0], "seekable"))
591 codec
->flags
|= CODECS_FLAG_SEEKABLE
;
592 else if (!strcmp(token
[0], "align16"))
593 codec
->flags
|= CODECS_FLAG_ALIGN16
;
595 goto err_out_parse_error
;
596 } else if (!strcmp(token
[0], "status")) {
597 if (get_token(1, 1) < 0)
598 goto err_out_parse_error
;
599 if (!strcasecmp(token
[0], "working"))
600 codec
->status
= CODECS_STATUS_WORKING
;
601 else if (!strcasecmp(token
[0], "crashing"))
602 codec
->status
= CODECS_STATUS_NOT_WORKING
;
603 else if (!strcasecmp(token
[0], "untested"))
604 codec
->status
= CODECS_STATUS_UNTESTED
;
605 else if (!strcasecmp(token
[0], "buggy"))
606 codec
->status
= CODECS_STATUS_PROBLEMS
;
608 goto err_out_parse_error
;
609 } else if (!strcmp(token
[0], "anyinput")) {
610 codec
->anyinput
= true;
612 goto err_out_parse_error
;
614 if (!validate_codec(codec
, codec_type
))
615 goto err_out_not_valid
;
616 mp_tmsg(MSGT_CODECCFG
, MSGL_V
, "%d audio & %d video codecs\n", nr_acodecs
,
618 if(video_codecs
) video_codecs
[nr_vcodecs
].name
= NULL
;
619 if(audio_codecs
) audio_codecs
[nr_acodecs
].name
= NULL
;
626 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"parse error");
627 err_out_print_linenum
:
630 codecs_uninit_free();
637 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"Codec is not defined correctly.");
638 goto err_out_print_linenum
;
640 mp_tmsg(MSGT_CODECCFG
,MSGL_ERR
,"This codecs.conf is too old and incompatible with this MPlayer release!");
641 goto err_out_print_linenum
;
644 static void codecs_free(codecs_t
* codecs
,int count
) {
646 for ( i
= 0; i
< count
; i
++)
647 if ( codecs
[i
].name
) {
648 free(codecs
[i
].name
);
649 free(codecs
[i
].info
);
650 free(codecs
[i
].comment
);
657 void codecs_uninit_free(void) {
659 codecs_free(video_codecs
,nr_vcodecs
);
662 codecs_free(audio_codecs
,nr_acodecs
);
666 codecs_t
*find_audio_codec(unsigned int fourcc
, unsigned int *fourccmap
,
667 codecs_t
*start
, int force
)
669 return find_codec(fourcc
, fourccmap
, start
, 1, force
);
672 codecs_t
*find_video_codec(unsigned int fourcc
, unsigned int *fourccmap
,
673 codecs_t
*start
, int force
)
675 return find_codec(fourcc
, fourccmap
, start
, 0, force
);
678 struct codecs
*find_codec(unsigned int fourcc
, unsigned int *fourccmap
,
679 codecs_t
*start
, int audioflag
, int force
)
681 struct codecs
*c
, *end
;
685 end
= c
+ nr_acodecs
;
688 end
= c
+ nr_vcodecs
;
691 c
= start
+ 1; // actually starts from the next one after the given one
692 for (; c
< end
; c
++) {
693 for (int j
= 0; j
< CODECS_MAX_FOURCC
; j
++) {
694 if (c
->fourcc
[j
] == -1)
696 if (c
->fourcc
[j
] == fourcc
) {
698 *fourccmap
= c
->fourccmap
[j
];
702 if (c
->anyinput
|| force
)
708 void stringset_init(stringset_t
*set
) {
709 *set
= calloc(1, sizeof(char *));
712 void stringset_free(stringset_t
*set
) {
714 while ((*set
)[count
]) free((*set
)[count
++]);
719 void stringset_add(stringset_t
*set
, const char *str
) {
721 while ((*set
)[count
]) count
++;
723 *set
= realloc(*set
, sizeof(char *) * (count
+ 1));
724 (*set
)[count
- 1] = strdup(str
);
725 (*set
)[count
] = NULL
;
728 int stringset_test(stringset_t
*set
, const char *str
) {
730 for (s
= *set
; *s
; s
++)
731 if (strcmp(*s
, str
) == 0)
736 void list_codecs(int audioflag
){
743 mp_msg(MSGT_CODECCFG
,MSGL_INFO
,"ac: afm: status: info: [lib/dll]\n");
747 mp_msg(MSGT_CODECCFG
,MSGL_INFO
,"vc: vfm: status: info: [lib/dll]\n");
750 for (/* NOTHING */; i
--; c
++) {
753 case CODECS_STATUS_WORKING
: s
="working ";break;
754 case CODECS_STATUS_PROBLEMS
: s
="problems";break;
755 case CODECS_STATUS_NOT_WORKING
: s
="crashing";break;
756 case CODECS_STATUS_UNTESTED
: s
="untested";break;
759 mp_msg(MSGT_CODECCFG
,MSGL_INFO
,"%-11s %-9s %s %s [%s]\n",c
->name
,c
->drv
,s
,c
->info
,c
->dll
);
761 mp_msg(MSGT_CODECCFG
,MSGL_INFO
,"%-11s %-9s %s %s\n",c
->name
,c
->drv
,s
,c
->info
);