[x265] [PATCH 1 of 2] zone: add support to parse zone files

Mateusz mateuszb at poczta.onet.pl
Wed Dec 26 21:17:09 CET 2018


W dniu 20.12.2018 o 12:03, bhavna at multicorewareinc.com pisze:
> diff -r 1f44f1f1623d -r 592b83c9068d source/x265.cpp
> --- a/source/x265.cpp	Mon Dec 17 16:49:08 2018 +0530
> +++ b/source/x265.cpp	Fri Dec 14 12:04:35 2018 +0530
> @@ -74,6 +74,7 @@
>      ReconFile* recon;
>      OutputFile* output;
>      FILE*       qpfile;
> +    FILE*       zoneFile;
>      FILE*    dolbyVisionRpu;    /* File containing Dolby Vision BL RPU metadata */
>      const char* reconPlayCmd;
>      const x265_api* api;
> @@ -97,6 +98,7 @@
>          recon = NULL;
>          output = NULL;
>          qpfile = NULL;
> +        zoneFile = NULL;
>          dolbyVisionRpu = NULL;
>          reconPlayCmd = NULL;
>          api = NULL;
> @@ -114,7 +116,9 @@
>      void destroy();
>      void printStatus(uint32_t frameNum);
>      bool parse(int argc, char **argv);
> +    bool parseZoneParam(int argc, char **argv, x265_param* globalParam, int zonefileCount);
>      bool parseQPFile(x265_picture &pic_org);
> +    bool parseZoneFile();
>  };
>  
>  void CLIOptions::destroy()
> @@ -128,6 +132,9 @@
>      if (qpfile)
>          fclose(qpfile);
>      qpfile = NULL;
> +    if (zoneFile)
> +        fclose(zoneFile);
> +    zoneFile = NULL;
>      if (dolbyVisionRpu)
>          fclose(dolbyVisionRpu);
>      dolbyVisionRpu = NULL;
> @@ -163,6 +170,110 @@
>      prevUpdateTime = time;
>  }
>  
> +bool CLIOptions::parseZoneParam(int argc, char **argv, x265_param* globalParam, int zonefileCount)
> +{
> +    bool bError = false;
> +    int bShowHelp = false;
> +    int outputBitDepth = 0;
> +    const char *profile = NULL;
> +
> +    /* Presets are applied before all other options. */
> +    for (optind = 0;;)
> +    {
> +        int c = getopt_long(argc, argv, short_options, long_options, NULL);
> +        if (c == -1)
> +            break;
> +        else if (c == 'D')
> +            outputBitDepth = atoi(optarg);
> +        else if (c == 'P')
> +            profile = optarg;
> +        else if (c == '?')
> +            bShowHelp = true;
> +    }
> +
> +    if (!outputBitDepth && profile)
> +    {
> +        /* try to derive the output bit depth from the requested profile */
> +        if (strstr(profile, "10"))
> +            outputBitDepth = 10;
> +        else if (strstr(profile, "12"))
> +            outputBitDepth = 12;
> +        else
> +            outputBitDepth = 8;
> +    }
> +
> +    api = x265_api_get(outputBitDepth);
> +    if (!api)
> +    {
> +        x265_log(NULL, X265_LOG_WARNING, "falling back to default bit-depth\n");
> +        api = x265_api_get(0);
> +    }
> +
> +    if (bShowHelp)
> +    {
> +        printVersion(globalParam, api);
> +        showHelp(globalParam);
> +    }
> +
> +    globalParam->rc.zones[zonefileCount].zoneParam = api->param_alloc();
> +    if (!globalParam->rc.zones[zonefileCount].zoneParam)
> +    {
> +        x265_log(NULL, X265_LOG_ERROR, "param alloc failed\n");
> +        return true;
> +    }
> +
> +    memcpy(globalParam->rc.zones[zonefileCount].zoneParam, globalParam, sizeof(x265_param));
> +
> +    for (optind = 0;;)
> +    {
> +        int long_options_index = -1;
> +        int c = getopt_long(argc, argv, short_options, long_options, &long_options_index);
> +        if (c == -1)
> +            break;
> +
> +        if (long_options_index < 0 && c > 0)
> +        {
> +            for (size_t i = 0; i < sizeof(long_options) / sizeof(long_options[0]); i++)
> +            {
> +                if (long_options[i].val == c)
> +                {
> +                    long_options_index = (int)i;
> +                    break;
> +                }
> +            }
> +
> +            if (long_options_index < 0)
> +            {
> +                /* getopt_long might have already printed an error message */
> +                if (c != 63)
> +                    x265_log(NULL, X265_LOG_WARNING, "internal error: short option '%c' has no long option\n", c);
> +                return true;
> +            }
> +        }
> +        if (long_options_index < 0)
> +        {
> +            x265_log(NULL, X265_LOG_WARNING, "short option '%c' unrecognized\n", c);
> +            return true;
> +        }
> +
> +        bError |= !!api->zone_param_parse(globalParam->rc.zones[zonefileCount].zoneParam, long_options[long_options_index].name, optarg);
> +
> +        if (bError)
> +        {
> +            const char *name = long_options_index > 0 ? long_options[long_options_index].name : argv[optind - 2];
> +            x265_log(NULL, X265_LOG_ERROR, "invalid argument: %s = %s\n", name, optarg);
> +            return true;
> +        }
> +    }
> +
> +    if (optind < argc)
> +    {
> +        x265_log(param, X265_LOG_WARNING, "extra unused command arguments given <%s>\n", argv[optind]);
> +        return true;
> +    }
> +    return false;
> +}
> +
>  bool CLIOptions::parse(int argc, char **argv)
>  {
>      bool bError = false;
> @@ -327,6 +438,12 @@
>                      return true;
>                  }
>              }
> +            OPT("zonefile")
> +            {
> +                this->zoneFile = x265_fopen(optarg, "rb");
> +                if (!this->zoneFile)
> +                    x265_log_file(param, X265_LOG_ERROR, "%s zone file not found or error in opening zone file\n", optarg);
> +            }
>              OPT("fullhelp")
>              {
>                  param->logLevel = X265_LOG_FULL;
> @@ -535,6 +652,59 @@
>      return 1;
>  }
>  
> +bool CLIOptions::parseZoneFile()
> +{
> +    char line[256];
> +    char* argLine;
> +    param->rc.zonefileCount = 0;
> +
> +    while (fgets(line, sizeof(line), zoneFile))
> +    {
> +        if (!((*line == '#') || (strcmp(line, "\r\n") == 0)))
> +            param->rc.zonefileCount++;
> +    }
> +
> +    rewind(zoneFile);
> +    param->rc.zones = X265_MALLOC(x265_zone, param->rc.zonefileCount);
> +    for (int i = 0; i < param->rc.zonefileCount; i++)
> +    {
> +        while (fgets(line, sizeof(line), zoneFile))
> +        {
> +            if (*line == '#' || (strcmp(line, "\r\n") == 0))
> +                continue;
> +            param->rc.zones[i].zoneParam = X265_MALLOC(x265_param, 1);
> +            int index = (int)strcspn(line, "\r\n");
> +            line[index] = '\0';
> +            argLine = line;
> +            while (isspace((unsigned char)*argLine)) argLine++;
> +            char* start = strchr(argLine, ' ');
> +            start++;
> +            param->rc.zones[i].startFrame = atoi(argLine);
> +            int argCount = 0;
> +            char **args = (char**)malloc(256 * sizeof(char *));
> +            // Adding a dummy string to avoid file parsing error
> +            args[argCount++] = (char *)"x265";
> +            char* token = strtok(start, " ");
> +            while (token) 
> +            {
> +                args[argCount++] = token;
> +                token = strtok(NULL, " ");
> +            }
> +            args[argCount] = '\0';

Maybe it's better to use NULL instead of '\0' for zeroing pointer.

Regards,
Mateusz



More information about the x265-devel mailing list