📄 config.c
字号:
/*
*****************************************************************************
* COPYRIGHT AND WARRANTY INFORMATION
*
* Copyright 2003, Advanced Audio Video Coding Standard, Part II
*
* DISCLAIMER OF WARRANTY
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations under
* the License.
*
* THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE AVS PATENT POLICY.
* The AVS Working Group doesn't represent or warrant that the programs
* furnished here under are free of infringement of any third-party patents.
* Commercial implementations of AVS, including shareware, may be
* subject to royalty fees to patent holders. Information regarding
* the AVS patent policy for standardization procedure is available at
* AVS Web site http://www.avs.org.cn. Patent Licensing is outside
* of AVS Working Group.
*
* The Original Code is Reference Software for China National Standard
* GB/T 20090.2-2006 (short for AVS-P2 or AVS Video) at version RM52J.
*
* The Initial Developer of the Original Code is Video subgroup of AVS
* Workinggroup (Audio and Video coding Standard Working Group of China).
* Contributors: Guoping Li, Siwei Ma, Jian Lou, Qiang Wang ,
* Jianwen Chen,Haiwu Zhao, Xiaozhen Zheng, Junhao Zheng, Zhiming Wang
*
******************************************************************************
*/
/*
*************************************************************************************
* File name:
* Function:
*
*************************************************************************************
*/
#define INCLUDED_BY_CONFIGFILE_C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "global.h"
#include "configfile.h"
#define MAX_ITEMS_TO_PARSE 10000
static char *GetConfigFileContent (char *Filename);
static void ParseContent (char *buf, int bufsize);
static int ParameterNameToMapIndex (char *s);
static void PatchInp ();
static void ProfileCheck();
static void LevelCheck();
/*
*************************************************************************
* Function:Parse the command line parameters and read the config files.
* Input: ac
number of command line parameters
av
command line parameters
* Output:
* Return:
* Attention:
*************************************************************************
*/
void Configure (int ac, char *av[])
{
char *content;
int CLcount, ContentLen, NumberParams;
memset (&configinput, 0, sizeof (InputParameters));
// Process default config file
// Parse the command line
CLcount = 1;
while (CLcount < ac)
{
if (0 == strncmp (av[CLcount], "-f", 2)) // A file parameter?
{
content = GetConfigFileContent (av[CLcount+1]);
printf ("Parsing Configfile %s", av[CLcount+1]);
ParseContent (content, strlen (content));
printf ("\n");
free (content);
CLcount += 2;
}
else
{
if (0 == strncmp (av[CLcount], "-p", 2)) // A config change?
{
// Collect all data until next parameter (starting with -<x> (x is any character)),
// put it into content, and parse content.
CLcount++;
ContentLen = 0;
NumberParams = CLcount;
// determine the necessary size for content
while (NumberParams < ac && av[NumberParams][0] != '-')
ContentLen += strlen (av[NumberParams++]); // Space for all the strings
ContentLen += 1000; // Additional 1000 bytes for spaces and \0s
if ((content = malloc (ContentLen))==NULL)
no_mem_exit("Configure: content");
content[0] = '\0';
// concatenate all parameters itendified before
while (CLcount < NumberParams)
{
char *source = &av[CLcount][0];
char *destin = &content[strlen (content)];
while (*source != '\0')
{
if (*source == '=') // The Parser expects whitespace before and after '='
{
*destin++=' '; *destin++='='; *destin++=' '; // Hence make sure we add it
}
else
*destin++=*source;
source++;
}
*destin = '\0';
CLcount++;
}
printf ("Parsing command line string '%s'", content);
ParseContent (content, strlen(content));
free (content);
printf ("\n");
}
else
{
snprintf (errortext, ET_SIZE, "Error in command line, ac %d, around string '%s', missing -f or -p parameters?", CLcount, av[CLcount]);
error (errortext, 300);
}
}
}
printf ("\n");
}
/*
*************************************************************************
* Function: Alocate memory buf, opens file Filename in f, reads contents into
buf and returns buf
* Input:name of config file
* Output:
* Return:
* Attention:
*************************************************************************
*/
char *GetConfigFileContent (char *Filename)
{
int FileSize;
FILE *f;
char *buf;
if (NULL == (f = fopen (Filename, "r")))
{
snprintf (errortext, ET_SIZE, "Cannot open configuration file %s.\n", Filename);
error (errortext, 300);
}
if (0 != fseek (f, 0, SEEK_END))
{
snprintf (errortext, ET_SIZE, "Cannot fseek in configuration file %s.\n", Filename);
error (errortext, 300);
}
FileSize = ftell (f);
if (FileSize < 0 || FileSize > 60000)
{
snprintf (errortext, ET_SIZE, "Unreasonable Filesize %d reported by ftell for configuration file %s.\n", FileSize, Filename);
error (errortext, 300);
}
if (0 != fseek (f, 0, SEEK_SET))
{
snprintf (errortext, ET_SIZE, "Cannot fseek in configuration file %s.\n", Filename);
error (errortext, 300);
}
if ((buf = malloc (FileSize + 1))==NULL)
no_mem_exit("GetConfigFileContent: buf");
// Note that ftell() gives us the file size as the file system sees it. The actual file size,
// as reported by fread() below will be often smaller due to CR/LF to CR conversion and/or
// control characters after the dos EOF marker in the file.
FileSize = fread (buf, 1, FileSize, f);
buf[FileSize] = '\0';
fclose (f);
return buf;
}
/*
*************************************************************************
* Function: Parses the character array buf and writes global variable input, which is defined in
configfile.h. This hack will continue to be necessary to facilitate the addition of
new parameters through the Map[] mechanism (Need compiler-generated addresses in map[]).
* Input: buf
buffer to be parsed
bufsize
buffer size of buffer
* Output:
* Return:
* Attention:
*************************************************************************
*/
void ParseContent (char *buf, int bufsize)
{
char *items[MAX_ITEMS_TO_PARSE];
int MapIdx;
int item = 0;
int InString = 0;
int InItem = 0;
char *p = buf;
char *bufend = &buf[bufsize];
int IntContent;
int i;
// Stage one: Generate an argc/argv-type list in items[], without comments and whitespace.
// This is context insensitive and could be done most easily with lex(1).
while (p < bufend)
{
switch (*p)
{
case 13:
p++;
break;
case '#': // Found comment
*p = '\0'; // Replace '#' with '\0' in case of comment immediately following integer or string
while (*p != '\n' && p < bufend) // Skip till EOL or EOF, whichever comes first
p++;
InString = 0;
InItem = 0;
break;
case '\n':
InItem = 0;
InString = 0;
*p++='\0';
break;
case ' ':
case '\t': // Skip whitespace, leave state unchanged
if (InString)
p++;
else
{ // Terminate non-strings once whitespace is found
*p++ = '\0';
InItem = 0;
}
break;
case '"': // Begin/End of String
*p++ = '\0';
if (!InString)
{
items[item++] = p;
InItem = ~InItem;
}
else
InItem = 0;
InString = ~InString; // Toggle
break;
default:
if (!InItem)
{
items[item++] = p;
InItem = ~InItem;
}
p++;
}
}
item--;
for (i=0; i<item; i+= 3)
{
if (0 > (MapIdx = ParameterNameToMapIndex (items[i])))
{
snprintf (errortext, ET_SIZE, " Parsing error in config file: Parameter Name '%s' not recognized.", items[i]);
error (errortext, 300);
}
if (strcmp ("=", items[i+1]))
{
snprintf (errortext, ET_SIZE, " Parsing error in config file: '=' expected as the second token in each line.");
error (errortext, 300);
}
// Now interprete the Value, context sensitive...
switch (Map[MapIdx].Type)
{
case 0: // Numerical
if (1 != sscanf (items[i+2], "%d", &IntContent))
{
snprintf (errortext, ET_SIZE, " Parsing error: Expected numerical value for Parameter of %s, found '%s'.", items[i], items[i+2]);
error (errortext, 300);
}
* (int *) (Map[MapIdx].Place) = IntContent;
printf (".");
break;
case 1:
strcpy ((char *) Map[MapIdx].Place, items [i+2]);
printf (".");
break;
}
}
memcpy (input, &configinput, sizeof (InputParameters));
PatchInp();
}
/*
*************************************************************************
* Function:Return the index number from Map[] for a given parameter name.
* Input:parameter name string
* Output:
* Return: the index number if the string is a valid parameter name, \n
-1 for error
* Attention:
*************************************************************************
*/
static int ParameterNameToMapIndex (char *s)
{
int i = 0;
while (Map[i].TokenName != NULL)
if (0==strcmp (Map[i].TokenName, s))
return i;
else
i++;
return -1;
}
// Added by LiShao, Tsinghua, 20070327
static void ProfileCheck()
{
switch (input->profile_id)
{
case 0x20:
{
if(input->level_id!=0x10&&input->level_id!=0x20&&input->level_id!=0x22&&input->level_id!=0x40&&input->level_id!=0x42)
{
printf("\n Level_ID invalid or Unsupported. \n");
exit(-1);
}
if(input->InterlaceCodingOption>2)
{
printf("\n The value of 'InterlaceCodingOption' is invalid! \n");
exit(-1);
}
if(input->chroma_format!=1&&input->chroma_format!=2)
{
printf("\n Chroma_format invaild.\n");
exit(-1);
}
break;
}
case 0x40: printf("\n Current Profile_ID is not 1.0 version. \n"); break;
default:
{
printf("\n Profile_ID invaild.\n");
exit(-1);
}
}
}
// Added by LiShao, Tsinghua, 20070327
static void LevelCheck()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -