cmdutils.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /*
  2. * Various utilities for command line tools
  3. * Copyright (c) 2000-2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <string.h>
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. #include <errno.h>
  25. #include <math.h>
  26. /* Include only the enabled headers since some compilers (namely, Sun
  27. Studio) will not omit unused inline functions and create undefined
  28. references to libraries that are not being built. */
  29. #include "config.h"
  30. #include "compat/va_copy.h"
  31. #include "libavformat/avformat.h"
  32. #include "libswscale/swscale.h"
  33. #include "libswscale/version.h"
  34. #include "libswresample/swresample.h"
  35. #include "libavutil/avassert.h"
  36. #include "libavutil/avstring.h"
  37. #include "libavutil/channel_layout.h"
  38. #include "libavutil/display.h"
  39. #include "libavutil/getenv_utf8.h"
  40. #include "libavutil/mathematics.h"
  41. #include "libavutil/imgutils.h"
  42. #include "libavutil/libm.h"
  43. #include "libavutil/parseutils.h"
  44. #include "libavutil/eval.h"
  45. #include "libavutil/dict.h"
  46. #include "libavutil/opt.h"
  47. #include "cmdutils.h"
  48. #include "fopen_utf8.h"
  49. #include "opt_common.h"
  50. #ifdef _WIN32
  51. #include <windows.h>
  52. #include "compat/w32dlfcn.h"
  53. #endif
  54. AVDictionary *sws_dict;
  55. AVDictionary *swr_opts;
  56. AVDictionary *format_opts, *codec_opts;
  57. int hide_banner = 0;
  58. void uninit_opts(void)
  59. {
  60. av_dict_free(&swr_opts);
  61. av_dict_free(&sws_dict);
  62. av_dict_free(&format_opts);
  63. av_dict_free(&codec_opts);
  64. }
  65. void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
  66. {
  67. vfprintf(stdout, fmt, vl);
  68. }
  69. void init_dynload(void)
  70. {
  71. #if HAVE_SETDLLDIRECTORY && defined(_WIN32)
  72. /* Calling SetDllDirectory with the empty string (but not NULL) removes the
  73. * current working directory from the DLL search path as a security pre-caution. */
  74. SetDllDirectory("");
  75. #endif
  76. }
  77. int parse_number(const char *context, const char *numstr, enum OptionType type,
  78. double min, double max, double *dst)
  79. {
  80. char *tail;
  81. const char *error;
  82. double d = av_strtod(numstr, &tail);
  83. if (*tail)
  84. error = "Expected number for %s but found: %s\n";
  85. else if (d < min || d > max)
  86. error = "The value for %s was %s which is not within %f - %f\n";
  87. else if (type == OPT_TYPE_INT64 && (int64_t)d != d)
  88. error = "Expected int64 for %s but found %s\n";
  89. else if (type == OPT_TYPE_INT && (int)d != d)
  90. error = "Expected int for %s but found %s\n";
  91. else {
  92. *dst = d;
  93. return 0;
  94. }
  95. av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
  96. return AVERROR(EINVAL);
  97. }
  98. void show_help_options(const OptionDef *options, const char *msg, int req_flags,
  99. int rej_flags, int alt_flags)
  100. {
  101. const OptionDef *po;
  102. int first;
  103. first = 1;
  104. for (po = options; po->name; po++) {
  105. char buf[128];
  106. if (((po->flags & req_flags) != req_flags) ||
  107. (alt_flags && !(po->flags & alt_flags)) ||
  108. (po->flags & rej_flags))
  109. continue;
  110. if (first) {
  111. printf("%s\n", msg);
  112. first = 0;
  113. }
  114. av_strlcpy(buf, po->name, sizeof(buf));
  115. if (po->argname) {
  116. av_strlcat(buf, " ", sizeof(buf));
  117. av_strlcat(buf, po->argname, sizeof(buf));
  118. }
  119. printf("-%-17s %s\n", buf, po->help);
  120. }
  121. printf("\n");
  122. }
  123. void show_help_children(const AVClass *class, int flags)
  124. {
  125. void *iter = NULL;
  126. const AVClass *child;
  127. if (class->option) {
  128. av_opt_show2(&class, NULL, flags, 0);
  129. printf("\n");
  130. }
  131. while (child = av_opt_child_class_iterate(class, &iter))
  132. show_help_children(child, flags);
  133. }
  134. static const OptionDef *find_option(const OptionDef *po, const char *name)
  135. {
  136. while (po->name) {
  137. const char *end;
  138. if (av_strstart(name, po->name, &end) && (!*end || *end == ':'))
  139. break;
  140. po++;
  141. }
  142. return po;
  143. }
  144. /* _WIN32 means using the windows libc - cygwin doesn't define that
  145. * by default. HAVE_COMMANDLINETOARGVW is true on cygwin, while
  146. * it doesn't provide the actual command line via GetCommandLineW(). */
  147. #if HAVE_COMMANDLINETOARGVW && defined(_WIN32)
  148. #include <shellapi.h>
  149. /* Will be leaked on exit */
  150. static char** win32_argv_utf8 = NULL;
  151. static int win32_argc = 0;
  152. /**
  153. * Prepare command line arguments for executable.
  154. * For Windows - perform wide-char to UTF-8 conversion.
  155. * Input arguments should be main() function arguments.
  156. * @param argc_ptr Arguments number (including executable)
  157. * @param argv_ptr Arguments list.
  158. */
  159. static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  160. {
  161. char *argstr_flat;
  162. wchar_t **argv_w;
  163. int i, buffsize = 0, offset = 0;
  164. if (win32_argv_utf8) {
  165. *argc_ptr = win32_argc;
  166. *argv_ptr = win32_argv_utf8;
  167. return;
  168. }
  169. win32_argc = 0;
  170. argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
  171. if (win32_argc <= 0 || !argv_w)
  172. return;
  173. /* determine the UTF-8 buffer size (including NULL-termination symbols) */
  174. for (i = 0; i < win32_argc; i++)
  175. buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  176. NULL, 0, NULL, NULL);
  177. win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
  178. argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
  179. if (!win32_argv_utf8) {
  180. LocalFree(argv_w);
  181. return;
  182. }
  183. for (i = 0; i < win32_argc; i++) {
  184. win32_argv_utf8[i] = &argstr_flat[offset];
  185. offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  186. &argstr_flat[offset],
  187. buffsize - offset, NULL, NULL);
  188. }
  189. win32_argv_utf8[i] = NULL;
  190. LocalFree(argv_w);
  191. *argc_ptr = win32_argc;
  192. *argv_ptr = win32_argv_utf8;
  193. }
  194. #else
  195. static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  196. {
  197. /* nothing to do */
  198. }
  199. #endif /* HAVE_COMMANDLINETOARGVW */
  200. static int opt_has_arg(const OptionDef *o)
  201. {
  202. if (o->type == OPT_TYPE_BOOL)
  203. return 0;
  204. if (o->type == OPT_TYPE_FUNC)
  205. return !!(o->flags & OPT_FUNC_ARG);
  206. return 1;
  207. }
  208. static int write_option(void *optctx, const OptionDef *po, const char *opt,
  209. const char *arg, const OptionDef *defs)
  210. {
  211. /* new-style options contain an offset into optctx, old-style address of
  212. * a global var*/
  213. void *dst = po->flags & OPT_FLAG_OFFSET ?
  214. (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
  215. SpecifierOptList *sol = NULL;
  216. double num;
  217. int ret;
  218. if (po->flags & OPT_FLAG_SPEC) {
  219. char *p = strchr(opt, ':');
  220. char *str;
  221. sol = dst;
  222. ret = GROW_ARRAY(sol->opt, sol->nb_opt);
  223. if (ret < 0)
  224. return ret;
  225. str = av_strdup(p ? p + 1 : "");
  226. if (!str)
  227. return AVERROR(ENOMEM);
  228. sol->opt[sol->nb_opt - 1].specifier = str;
  229. dst = &sol->opt[sol->nb_opt - 1].u;
  230. }
  231. if (po->type == OPT_TYPE_STRING) {
  232. char *str;
  233. str = av_strdup(arg);
  234. av_freep(dst);
  235. if (!str)
  236. return AVERROR(ENOMEM);
  237. *(char **)dst = str;
  238. } else if (po->type == OPT_TYPE_BOOL || po->type == OPT_TYPE_INT) {
  239. ret = parse_number(opt, arg, OPT_TYPE_INT64, INT_MIN, INT_MAX, &num);
  240. if (ret < 0)
  241. return ret;
  242. *(int *)dst = num;
  243. } else if (po->type == OPT_TYPE_INT64) {
  244. ret = parse_number(opt, arg, OPT_TYPE_INT64, INT64_MIN, INT64_MAX, &num);
  245. if (ret < 0)
  246. return ret;
  247. *(int64_t *)dst = num;
  248. } else if (po->type == OPT_TYPE_TIME) {
  249. ret = av_parse_time(dst, arg, 1);
  250. if (ret < 0) {
  251. av_log(NULL, AV_LOG_ERROR, "Invalid duration for option %s: %s\n",
  252. opt, arg);
  253. return ret;
  254. }
  255. } else if (po->type == OPT_TYPE_FLOAT) {
  256. ret = parse_number(opt, arg, OPT_TYPE_FLOAT, -INFINITY, INFINITY, &num);
  257. if (ret < 0)
  258. return ret;
  259. *(float *)dst = num;
  260. } else if (po->type == OPT_TYPE_DOUBLE) {
  261. ret = parse_number(opt, arg, OPT_TYPE_DOUBLE, -INFINITY, INFINITY, &num);
  262. if (ret < 0)
  263. return ret;
  264. *(double *)dst = num;
  265. } else {
  266. int ret;
  267. av_assert0(po->type == OPT_TYPE_FUNC && po->u.func_arg);
  268. ret = po->u.func_arg(optctx, opt, arg);
  269. if (ret < 0) {
  270. av_log(NULL, AV_LOG_ERROR,
  271. "Failed to set value '%s' for option '%s': %s\n",
  272. arg, opt, av_err2str(ret));
  273. return ret;
  274. }
  275. }
  276. if (po->flags & OPT_EXIT)
  277. return AVERROR_EXIT;
  278. if (sol) {
  279. sol->type = po->type;
  280. sol->opt_canon = (po->flags & OPT_HAS_CANON) ?
  281. find_option(defs, po->u1.name_canon) : po;
  282. }
  283. return 0;
  284. }
  285. int parse_option(void *optctx, const char *opt, const char *arg,
  286. const OptionDef *options)
  287. {
  288. static const OptionDef opt_avoptions = {
  289. .name = "AVOption passthrough",
  290. .type = OPT_TYPE_FUNC,
  291. .flags = OPT_FUNC_ARG,
  292. .u.func_arg = opt_default,
  293. };
  294. const OptionDef *po;
  295. int ret;
  296. po = find_option(options, opt);
  297. if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
  298. /* handle 'no' bool option */
  299. po = find_option(options, opt + 2);
  300. if ((po->name && po->type == OPT_TYPE_BOOL))
  301. arg = "0";
  302. } else if (po->type == OPT_TYPE_BOOL)
  303. arg = "1";
  304. if (!po->name)
  305. po = &opt_avoptions;
  306. if (!po->name) {
  307. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
  308. return AVERROR(EINVAL);
  309. }
  310. if (opt_has_arg(po) && !arg) {
  311. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
  312. return AVERROR(EINVAL);
  313. }
  314. ret = write_option(optctx, po, opt, arg, options);
  315. if (ret < 0)
  316. return ret;
  317. return opt_has_arg(po);
  318. }
  319. int parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
  320. int (*parse_arg_function)(void *, const char*))
  321. {
  322. const char *opt;
  323. int optindex, handleoptions = 1, ret;
  324. /* perform system-dependent conversions for arguments list */
  325. prepare_app_arguments(&argc, &argv);
  326. /* parse options */
  327. optindex = 1;
  328. while (optindex < argc) {
  329. opt = argv[optindex++];
  330. if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
  331. if (opt[1] == '-' && opt[2] == '\0') {
  332. handleoptions = 0;
  333. continue;
  334. }
  335. opt++;
  336. if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
  337. return ret;
  338. optindex += ret;
  339. } else {
  340. if (parse_arg_function) {
  341. ret = parse_arg_function(optctx, opt);
  342. if (ret < 0)
  343. return ret;
  344. }
  345. }
  346. }
  347. return 0;
  348. }
  349. int parse_optgroup(void *optctx, OptionGroup *g, const OptionDef *defs)
  350. {
  351. int i, ret;
  352. av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
  353. g->group_def->name, g->arg);
  354. for (i = 0; i < g->nb_opts; i++) {
  355. Option *o = &g->opts[i];
  356. if (g->group_def->flags &&
  357. !(g->group_def->flags & o->opt->flags)) {
  358. av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
  359. "%s %s -- you are trying to apply an input option to an "
  360. "output file or vice versa. Move this option before the "
  361. "file it belongs to.\n", o->key, o->opt->help,
  362. g->group_def->name, g->arg);
  363. return AVERROR(EINVAL);
  364. }
  365. av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
  366. o->key, o->opt->help, o->val);
  367. ret = write_option(optctx, o->opt, o->key, o->val, defs);
  368. if (ret < 0)
  369. return ret;
  370. }
  371. av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
  372. return 0;
  373. }
  374. int locate_option(int argc, char **argv, const OptionDef *options,
  375. const char *optname)
  376. {
  377. const OptionDef *po;
  378. int i;
  379. for (i = 1; i < argc; i++) {
  380. const char *cur_opt = argv[i];
  381. if (*cur_opt++ != '-')
  382. continue;
  383. po = find_option(options, cur_opt);
  384. if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
  385. po = find_option(options, cur_opt + 2);
  386. if ((!po->name && !strcmp(cur_opt, optname)) ||
  387. (po->name && !strcmp(optname, po->name)))
  388. return i;
  389. if (!po->name || opt_has_arg(po))
  390. i++;
  391. }
  392. return 0;
  393. }
  394. static void dump_argument(FILE *report_file, const char *a)
  395. {
  396. const unsigned char *p;
  397. for (p = a; *p; p++)
  398. if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
  399. *p == '_' || (*p >= 'a' && *p <= 'z')))
  400. break;
  401. if (!*p) {
  402. fputs(a, report_file);
  403. return;
  404. }
  405. fputc('"', report_file);
  406. for (p = a; *p; p++) {
  407. if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
  408. fprintf(report_file, "\\%c", *p);
  409. else if (*p < ' ' || *p > '~')
  410. fprintf(report_file, "\\x%02x", *p);
  411. else
  412. fputc(*p, report_file);
  413. }
  414. fputc('"', report_file);
  415. }
  416. static void check_options(const OptionDef *po)
  417. {
  418. while (po->name) {
  419. if (po->flags & OPT_PERFILE)
  420. av_assert0(po->flags & (OPT_INPUT | OPT_OUTPUT));
  421. if (po->type == OPT_TYPE_FUNC)
  422. av_assert0(!(po->flags & (OPT_FLAG_OFFSET | OPT_FLAG_SPEC)));
  423. // OPT_FUNC_ARG can only be ser for OPT_TYPE_FUNC
  424. av_assert0((po->type == OPT_TYPE_FUNC) || !(po->flags & OPT_FUNC_ARG));
  425. po++;
  426. }
  427. }
  428. void parse_loglevel(int argc, char **argv, const OptionDef *options)
  429. {
  430. int idx = locate_option(argc, argv, options, "loglevel");
  431. char *env;
  432. check_options(options);
  433. if (!idx)
  434. idx = locate_option(argc, argv, options, "v");
  435. if (idx && argv[idx + 1])
  436. opt_loglevel(NULL, "loglevel", argv[idx + 1]);
  437. idx = locate_option(argc, argv, options, "report");
  438. env = getenv_utf8("FFREPORT");
  439. if (env || idx) {
  440. FILE *report_file = NULL;
  441. init_report(env, &report_file);
  442. if (report_file) {
  443. int i;
  444. fprintf(report_file, "Command line:\n");
  445. for (i = 0; i < argc; i++) {
  446. dump_argument(report_file, argv[i]);
  447. fputc(i < argc - 1 ? ' ' : '\n', report_file);
  448. }
  449. fflush(report_file);
  450. }
  451. }
  452. freeenv_utf8(env);
  453. idx = locate_option(argc, argv, options, "hide_banner");
  454. if (idx)
  455. hide_banner = 1;
  456. }
  457. static const AVOption *opt_find(void *obj, const char *name, const char *unit,
  458. int opt_flags, int search_flags)
  459. {
  460. const AVOption *o = av_opt_find(obj, name, unit, opt_flags, search_flags);
  461. if(o && !o->flags)
  462. return NULL;
  463. return o;
  464. }
  465. #define FLAGS (o->type == AV_OPT_TYPE_FLAGS && (arg[0]=='-' || arg[0]=='+')) ? AV_DICT_APPEND : 0
  466. int opt_default(void *optctx, const char *opt, const char *arg)
  467. {
  468. const AVOption *o;
  469. int consumed = 0;
  470. char opt_stripped[128];
  471. const char *p;
  472. const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
  473. #if CONFIG_SWSCALE
  474. const AVClass *sc = sws_get_class();
  475. #endif
  476. #if CONFIG_SWRESAMPLE
  477. const AVClass *swr_class = swr_get_class();
  478. #endif
  479. if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug"))
  480. av_log_set_level(AV_LOG_DEBUG);
  481. if (!(p = strchr(opt, ':')))
  482. p = opt + strlen(opt);
  483. av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
  484. if ((o = opt_find(&cc, opt_stripped, NULL, 0,
  485. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
  486. ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
  487. (o = opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
  488. av_dict_set(&codec_opts, opt, arg, FLAGS);
  489. consumed = 1;
  490. }
  491. if ((o = opt_find(&fc, opt, NULL, 0,
  492. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  493. av_dict_set(&format_opts, opt, arg, FLAGS);
  494. if (consumed)
  495. av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt);
  496. consumed = 1;
  497. }
  498. #if CONFIG_SWSCALE
  499. if (!consumed && (o = opt_find(&sc, opt, NULL, 0,
  500. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  501. if (!strcmp(opt, "srcw") || !strcmp(opt, "srch") ||
  502. !strcmp(opt, "dstw") || !strcmp(opt, "dsth") ||
  503. !strcmp(opt, "src_format") || !strcmp(opt, "dst_format")) {
  504. av_log(NULL, AV_LOG_ERROR, "Directly using swscale dimensions/format options is not supported, please use the -s or -pix_fmt options\n");
  505. return AVERROR(EINVAL);
  506. }
  507. av_dict_set(&sws_dict, opt, arg, FLAGS);
  508. consumed = 1;
  509. }
  510. #else
  511. if (!consumed && !strcmp(opt, "sws_flags")) {
  512. av_log(NULL, AV_LOG_WARNING, "Ignoring %s %s, due to disabled swscale\n", opt, arg);
  513. consumed = 1;
  514. }
  515. #endif
  516. #if CONFIG_SWRESAMPLE
  517. if (!consumed && (o=opt_find(&swr_class, opt, NULL, 0,
  518. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  519. av_dict_set(&swr_opts, opt, arg, FLAGS);
  520. consumed = 1;
  521. }
  522. #endif
  523. if (consumed)
  524. return 0;
  525. return AVERROR_OPTION_NOT_FOUND;
  526. }
  527. /*
  528. * Check whether given option is a group separator.
  529. *
  530. * @return index of the group definition that matched or -1 if none
  531. */
  532. static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
  533. const char *opt)
  534. {
  535. int i;
  536. for (i = 0; i < nb_groups; i++) {
  537. const OptionGroupDef *p = &groups[i];
  538. if (p->sep && !strcmp(p->sep, opt))
  539. return i;
  540. }
  541. return -1;
  542. }
  543. /*
  544. * Finish parsing an option group.
  545. *
  546. * @param group_idx which group definition should this group belong to
  547. * @param arg argument of the group delimiting option
  548. */
  549. static int finish_group(OptionParseContext *octx, int group_idx,
  550. const char *arg)
  551. {
  552. OptionGroupList *l = &octx->groups[group_idx];
  553. OptionGroup *g;
  554. int ret;
  555. ret = GROW_ARRAY(l->groups, l->nb_groups);
  556. if (ret < 0)
  557. return ret;
  558. g = &l->groups[l->nb_groups - 1];
  559. *g = octx->cur_group;
  560. g->arg = arg;
  561. g->group_def = l->group_def;
  562. g->sws_dict = sws_dict;
  563. g->swr_opts = swr_opts;
  564. g->codec_opts = codec_opts;
  565. g->format_opts = format_opts;
  566. codec_opts = NULL;
  567. format_opts = NULL;
  568. sws_dict = NULL;
  569. swr_opts = NULL;
  570. memset(&octx->cur_group, 0, sizeof(octx->cur_group));
  571. return ret;
  572. }
  573. /*
  574. * Add an option instance to currently parsed group.
  575. */
  576. static int add_opt(OptionParseContext *octx, const OptionDef *opt,
  577. const char *key, const char *val)
  578. {
  579. int global = !(opt->flags & OPT_PERFILE);
  580. OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
  581. int ret;
  582. ret = GROW_ARRAY(g->opts, g->nb_opts);
  583. if (ret < 0)
  584. return ret;
  585. g->opts[g->nb_opts - 1].opt = opt;
  586. g->opts[g->nb_opts - 1].key = key;
  587. g->opts[g->nb_opts - 1].val = val;
  588. return 0;
  589. }
  590. static int init_parse_context(OptionParseContext *octx,
  591. const OptionGroupDef *groups, int nb_groups)
  592. {
  593. static const OptionGroupDef global_group = { "global" };
  594. int i;
  595. memset(octx, 0, sizeof(*octx));
  596. octx->groups = av_calloc(nb_groups, sizeof(*octx->groups));
  597. if (!octx->groups)
  598. return AVERROR(ENOMEM);
  599. octx->nb_groups = nb_groups;
  600. for (i = 0; i < octx->nb_groups; i++)
  601. octx->groups[i].group_def = &groups[i];
  602. octx->global_opts.group_def = &global_group;
  603. octx->global_opts.arg = "";
  604. return 0;
  605. }
  606. void uninit_parse_context(OptionParseContext *octx)
  607. {
  608. int i, j;
  609. for (i = 0; i < octx->nb_groups; i++) {
  610. OptionGroupList *l = &octx->groups[i];
  611. for (j = 0; j < l->nb_groups; j++) {
  612. av_freep(&l->groups[j].opts);
  613. av_dict_free(&l->groups[j].codec_opts);
  614. av_dict_free(&l->groups[j].format_opts);
  615. av_dict_free(&l->groups[j].sws_dict);
  616. av_dict_free(&l->groups[j].swr_opts);
  617. }
  618. av_freep(&l->groups);
  619. }
  620. av_freep(&octx->groups);
  621. av_freep(&octx->cur_group.opts);
  622. av_freep(&octx->global_opts.opts);
  623. uninit_opts();
  624. }
  625. int split_commandline(OptionParseContext *octx, int argc, char *argv[],
  626. const OptionDef *options,
  627. const OptionGroupDef *groups, int nb_groups)
  628. {
  629. int ret;
  630. int optindex = 1;
  631. int dashdash = -2;
  632. /* perform system-dependent conversions for arguments list */
  633. prepare_app_arguments(&argc, &argv);
  634. ret = init_parse_context(octx, groups, nb_groups);
  635. if (ret < 0)
  636. return ret;
  637. av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
  638. while (optindex < argc) {
  639. const char *opt = argv[optindex++], *arg;
  640. const OptionDef *po;
  641. int ret;
  642. av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
  643. if (opt[0] == '-' && opt[1] == '-' && !opt[2]) {
  644. dashdash = optindex;
  645. continue;
  646. }
  647. /* unnamed group separators, e.g. output filename */
  648. if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
  649. ret = finish_group(octx, 0, opt);
  650. if (ret < 0)
  651. return ret;
  652. av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
  653. continue;
  654. }
  655. opt++;
  656. #define GET_ARG(arg) \
  657. do { \
  658. arg = argv[optindex++]; \
  659. if (!arg) { \
  660. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
  661. return AVERROR(EINVAL); \
  662. } \
  663. } while (0)
  664. /* named group separators, e.g. -i */
  665. if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
  666. GET_ARG(arg);
  667. ret = finish_group(octx, ret, arg);
  668. if (ret < 0)
  669. return ret;
  670. av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
  671. groups[ret].name, arg);
  672. continue;
  673. }
  674. /* normal options */
  675. po = find_option(options, opt);
  676. if (po->name) {
  677. if (po->flags & OPT_EXIT) {
  678. /* optional argument, e.g. -h */
  679. arg = argv[optindex++];
  680. } else if (opt_has_arg(po)) {
  681. GET_ARG(arg);
  682. } else {
  683. arg = "1";
  684. }
  685. ret = add_opt(octx, po, opt, arg);
  686. if (ret < 0)
  687. return ret;
  688. av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
  689. "argument '%s'.\n", po->name, po->help, arg);
  690. continue;
  691. }
  692. /* AVOptions */
  693. if (argv[optindex]) {
  694. ret = opt_default(NULL, opt, argv[optindex]);
  695. if (ret >= 0) {
  696. av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
  697. "argument '%s'.\n", opt, argv[optindex]);
  698. optindex++;
  699. continue;
  700. } else if (ret != AVERROR_OPTION_NOT_FOUND) {
  701. av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
  702. "with argument '%s'.\n", opt, argv[optindex]);
  703. return ret;
  704. }
  705. }
  706. /* boolean -nofoo options */
  707. if (opt[0] == 'n' && opt[1] == 'o' &&
  708. (po = find_option(options, opt + 2)) &&
  709. po->name && po->type == OPT_TYPE_BOOL) {
  710. ret = add_opt(octx, po, opt, "0");
  711. if (ret < 0)
  712. return ret;
  713. av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
  714. "argument 0.\n", po->name, po->help);
  715. continue;
  716. }
  717. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
  718. return AVERROR_OPTION_NOT_FOUND;
  719. }
  720. if (octx->cur_group.nb_opts || codec_opts || format_opts)
  721. av_log(NULL, AV_LOG_WARNING, "Trailing option(s) found in the "
  722. "command: may be ignored.\n");
  723. av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
  724. return 0;
  725. }
  726. void print_error(const char *filename, int err)
  727. {
  728. av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, av_err2str(err));
  729. }
  730. int read_yesno(void)
  731. {
  732. int c = getchar();
  733. int yesno = (av_toupper(c) == 'Y');
  734. while (c != '\n' && c != EOF)
  735. c = getchar();
  736. return yesno;
  737. }
  738. FILE *get_preset_file(char *filename, size_t filename_size,
  739. const char *preset_name, int is_path,
  740. const char *codec_name)
  741. {
  742. FILE *f = NULL;
  743. int i;
  744. #if HAVE_GETMODULEHANDLE && defined(_WIN32)
  745. char *datadir = NULL;
  746. #endif
  747. char *env_home = getenv_utf8("HOME");
  748. char *env_ffmpeg_datadir = getenv_utf8("FFMPEG_DATADIR");
  749. const char *base[3] = { env_ffmpeg_datadir,
  750. env_home, /* index=1(HOME) is special: search in a .ffmpeg subfolder */
  751. FFMPEG_DATADIR, };
  752. if (is_path) {
  753. av_strlcpy(filename, preset_name, filename_size);
  754. f = fopen_utf8(filename, "r");
  755. } else {
  756. #if HAVE_GETMODULEHANDLE && defined(_WIN32)
  757. wchar_t *datadir_w = get_module_filename(NULL);
  758. base[2] = NULL;
  759. if (wchartoutf8(datadir_w, &datadir))
  760. datadir = NULL;
  761. av_free(datadir_w);
  762. if (datadir)
  763. {
  764. char *ls;
  765. for (ls = datadir; *ls; ls++)
  766. if (*ls == '\\') *ls = '/';
  767. if (ls = strrchr(datadir, '/'))
  768. {
  769. ptrdiff_t datadir_len = ls - datadir;
  770. size_t desired_size = datadir_len + strlen("/ffpresets") + 1;
  771. char *new_datadir = av_realloc_array(
  772. datadir, desired_size, sizeof *datadir);
  773. if (new_datadir) {
  774. datadir = new_datadir;
  775. datadir[datadir_len] = 0;
  776. strncat(datadir, "/ffpresets", desired_size - 1 - datadir_len);
  777. base[2] = datadir;
  778. }
  779. }
  780. }
  781. #endif
  782. for (i = 0; i < 3 && !f; i++) {
  783. if (!base[i])
  784. continue;
  785. snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
  786. i != 1 ? "" : "/.ffmpeg", preset_name);
  787. f = fopen_utf8(filename, "r");
  788. if (!f && codec_name) {
  789. snprintf(filename, filename_size,
  790. "%s%s/%s-%s.ffpreset",
  791. base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
  792. preset_name);
  793. f = fopen_utf8(filename, "r");
  794. }
  795. }
  796. }
  797. #if HAVE_GETMODULEHANDLE && defined(_WIN32)
  798. av_free(datadir);
  799. #endif
  800. freeenv_utf8(env_ffmpeg_datadir);
  801. freeenv_utf8(env_home);
  802. return f;
  803. }
  804. int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
  805. {
  806. int ret = avformat_match_stream_specifier(s, st, spec);
  807. if (ret < 0)
  808. av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
  809. return ret;
  810. }
  811. int filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_id,
  812. AVFormatContext *s, AVStream *st, const AVCodec *codec,
  813. AVDictionary **dst)
  814. {
  815. AVDictionary *ret = NULL;
  816. const AVDictionaryEntry *t = NULL;
  817. int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
  818. : AV_OPT_FLAG_DECODING_PARAM;
  819. char prefix = 0;
  820. const AVClass *cc = avcodec_get_class();
  821. if (!codec)
  822. codec = s->oformat ? avcodec_find_encoder(codec_id)
  823. : avcodec_find_decoder(codec_id);
  824. switch (st->codecpar->codec_type) {
  825. case AVMEDIA_TYPE_VIDEO:
  826. prefix = 'v';
  827. flags |= AV_OPT_FLAG_VIDEO_PARAM;
  828. break;
  829. case AVMEDIA_TYPE_AUDIO:
  830. prefix = 'a';
  831. flags |= AV_OPT_FLAG_AUDIO_PARAM;
  832. break;
  833. case AVMEDIA_TYPE_SUBTITLE:
  834. prefix = 's';
  835. flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
  836. break;
  837. }
  838. while (t = av_dict_iterate(opts, t)) {
  839. const AVClass *priv_class;
  840. char *p = strchr(t->key, ':');
  841. /* check stream specification in opt name */
  842. if (p) {
  843. int err = check_stream_specifier(s, st, p + 1);
  844. if (err < 0) {
  845. av_dict_free(&ret);
  846. return err;
  847. } else if (!err)
  848. continue;
  849. *p = 0;
  850. }
  851. if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
  852. !codec ||
  853. ((priv_class = codec->priv_class) &&
  854. av_opt_find(&priv_class, t->key, NULL, flags,
  855. AV_OPT_SEARCH_FAKE_OBJ)))
  856. av_dict_set(&ret, t->key, t->value, 0);
  857. else if (t->key[0] == prefix &&
  858. av_opt_find(&cc, t->key + 1, NULL, flags,
  859. AV_OPT_SEARCH_FAKE_OBJ))
  860. av_dict_set(&ret, t->key + 1, t->value, 0);
  861. if (p)
  862. *p = ':';
  863. }
  864. *dst = ret;
  865. return 0;
  866. }
  867. int setup_find_stream_info_opts(AVFormatContext *s,
  868. AVDictionary *codec_opts,
  869. AVDictionary ***dst)
  870. {
  871. int ret;
  872. AVDictionary **opts;
  873. *dst = NULL;
  874. if (!s->nb_streams)
  875. return 0;
  876. opts = av_calloc(s->nb_streams, sizeof(*opts));
  877. if (!opts)
  878. return AVERROR(ENOMEM);
  879. for (int i = 0; i < s->nb_streams; i++) {
  880. ret = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id,
  881. s, s->streams[i], NULL, &opts[i]);
  882. if (ret < 0)
  883. goto fail;
  884. }
  885. *dst = opts;
  886. return 0;
  887. fail:
  888. for (int i = 0; i < s->nb_streams; i++)
  889. av_dict_free(&opts[i]);
  890. av_freep(&opts);
  891. return ret;
  892. }
  893. int grow_array(void **array, int elem_size, int *size, int new_size)
  894. {
  895. if (new_size >= INT_MAX / elem_size) {
  896. av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
  897. return AVERROR(ERANGE);
  898. }
  899. if (*size < new_size) {
  900. uint8_t *tmp = av_realloc_array(*array, new_size, elem_size);
  901. if (!tmp)
  902. return AVERROR(ENOMEM);
  903. memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
  904. *size = new_size;
  905. *array = tmp;
  906. return 0;
  907. }
  908. return 0;
  909. }
  910. void *allocate_array_elem(void *ptr, size_t elem_size, int *nb_elems)
  911. {
  912. void *new_elem;
  913. if (!(new_elem = av_mallocz(elem_size)) ||
  914. av_dynarray_add_nofree(ptr, nb_elems, new_elem) < 0)
  915. return NULL;
  916. return new_elem;
  917. }
  918. double get_rotation(const int32_t *displaymatrix)
  919. {
  920. double theta = 0;
  921. if (displaymatrix)
  922. theta = -round(av_display_rotation_get((int32_t*) displaymatrix));
  923. theta -= 360*floor(theta/360 + 0.9/360);
  924. if (fabs(theta - 90*round(theta/90)) > 2)
  925. av_log(NULL, AV_LOG_WARNING, "Odd rotation angle.\n"
  926. "If you want to help, upload a sample "
  927. "of this file to https://streams.videolan.org/upload/ "
  928. "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)");
  929. return theta;
  930. }