File: x265.cpp

package info (click to toggle)
x265 4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,408 kB
  • sloc: asm: 187,063; cpp: 118,996; ansic: 741; makefile: 146; sh: 91; python: 11
file content (366 lines) | stat: -rw-r--r-- 11,109 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*****************************************************************************
 * Copyright (C) 2013-2020 MulticoreWare, Inc
 *
 * Authors: Steve Borho <steve@borho.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
 *
 * This program is also available under a commercial proprietary license.
 * For more information, contact us at license @ x265.com.
 *****************************************************************************/

#if _MSC_VER
#pragma warning(disable: 4127) // conditional expression is constant, yes I know
#endif

#include "x265.h"
#include "x265cli.h"
#include "abrEncApp.h"

#if HAVE_VLD
/* Visual Leak Detector */
#include <vld.h>
#endif

#include <signal.h>
#include <errno.h>
#include <fcntl.h>

#include <string>
#include <ostream>
#include <fstream>
#include <queue>

using namespace X265_NS;

#define X265_HEAD_ENTRIES 3
#define CONSOLE_TITLE_SIZE 200

#ifdef _WIN32
#define strdup _strdup
static char orgConsoleTitle[CONSOLE_TITLE_SIZE] = "";
#endif

#ifdef _WIN32
/* Copy of x264 code, which allows for Unicode characters in the command line.
 * Retrieve command line arguments as UTF-8. */
static int get_argv_utf8(int *argc_ptr, char ***argv_ptr)
{
    int ret = 0;
    wchar_t **argv_utf16 = CommandLineToArgvW(GetCommandLineW(), argc_ptr);
    if (argv_utf16)
    {
        int argc = *argc_ptr;
        int offset = (argc + 1) * sizeof(char*);
        int size = offset;

        for (int i = 0; i < argc; i++)
            size += WideCharToMultiByte(CP_UTF8, 0, argv_utf16[i], -1, NULL, 0, NULL, NULL);

        char **argv = *argv_ptr = (char**)malloc(size);
        if (argv)
        {
            for (int i = 0; i < argc; i++)
            {
                argv[i] = (char*)argv + offset;
                offset += WideCharToMultiByte(CP_UTF8, 0, argv_utf16[i], -1, argv[i], size - offset, NULL, NULL);
            }
            argv[argc] = NULL;
            ret = 1;
        }
        LocalFree(argv_utf16);
    }
    return ret;
}
#endif

/* Checks for abr-ladder config file in the command line.
 * Returns true if abr-config file is present. Returns 
 * false otherwise */

static bool checkAbrLadder(int argc, char **argv, FILE **abrConfig)
{
    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 false;
            }
        }
        if (long_options_index < 0)
        {
            x265_log(NULL, X265_LOG_WARNING, "short option '%c' unrecognized\n", c);
            return false;
        }
        if (!strcmp(long_options[long_options_index].name, "abr-ladder"))
        {
            *abrConfig = x265_fopen(optarg, "rb");
            if (!abrConfig)
                x265_log_file(NULL, X265_LOG_ERROR, "%s abr-ladder config file not found or error in opening zone file\n", optarg);
            return true;
        }
    }
    return false;
}

static uint8_t getNumAbrEncodes(FILE* abrConfig)
{
    char line[1024];
    uint8_t numEncodes = 0;

    while (fgets(line, sizeof(line), abrConfig))
    {
        if (strcmp(line, "\n") == 0)
            continue;
        else if (!(*line == '#'))
            numEncodes++;
    }
    rewind(abrConfig);
    return numEncodes;
}

static bool parseAbrConfig(FILE* abrConfig, CLIOptions cliopt[], uint8_t numEncodes)
{
    char line[1024];
    char* argLine;

    char *strPool = (char*)malloc(256 * X265_MAX_STRING_SIZE * sizeof(char));
    int strPoolSize = 256 * X265_MAX_STRING_SIZE;
    for (uint32_t i = 0; i < numEncodes; i++)
    {
        char **argv = (char**)malloc(256 * sizeof(char *));
        cliopt[i].stringPool = (i == 0 ? strPool : NULL);
        cliopt[i].argString = argv;
        cliopt[i].orgArgv = NULL;
        if (fgets(line, sizeof(line), abrConfig) == NULL) {
            fprintf(stderr, "Error reading line from configuration file.\n");
            return false;
        }
        if (*line == '#' || (strcmp(line, "\r\n") == 0))
            continue;
        int index = (int)strcspn(line, "\r\n");
        line[index] = '\0';
        argLine = line;
        char* start = strchr(argLine, ' ');
        while (isspace((unsigned char)*start)) start++;
        int argc = 0;
        // Adding a dummy string to avoid file parsing error
        argv[argc++] = (char *)"x265";

        /* Parse CLI header to identify the ID of the load encode and the reuse level */
        char *header = strtok(argLine, "[]");
        uint32_t idCount = 0;
        char *id = strtok(header, ":");
        char *head[X265_HEAD_ENTRIES];
        cliopt[i].encId = i;
        cliopt[i].isAbrLadderConfig = true;

        while (id && (idCount <= X265_HEAD_ENTRIES))
        {
            head[idCount] = id;
            id = strtok(NULL, ":");
            idCount++;
        }
        if (idCount != X265_HEAD_ENTRIES)
        {
            x265_log(NULL, X265_LOG_ERROR, "Incorrect number of arguments in ABR CLI header at line %d\n", i);
            return false;
        }
        else
        {
            snprintf(cliopt[i].encName, X265_MAX_STRING_SIZE, "%s", head[0]);
            cliopt[i].loadLevel = atoi(head[1]);
            snprintf(cliopt[i].reuseName, X265_MAX_STRING_SIZE, "%s", head[2]);
        }

        char* token = strtok(start, " ");
        while (token)
        {
            argv[argc] = strPool;
            strPool += strlen(token) + 1;
            strPoolSize -= (int)strlen(token) + 1;
            strcpy(argv[argc], token);
            token = strtok(NULL, " ");
            argc++;
        }
        argv[argc] = NULL;
        if (cliopt[i].parse(argc++, argv))
        {
            cliopt[i].destroy();
            if (cliopt[i].api)
                cliopt[i].api->param_free(cliopt[i].param);
            exit(1);
        }
    }
    X265_CHECK(strPoolSize >= 0, "string pool broken!");
    return true;
}

static bool setRefContext(CLIOptions cliopt[], uint32_t numEncodes)
{
    bool hasRef = false;
    bool isRefFound = false;

    /* Identify reference encode IDs and set save/load reuse levels */
    for (uint32_t curEnc = 0; curEnc < numEncodes; curEnc++)
    {
        isRefFound = false;
        hasRef = !strcmp(cliopt[curEnc].reuseName, "nil") ? false : true;
        if (hasRef)
        {
            for (uint32_t refEnc = 0; refEnc < numEncodes; refEnc++)
            {
                if (!strcmp(cliopt[curEnc].reuseName, cliopt[refEnc].encName))
                {
                    cliopt[curEnc].refId = refEnc;
                    cliopt[refEnc].numRefs++;
                    cliopt[refEnc].saveLevel = X265_MAX(cliopt[refEnc].saveLevel, cliopt[curEnc].loadLevel);
                    isRefFound = true;
                    break;
                }
            }
            if (!isRefFound)
            {
                x265_log(NULL, X265_LOG_ERROR, "Reference encode (%s) not found for %s\n", cliopt[curEnc].reuseName,
                    cliopt[curEnc].encName);
                return false;
            }
        }
    }
    return true;
}
/* CLI return codes:
 *
 * 0 - encode successful
 * 1 - unable to parse command line
 * 2 - unable to open encoder
 * 3 - unable to generate stream headers
 * 4 - encoder abort */

int main(int argc, char **argv)
{
#if HAVE_VLD
    // This uses Microsoft's proprietary WCHAR type, but this only builds on Windows to start with
    VLDSetReportOptions(VLD_OPT_REPORT_TO_DEBUGGER | VLD_OPT_REPORT_TO_FILE, L"x265_leaks.txt");
#endif
    PROFILE_INIT();
    THREAD_NAME("API", 0);

    GetConsoleTitle(orgConsoleTitle, CONSOLE_TITLE_SIZE);
    SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED);
#if _WIN32
    char** orgArgv = argv;
    get_argv_utf8(&argc, &argv);
#endif

    uint8_t numEncodes = 1;
    FILE *abrConfig = NULL;
    bool isAbrLadder = checkAbrLadder(argc, argv, &abrConfig);

    if (isAbrLadder)
        numEncodes = getNumAbrEncodes(abrConfig);

    CLIOptions* cliopt = new CLIOptions[numEncodes];
    cliopt[0].orgArgv = argv;
    cliopt[0].argString = argv;

    if (isAbrLadder)
    {
        if (!parseAbrConfig(abrConfig, cliopt, numEncodes))
            exit(1);
        if (!setRefContext(cliopt, numEncodes))
            exit(1);
    }
    else if (cliopt[0].parse(argc, argv))
    {
        cliopt[0].destroy();
        if (cliopt[0].api)
            cliopt[0].api->param_free(cliopt[0].param);
        exit(1);
    }

    int ret = 0;

    if (cliopt[0].scenecutAwareQpConfig)
    {
        if (!cliopt[0].parseScenecutAwareQpConfig())
        {
            x265_log(NULL, X265_LOG_ERROR, "Unable to parse scenecut aware qp config file \n");
            fclose(cliopt[0].scenecutAwareQpConfig);
            cliopt[0].scenecutAwareQpConfig = NULL;
        }
    }

    AbrEncoder* abrEnc = new AbrEncoder(cliopt, numEncodes, ret);
    int threadsActive = abrEnc->m_numActiveEncodes.get();
    while (threadsActive)
    {
        threadsActive = abrEnc->m_numActiveEncodes.waitForChange(threadsActive);
        for (uint8_t idx = 0; idx < numEncodes; idx++)
        {
            if (abrEnc->m_passEnc[idx]->m_ret)
            {
                if (isAbrLadder)
                    x265_log(NULL, X265_LOG_INFO, "Error generating ABR-ladder \n");
                ret = abrEnc->m_passEnc[idx]->m_ret;
                threadsActive = 0;
                break;
            }
        }
    }

    abrEnc->destroy();
    delete abrEnc;

    for (uint8_t idx = 0; idx < numEncodes; idx++)
        cliopt[idx].destroy();

    delete[] cliopt;

    SetConsoleTitle(orgConsoleTitle);
    SetThreadExecutionState(ES_CONTINUOUS);

#if _WIN32
    if (argv != orgArgv)
    {
        free(argv);
        argv = orgArgv;
    }
#endif

#if HAVE_VLD
    assert(VLDReportLeaks() == 0);
#endif

    return ret;
}