📄 codec-cfg.c
字号:
/* * codec.conf parser * by Szabolcs Berecz <szabi@inf.elte.hu> * (C) 2001 * * to compile tester app: gcc -Iloader/ -DTESTING -o codec-cfg codec-cfg.c * to compile CODECS2HTML: gcc -Iloader/ -DCODECS2HTML -o codecs2html codecs-cfg.c * * TODO: implement informat in CODECS2HTML too */#define DEBUG//disable asserts#define NDEBUG #include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <unistd.h>#include <errno.h>#include <ctype.h>#include <assert.h>#include <string.h>#include "config.h"#include "mp_msg.h"#include "help_mp.h"// for mmioFOURCC:#include "libmpdemux/aviheader.h"#include "libmpcodecs/img_format.h"#include "codec-cfg.h"#ifndef CODECS2HTML#include "codecs.conf.h"#endif#define PRINT_LINENUM mp_msg(MSGT_CODECCFG,MSGL_ERR," at line %d\n", line_num)#define MAX_NR_TOKEN 16#define MAX_LINE_LEN 1000#define RET_EOF -1#define RET_EOL -2#define TYPE_VIDEO 0#define TYPE_AUDIO 1char * codecs_file = NULL;static int add_to_fourcc(char *s, char *alias, unsigned int *fourcc, unsigned int *map){ int i, j, freeslots; unsigned int tmp; /* find first unused slot */ for (i = 0; i < CODECS_MAX_FOURCC && fourcc[i] != 0xffffffff; i++) /* NOTHING */; freeslots = CODECS_MAX_FOURCC - i; if (!freeslots) goto err_out_too_many; do { tmp = mmioFOURCC(s[0], s[1], s[2], s[3]); for (j = 0; j < i; j++) if (tmp == fourcc[j]) goto err_out_duplicated; fourcc[i] = tmp; map[i] = alias ? mmioFOURCC(alias[0], alias[1], alias[2], alias[3]) : tmp; s += 4; i++; } while ((*(s++) == ',') && --freeslots); if (!freeslots) goto err_out_too_many; if (*(--s) != '\0') goto err_out_parse_error; return 1;err_out_duplicated: mp_msg(MSGT_CODECCFG,MSGL_ERR,MSGTR_DuplicateFourcc); return 0;err_out_too_many: mp_msg(MSGT_CODECCFG,MSGL_ERR,MSGTR_TooManyFourccs); return 0;err_out_parse_error: mp_msg(MSGT_CODECCFG,MSGL_ERR,MSGTR_ParseError); return 0;}static int add_to_format(char *s, char *alias,unsigned int *fourcc, unsigned int *fourccmap){ int i, j; char *endptr; /* find first unused slot */ for (i = 0; i < CODECS_MAX_FOURCC && fourcc[i] != 0xffffffff; i++) /* NOTHING */; if (i == CODECS_MAX_FOURCC) { mp_msg(MSGT_CODECCFG,MSGL_ERR,MSGTR_TooManyFourccs); return 0; } fourcc[i]=strtoul(s,&endptr,0); if (*endptr != '\0') { mp_msg(MSGT_CODECCFG,MSGL_ERR,MSGTR_ParseErrorFIDNotNumber); return 0; } if(alias){ fourccmap[i]=strtoul(alias,&endptr,0); if (*endptr != '\0') { mp_msg(MSGT_CODECCFG,MSGL_ERR,MSGTR_ParseErrorFIDAliasNotNumber); return 0; } } else fourccmap[i]=fourcc[i]; for (j = 0; j < i; j++) if (fourcc[j] == fourcc[i]) { mp_msg(MSGT_CODECCFG,MSGL_ERR,MSGTR_DuplicateFID); return 0; } return 1;} static struct { const char *name; const unsigned int num; } fmt_table[] = { {"YV12", IMGFMT_YV12}, {"I420", IMGFMT_I420}, {"IYUV", IMGFMT_IYUV}, {"NV12", IMGFMT_NV12}, {"NV21", IMGFMT_NV21}, {"YVU9", IMGFMT_YVU9}, {"IF09", IMGFMT_IF09}, {"444P", IMGFMT_444P}, {"422P", IMGFMT_422P}, {"411P", IMGFMT_411P}, {"Y800", IMGFMT_Y800}, {"Y8", IMGFMT_Y8}, {"YUY2", IMGFMT_YUY2}, {"UYVY", IMGFMT_UYVY}, {"YVYU", IMGFMT_YVYU}, {"RGB4", IMGFMT_RGB|4}, {"RGB8", IMGFMT_RGB|8}, {"RGB15", IMGFMT_RGB|15}, {"RGB16", IMGFMT_RGB|16}, {"RGB24", IMGFMT_RGB|24}, {"RGB32", IMGFMT_RGB|32}, {"BGR4", IMGFMT_BGR|4}, {"BGR8", IMGFMT_BGR|8}, {"BGR15", IMGFMT_BGR|15}, {"BGR16", IMGFMT_BGR|16}, {"BGR24", IMGFMT_BGR|24}, {"BGR32", IMGFMT_BGR|32}, {"RGB1", IMGFMT_RGB|1}, {"BGR1", IMGFMT_BGR|1}, {"MPES", IMGFMT_MPEGPES}, {"ZRMJPEGNI", IMGFMT_ZRMJPEGNI}, {"ZRMJPEGIT", IMGFMT_ZRMJPEGIT}, {"ZRMJPEGIB", IMGFMT_ZRMJPEGIB}, {"IDCT_MPEG2",IMGFMT_XVMC_IDCT_MPEG2}, {"MOCO_MPEG2",IMGFMT_XVMC_MOCO_MPEG2}, {NULL, 0} };static int add_to_inout(char *sfmt, char *sflags, unsigned int *outfmt, unsigned char *outflags){ static char *flagstr[] = { "flip", "noflip", "yuvhack", "query", "static", NULL }; int i, j, freeslots; unsigned char flags; for (i = 0; i < CODECS_MAX_OUTFMT && outfmt[i] != 0xffffffff; i++) /* NOTHING */; freeslots = CODECS_MAX_OUTFMT - i; if (!freeslots) goto err_out_too_many; flags = 0; if(sflags) { do { for (j = 0; flagstr[j] != NULL; j++) if (!strncmp(sflags, flagstr[j], strlen(flagstr[j]))) break; if (flagstr[j] == NULL) goto err_out_parse_error; flags|=(1<<j); sflags+=strlen(flagstr[j]); } while (*(sflags++) == ','); if (*(--sflags) != '\0') goto err_out_parse_error; } do { for (j = 0; fmt_table[j].name != NULL; j++) if (!strncmp(sfmt, fmt_table[j].name, strlen(fmt_table[j].name))) break; if (fmt_table[j].name == NULL) goto err_out_parse_error; outfmt[i] = fmt_table[j].num; outflags[i] = flags; ++i; sfmt+=strlen(fmt_table[j].name); } while ((*(sfmt++) == ',') && --freeslots); if (!freeslots) goto err_out_too_many; if (*(--sfmt) != '\0') goto err_out_parse_error; return 1;err_out_too_many: mp_msg(MSGT_CODECCFG,MSGL_ERR,MSGTR_TooManyOut); return 0;err_out_parse_error: mp_msg(MSGT_CODECCFG,MSGL_ERR,MSGTR_ParseError); return 0;}#if 0static short get_driver(char *s,int audioflag){ static char *audiodrv[] = { "null", "mp3lib", "pcm", "libac3", "acm", "alaw", "msgsm", "dshow", "dvdpcm", "hwac3", "libvorbis", "ffmpeg", "libmad", "msadpcm", "liba52", "g72x", "imaadpcm", "dk4adpcm", "dk3adpcm", "roqaudio", "faad", "realaud", "libdv", NULL }; static char *videodrv[] = { "null", "libmpeg2", "vfw", "odivx", "dshow", "ffmpeg", "vfwex", "divx4", "raw", "msrle", "xanim", "msvidc", "fli", "cinepak", "qtrle", "nuv", "cyuv", "qtsmc", "ducktm1", "roqvideo", "qtrpza", "mpng", "ijpg", "zlib", "mpegpes", "zrmjpeg", "realvid", "xvid", "libdv", NULL }; char **drv=audioflag?audiodrv:videodrv; int i; for(i=0;drv[i];i++) if(!strcmp(s,drv[i])) return i; return -1;}#endifstatic int validate_codec(codecs_t *c, int type){ unsigned int i; char *tmp_name = c->name; for (i = 0; i < strlen(tmp_name) && isalnum(tmp_name[i]); i++) /* NOTHING */; if (i < strlen(tmp_name)) { mp_msg(MSGT_CODECCFG,MSGL_ERR,MSGTR_InvalidCodecName, c->name); return 0; } if (!c->info) c->info = strdup(c->name);#if 0 if (c->fourcc[0] == 0xffffffff) { mp_msg(MSGT_CODECCFG,MSGL_ERR,MSGTR_CodecLacksFourcc, c->name); return 0; }#endif if (!c->drv) { mp_msg(MSGT_CODECCFG,MSGL_ERR,MSGTR_CodecLacksDriver, c->name); return 0; }#if 0#warning codec->driver == 4;... <- ezt nem kellene belehegeszteni...#warning HOL VANNAK DEFINIALVA???????????? if (!c->dll && (c->driver == 4 || (c->driver == 2 && type == TYPE_VIDEO))) { mp_msg(MSGT_CODECCFG,MSGL_ERR,MSGTR_CodecNeedsDLL, c->name); return 0; }#warning guid.f1 lehet 0? honnan lehet tudni, hogy nem adtak meg?// if (!(codec->flags & CODECS_FLAG_AUDIO) && codec->driver == 4) if (type == TYPE_VIDEO) if (c->outfmt[0] == 0xffffffff) { mp_msg(MSGT_CODECCFG,MSGL_ERR,MSGTR_CodecNeedsOutfmt, c->name); return 0; }#endif return 1;}static int add_comment(char *s, char **d){ int pos; if (!*d) pos = 0; else { pos = strlen(*d); (*d)[pos++] = '\n'; } if (!(*d = (char *) realloc(*d, pos + strlen(s) + 1))) { mp_msg(MSGT_CODECCFG,MSGL_FATAL,MSGTR_CantAllocateComment); return 0; } strcpy(*d + pos, s); return 1;}static short get_cpuflags(char *s){ static char *flagstr[] = { "mmx", "sse", "3dnow", NULL }; int i; short flags = 0; do { for (i = 0; flagstr[i]; i++) if (!strncmp(s, flagstr[i], strlen(flagstr[i]))) break; if (!flagstr[i]) goto err_out_parse_error; flags |= 1<<i; s += strlen(flagstr[i]); } while (*(s++) == ','); if (*(--s) != '\0') goto err_out_parse_error; return flags;err_out_parse_error: return 0;}static FILE *fp;static int line_num = 0;static char *line;static char *token[MAX_NR_TOKEN];static int read_nextline = 1;static int get_token(int min, int max){ static int line_pos; int i; char c; if (max >= MAX_NR_TOKEN) { mp_msg(MSGT_CODECCFG,MSGL_ERR,MSGTR_GetTokenMaxNotLessThanMAX_NR_TOKEN); goto out_eof; } memset(token, 0x00, sizeof(*token) * max); if (read_nextline) { if (!fgets(line, MAX_LINE_LEN, fp)) goto out_eof; line_pos = 0; ++line_num; read_nextline = 0; } for (i = 0; i < max; i++) { while (isspace(line[line_pos])) ++line_pos; if (line[line_pos] == '\0' || line[line_pos] == '#' || line[line_pos] == ';') { read_nextline = 1; if (i >= min) goto out_ok; goto out_eol; } token[i] = line + line_pos; c = line[line_pos]; if (c == '"' || c == '\'') { token[i]++; while (line[++line_pos] != c && line[line_pos]) /* NOTHING */; } else { for (/* NOTHING */; !isspace(line[line_pos]) && line[line_pos]; line_pos++) /* NOTHING */; } if (!line[line_pos]) { read_nextline = 1; if (i >= min - 1) goto out_ok; goto out_eol; } line[line_pos] = '\0'; line_pos++; }out_ok: return i;out_eof: read_nextline = 1; return RET_EOF;out_eol: return RET_EOL;}static codecs_t *video_codecs=NULL;static codecs_t *audio_codecs=NULL;static int nr_vcodecs = 0;static int nr_acodecs = 0;int parse_codec_cfg(char *cfgfile){ codecs_t *codec = NULL; // current codec codecs_t **codecsp = NULL;// points to audio_codecs or to video_codecs char *endptr; // strtoul()... int *nr_codecsp; int codec_type; /* TYPE_VIDEO/TYPE_AUDIO */ int tmp, i; // in case we call it a second time codecs_uninit_free(); nr_vcodecs = 0; nr_acodecs = 0; if(cfgfile==NULL) {#ifdef CODECS2HTML return 0; #else video_codecs = builtin_video_codecs; audio_codecs = builtin_audio_codecs; nr_vcodecs = sizeof(builtin_video_codecs)/sizeof(codecs_t); nr_acodecs = sizeof(builtin_audio_codecs)/sizeof(codecs_t); return 1;#endif } mp_msg(MSGT_CODECCFG,MSGL_V,MSGTR_ReadingFile, cfgfile); if ((fp = fopen(cfgfile, "r")) == NULL) { mp_msg(MSGT_CODECCFG,MSGL_V,MSGTR_CantOpenFileError, cfgfile, strerror(errno)); return 0; } if ((line = (char *) malloc(MAX_LINE_LEN + 1)) == NULL) { mp_msg(MSGT_CODECCFG,MSGL_FATAL,MSGTR_CantGetMemoryForLine, strerror(errno)); return 0; } read_nextline = 1; /* * this only catches release lines at the start of * codecs.conf, before audiocodecs and videocodecs. */ while ((tmp = get_token(1, 1)) == RET_EOL) /* NOTHING */; if (tmp == RET_EOF) goto out; if (!strcmp(token[0], "release")) { if (get_token(1, 2) < 0) goto err_out_parse_error; tmp = atoi(token[0]); if (tmp < CODEC_CFG_MIN) goto err_out_release_num; while ((tmp = get_token(1, 1)) == RET_EOL) /* NOTHING */; if (tmp == RET_EOF) goto out; } else goto err_out_release_num; /* * check if the next block starts with 'audiocodec' or * with 'videocodec' */ if (!strcmp(token[0], "audiocodec") || !strcmp(token[0], "videocodec")) goto loop_enter; goto err_out_parse_error; while ((tmp = get_token(1, 1)) != RET_EOF) { if (tmp == RET_EOL) continue; if (!strcmp(token[0], "audiocodec") || !strcmp(token[0], "videocodec")) { if (!validate_codec(codec, codec_type)) goto err_out_not_valid; loop_enter: if (*token[0] == 'v') { codec_type = TYPE_VIDEO; nr_codecsp = &nr_vcodecs; codecsp = &video_codecs; } else if (*token[0] == 'a') { codec_type = TYPE_AUDIO; nr_codecsp = &nr_acodecs; codecsp = &audio_codecs;#ifdef DEBUG } else { mp_msg(MSGT_CODECCFG,MSGL_ERR,"picsba\n"); goto err_out;#endif } if (!(*codecsp = (codecs_t *) realloc(*codecsp, sizeof(codecs_t) * (*nr_codecsp + 2)))) { mp_msg(MSGT_CODECCFG,MSGL_FATAL,MSGTR_CantReallocCodecsp, strerror(errno)); goto err_out; } codec=*codecsp + *nr_codecsp; ++*nr_codecsp; memset(codec,0,sizeof(codecs_t)); memset(codec->fourcc, 0xff, sizeof(codec->fourcc)); memset(codec->outfmt, 0xff, sizeof(codec->outfmt)); memset(codec->infmt, 0xff, sizeof(codec->infmt)); if (get_token(1, 1) < 0) goto err_out_parse_error; for (i = 0; i < *nr_codecsp - 1; i++) { if(( (*codecsp)[i].name!=NULL) && (!strcmp(token[0], (*codecsp)[i].name)) ) { mp_msg(MSGT_CODECCFG,MSGL_ERR,MSGTR_CodecNameNotUnique, token[0]); goto err_out_print_linenum; } } if (!(codec->name = strdup(token[0]))) { mp_msg(MSGT_CODECCFG,MSGL_ERR,MSGTR_CantStrdupName, strerror(errno)); goto err_out; } } else if (!strcmp(token[0], "info")) { if (codec->info || get_token(1, 1) < 0) goto err_out_parse_error; if (!(codec->info = strdup(token[0]))) { mp_msg(MSGT_CODECCFG,MSGL_ERR,MSGTR_CantStrdupInfo, strerror(errno)); goto err_out; } } else if (!strcmp(token[0], "comment")) { if (get_token(1, 1) < 0) goto err_out_parse_error; add_comment(token[0], &codec->comment);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -