cmdutils.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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)
  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. return 0;
  281. }
  282. int parse_option(void *optctx, const char *opt, const char *arg,
  283. const OptionDef *options)
  284. {
  285. static const OptionDef opt_avoptions = {
  286. .name = "AVOption passthrough",
  287. .type = OPT_TYPE_FUNC,
  288. .flags = OPT_FUNC_ARG,
  289. .u.func_arg = opt_default,
  290. };
  291. const OptionDef *po;
  292. int ret;
  293. po = find_option(options, opt);
  294. if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
  295. /* handle 'no' bool option */
  296. po = find_option(options, opt + 2);
  297. if ((po->name && po->type == OPT_TYPE_BOOL))
  298. arg = "0";
  299. } else if (po->type == OPT_TYPE_BOOL)
  300. arg = "1";
  301. if (!po->name)
  302. po = &opt_avoptions;
  303. if (!po->name) {
  304. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
  305. return AVERROR(EINVAL);
  306. }
  307. if (opt_has_arg(po) && !arg) {
  308. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
  309. return AVERROR(EINVAL);
  310. }
  311. ret = write_option(optctx, po, opt, arg);
  312. if (ret < 0)
  313. return ret;
  314. return opt_has_arg(po);
  315. }
  316. int parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
  317. int (*parse_arg_function)(void *, const char*))
  318. {
  319. const char *opt;
  320. int optindex, handleoptions = 1, ret;
  321. /* perform system-dependent conversions for arguments list */
  322. prepare_app_arguments(&argc, &argv);
  323. /* parse options */
  324. optindex = 1;
  325. while (optindex < argc) {
  326. opt = argv[optindex++];
  327. if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
  328. if (opt[1] == '-' && opt[2] == '\0') {
  329. handleoptions = 0;
  330. continue;
  331. }
  332. opt++;
  333. if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
  334. return ret;
  335. optindex += ret;
  336. } else {
  337. if (parse_arg_function) {
  338. ret = parse_arg_function(optctx, opt);
  339. if (ret < 0)
  340. return ret;
  341. }
  342. }
  343. }
  344. return 0;
  345. }
  346. int parse_optgroup(void *optctx, OptionGroup *g)
  347. {
  348. int i, ret;
  349. av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
  350. g->group_def->name, g->arg);
  351. for (i = 0; i < g->nb_opts; i++) {
  352. Option *o = &g->opts[i];
  353. if (g->group_def->flags &&
  354. !(g->group_def->flags & o->opt->flags)) {
  355. av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
  356. "%s %s -- you are trying to apply an input option to an "
  357. "output file or vice versa. Move this option before the "
  358. "file it belongs to.\n", o->key, o->opt->help,
  359. g->group_def->name, g->arg);
  360. return AVERROR(EINVAL);
  361. }
  362. av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
  363. o->key, o->opt->help, o->val);
  364. ret = write_option(optctx, o->opt, o->key, o->val);
  365. if (ret < 0)
  366. return ret;
  367. }
  368. av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
  369. return 0;
  370. }
  371. int locate_option(int argc, char **argv, const OptionDef *options,
  372. const char *optname)
  373. {
  374. const OptionDef *po;
  375. int i;
  376. for (i = 1; i < argc; i++) {
  377. const char *cur_opt = argv[i];
  378. if (*cur_opt++ != '-')
  379. continue;
  380. po = find_option(options, cur_opt);
  381. if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
  382. po = find_option(options, cur_opt + 2);
  383. if ((!po->name && !strcmp(cur_opt, optname)) ||
  384. (po->name && !strcmp(optname, po->name)))
  385. return i;
  386. if (!po->name || opt_has_arg(po))
  387. i++;
  388. }
  389. return 0;
  390. }
  391. static void dump_argument(FILE *report_file, const char *a)
  392. {
  393. const unsigned char *p;
  394. for (p = a; *p; p++)
  395. if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
  396. *p == '_' || (*p >= 'a' && *p <= 'z')))
  397. break;
  398. if (!*p) {
  399. fputs(a, report_file);
  400. return;
  401. }
  402. fputc('"', report_file);
  403. for (p = a; *p; p++) {
  404. if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
  405. fprintf(report_file, "\\%c", *p);
  406. else if (*p < ' ' || *p > '~')
  407. fprintf(report_file, "\\x%02x", *p);
  408. else
  409. fputc(*p, report_file);
  410. }
  411. fputc('"', report_file);
  412. }
  413. static void check_options(const OptionDef *po)
  414. {
  415. while (po->name) {
  416. if (po->flags & OPT_PERFILE)
  417. av_assert0(po->flags & (OPT_INPUT | OPT_OUTPUT));
  418. if (po->type == OPT_TYPE_FUNC)
  419. av_assert0(!(po->flags & (OPT_FLAG_OFFSET | OPT_FLAG_SPEC)));
  420. // OPT_FUNC_ARG can only be ser for OPT_TYPE_FUNC
  421. av_assert0((po->type == OPT_TYPE_FUNC) || !(po->flags & OPT_FUNC_ARG));
  422. po++;
  423. }
  424. }
  425. void parse_loglevel(int argc, char **argv, const OptionDef *options)
  426. {
  427. int idx = locate_option(argc, argv, options, "loglevel");
  428. char *env;
  429. check_options(options);
  430. if (!idx)
  431. idx = locate_option(argc, argv, options, "v");
  432. if (idx && argv[idx + 1])
  433. opt_loglevel(NULL, "loglevel", argv[idx + 1]);
  434. idx = locate_option(argc, argv, options, "report");
  435. env = getenv_utf8("FFREPORT");
  436. if (env || idx) {
  437. FILE *report_file = NULL;
  438. init_report(env, &report_file);
  439. if (report_file) {
  440. int i;
  441. fprintf(report_file, "Command line:\n");
  442. for (i = 0; i < argc; i++) {
  443. dump_argument(report_file, argv[i]);
  444. fputc(i < argc - 1 ? ' ' : '\n', report_file);
  445. }
  446. fflush(report_file);
  447. }
  448. }
  449. freeenv_utf8(env);
  450. idx = locate_option(argc, argv, options, "hide_banner");
  451. if (idx)
  452. hide_banner = 1;
  453. }
  454. static const AVOption *opt_find(void *obj, const char *name, const char *unit,
  455. int opt_flags, int search_flags)
  456. {
  457. const AVOption *o = av_opt_find(obj, name, unit, opt_flags, search_flags);
  458. if(o && !o->flags)
  459. return NULL;
  460. return o;
  461. }
  462. #define FLAGS (o->type == AV_OPT_TYPE_FLAGS && (arg[0]=='-' || arg[0]=='+')) ? AV_DICT_APPEND : 0
  463. int opt_default(void *optctx, const char *opt, const char *arg)
  464. {
  465. const AVOption *o;
  466. int consumed = 0;
  467. char opt_stripped[128];
  468. const char *p;
  469. const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
  470. #if CONFIG_SWSCALE
  471. const AVClass *sc = sws_get_class();
  472. #endif
  473. #if CONFIG_SWRESAMPLE
  474. const AVClass *swr_class = swr_get_class();
  475. #endif
  476. if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug"))
  477. av_log_set_level(AV_LOG_DEBUG);
  478. if (!(p = strchr(opt, ':')))
  479. p = opt + strlen(opt);
  480. av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
  481. if ((o = opt_find(&cc, opt_stripped, NULL, 0,
  482. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
  483. ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
  484. (o = opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
  485. av_dict_set(&codec_opts, opt, arg, FLAGS);
  486. consumed = 1;
  487. }
  488. if ((o = opt_find(&fc, opt, NULL, 0,
  489. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  490. av_dict_set(&format_opts, opt, arg, FLAGS);
  491. if (consumed)
  492. av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt);
  493. consumed = 1;
  494. }
  495. #if CONFIG_SWSCALE
  496. if (!consumed && (o = opt_find(&sc, opt, NULL, 0,
  497. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  498. if (!strcmp(opt, "srcw") || !strcmp(opt, "srch") ||
  499. !strcmp(opt, "dstw") || !strcmp(opt, "dsth") ||
  500. !strcmp(opt, "src_format") || !strcmp(opt, "dst_format")) {
  501. av_log(NULL, AV_LOG_ERROR, "Directly using swscale dimensions/format options is not supported, please use the -s or -pix_fmt options\n");
  502. return AVERROR(EINVAL);
  503. }
  504. av_dict_set(&sws_dict, opt, arg, FLAGS);
  505. consumed = 1;
  506. }
  507. #else
  508. if (!consumed && !strcmp(opt, "sws_flags")) {
  509. av_log(NULL, AV_LOG_WARNING, "Ignoring %s %s, due to disabled swscale\n", opt, arg);
  510. consumed = 1;
  511. }
  512. #endif
  513. #if CONFIG_SWRESAMPLE
  514. if (!consumed && (o=opt_find(&swr_class, opt, NULL, 0,
  515. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  516. av_dict_set(&swr_opts, opt, arg, FLAGS);
  517. consumed = 1;
  518. }
  519. #endif
  520. if (consumed)
  521. return 0;
  522. return AVERROR_OPTION_NOT_FOUND;
  523. }
  524. /*
  525. * Check whether given option is a group separator.
  526. *
  527. * @return index of the group definition that matched or -1 if none
  528. */
  529. static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
  530. const char *opt)
  531. {
  532. int i;
  533. for (i = 0; i < nb_groups; i++) {
  534. const OptionGroupDef *p = &groups[i];
  535. if (p->sep && !strcmp(p->sep, opt))
  536. return i;
  537. }
  538. return -1;
  539. }
  540. /*
  541. * Finish parsing an option group.
  542. *
  543. * @param group_idx which group definition should this group belong to
  544. * @param arg argument of the group delimiting option
  545. */
  546. static int finish_group(OptionParseContext *octx, int group_idx,
  547. const char *arg)
  548. {
  549. OptionGroupList *l = &octx->groups[group_idx];
  550. OptionGroup *g;
  551. int ret;
  552. ret = GROW_ARRAY(l->groups, l->nb_groups);
  553. if (ret < 0)
  554. return ret;
  555. g = &l->groups[l->nb_groups - 1];
  556. *g = octx->cur_group;
  557. g->arg = arg;
  558. g->group_def = l->group_def;
  559. g->sws_dict = sws_dict;
  560. g->swr_opts = swr_opts;
  561. g->codec_opts = codec_opts;
  562. g->format_opts = format_opts;
  563. codec_opts = NULL;
  564. format_opts = NULL;
  565. sws_dict = NULL;
  566. swr_opts = NULL;
  567. memset(&octx->cur_group, 0, sizeof(octx->cur_group));
  568. return ret;
  569. }
  570. /*
  571. * Add an option instance to currently parsed group.
  572. */
  573. static int add_opt(OptionParseContext *octx, const OptionDef *opt,
  574. const char *key, const char *val)
  575. {
  576. int global = !(opt->flags & OPT_PERFILE);
  577. OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
  578. int ret;
  579. ret = GROW_ARRAY(g->opts, g->nb_opts);
  580. if (ret < 0)
  581. return ret;
  582. g->opts[g->nb_opts - 1].opt = opt;
  583. g->opts[g->nb_opts - 1].key = key;
  584. g->opts[g->nb_opts - 1].val = val;
  585. return 0;
  586. }
  587. static int init_parse_context(OptionParseContext *octx,
  588. const OptionGroupDef *groups, int nb_groups)
  589. {
  590. static const OptionGroupDef global_group = { "global" };
  591. int i;
  592. memset(octx, 0, sizeof(*octx));
  593. octx->groups = av_calloc(nb_groups, sizeof(*octx->groups));
  594. if (!octx->groups)
  595. return AVERROR(ENOMEM);
  596. octx->nb_groups = nb_groups;
  597. for (i = 0; i < octx->nb_groups; i++)
  598. octx->groups[i].group_def = &groups[i];
  599. octx->global_opts.group_def = &global_group;
  600. octx->global_opts.arg = "";
  601. return 0;
  602. }
  603. void uninit_parse_context(OptionParseContext *octx)
  604. {
  605. int i, j;
  606. for (i = 0; i < octx->nb_groups; i++) {
  607. OptionGroupList *l = &octx->groups[i];
  608. for (j = 0; j < l->nb_groups; j++) {
  609. av_freep(&l->groups[j].opts);
  610. av_dict_free(&l->groups[j].codec_opts);
  611. av_dict_free(&l->groups[j].format_opts);
  612. av_dict_free(&l->groups[j].sws_dict);
  613. av_dict_free(&l->groups[j].swr_opts);
  614. }
  615. av_freep(&l->groups);
  616. }
  617. av_freep(&octx->groups);
  618. av_freep(&octx->cur_group.opts);
  619. av_freep(&octx->global_opts.opts);
  620. uninit_opts();
  621. }
  622. int split_commandline(OptionParseContext *octx, int argc, char *argv[],
  623. const OptionDef *options,
  624. const OptionGroupDef *groups, int nb_groups)
  625. {
  626. int ret;
  627. int optindex = 1;
  628. int dashdash = -2;
  629. /* perform system-dependent conversions for arguments list */
  630. prepare_app_arguments(&argc, &argv);
  631. ret = init_parse_context(octx, groups, nb_groups);
  632. if (ret < 0)
  633. return ret;
  634. av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
  635. while (optindex < argc) {
  636. const char *opt = argv[optindex++], *arg;
  637. const OptionDef *po;
  638. int ret;
  639. av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
  640. if (opt[0] == '-' && opt[1] == '-' && !opt[2]) {
  641. dashdash = optindex;
  642. continue;
  643. }
  644. /* unnamed group separators, e.g. output filename */
  645. if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
  646. ret = finish_group(octx, 0, opt);
  647. if (ret < 0)
  648. return ret;
  649. av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
  650. continue;
  651. }
  652. opt++;
  653. #define GET_ARG(arg) \
  654. do { \
  655. arg = argv[optindex++]; \
  656. if (!arg) { \
  657. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
  658. return AVERROR(EINVAL); \
  659. } \
  660. } while (0)
  661. /* named group separators, e.g. -i */
  662. if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
  663. GET_ARG(arg);
  664. ret = finish_group(octx, ret, arg);
  665. if (ret < 0)
  666. return ret;
  667. av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
  668. groups[ret].name, arg);
  669. continue;
  670. }
  671. /* normal options */
  672. po = find_option(options, opt);
  673. if (po->name) {
  674. if (po->flags & OPT_EXIT) {
  675. /* optional argument, e.g. -h */
  676. arg = argv[optindex++];
  677. } else if (opt_has_arg(po)) {
  678. GET_ARG(arg);
  679. } else {
  680. arg = "1";
  681. }
  682. ret = add_opt(octx, po, opt, arg);
  683. if (ret < 0)
  684. return ret;
  685. av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
  686. "argument '%s'.\n", po->name, po->help, arg);
  687. continue;
  688. }
  689. /* AVOptions */
  690. if (argv[optindex]) {
  691. ret = opt_default(NULL, opt, argv[optindex]);
  692. if (ret >= 0) {
  693. av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
  694. "argument '%s'.\n", opt, argv[optindex]);
  695. optindex++;
  696. continue;
  697. } else if (ret != AVERROR_OPTION_NOT_FOUND) {
  698. av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
  699. "with argument '%s'.\n", opt, argv[optindex]);
  700. return ret;
  701. }
  702. }
  703. /* boolean -nofoo options */
  704. if (opt[0] == 'n' && opt[1] == 'o' &&
  705. (po = find_option(options, opt + 2)) &&
  706. po->name && po->type == OPT_TYPE_BOOL) {
  707. ret = add_opt(octx, po, opt, "0");
  708. if (ret < 0)
  709. return ret;
  710. av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
  711. "argument 0.\n", po->name, po->help);
  712. continue;
  713. }
  714. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
  715. return AVERROR_OPTION_NOT_FOUND;
  716. }
  717. if (octx->cur_group.nb_opts || codec_opts || format_opts)
  718. av_log(NULL, AV_LOG_WARNING, "Trailing option(s) found in the "
  719. "command: may be ignored.\n");
  720. av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
  721. return 0;
  722. }
  723. void print_error(const char *filename, int err)
  724. {
  725. av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, av_err2str(err));
  726. }
  727. int read_yesno(void)
  728. {
  729. int c = getchar();
  730. int yesno = (av_toupper(c) == 'Y');
  731. while (c != '\n' && c != EOF)
  732. c = getchar();
  733. return yesno;
  734. }
  735. FILE *get_preset_file(char *filename, size_t filename_size,
  736. const char *preset_name, int is_path,
  737. const char *codec_name)
  738. {
  739. FILE *f = NULL;
  740. int i;
  741. #if HAVE_GETMODULEHANDLE && defined(_WIN32)
  742. char *datadir = NULL;
  743. #endif
  744. char *env_home = getenv_utf8("HOME");
  745. char *env_ffmpeg_datadir = getenv_utf8("FFMPEG_DATADIR");
  746. const char *base[3] = { env_ffmpeg_datadir,
  747. env_home, /* index=1(HOME) is special: search in a .ffmpeg subfolder */
  748. FFMPEG_DATADIR, };
  749. if (is_path) {
  750. av_strlcpy(filename, preset_name, filename_size);
  751. f = fopen_utf8(filename, "r");
  752. } else {
  753. #if HAVE_GETMODULEHANDLE && defined(_WIN32)
  754. wchar_t *datadir_w = get_module_filename(NULL);
  755. base[2] = NULL;
  756. if (wchartoutf8(datadir_w, &datadir))
  757. datadir = NULL;
  758. av_free(datadir_w);
  759. if (datadir)
  760. {
  761. char *ls;
  762. for (ls = datadir; *ls; ls++)
  763. if (*ls == '\\') *ls = '/';
  764. if (ls = strrchr(datadir, '/'))
  765. {
  766. ptrdiff_t datadir_len = ls - datadir;
  767. size_t desired_size = datadir_len + strlen("/ffpresets") + 1;
  768. char *new_datadir = av_realloc_array(
  769. datadir, desired_size, sizeof *datadir);
  770. if (new_datadir) {
  771. datadir = new_datadir;
  772. datadir[datadir_len] = 0;
  773. strncat(datadir, "/ffpresets", desired_size - 1 - datadir_len);
  774. base[2] = datadir;
  775. }
  776. }
  777. }
  778. #endif
  779. for (i = 0; i < 3 && !f; i++) {
  780. if (!base[i])
  781. continue;
  782. snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
  783. i != 1 ? "" : "/.ffmpeg", preset_name);
  784. f = fopen_utf8(filename, "r");
  785. if (!f && codec_name) {
  786. snprintf(filename, filename_size,
  787. "%s%s/%s-%s.ffpreset",
  788. base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
  789. preset_name);
  790. f = fopen_utf8(filename, "r");
  791. }
  792. }
  793. }
  794. #if HAVE_GETMODULEHANDLE && defined(_WIN32)
  795. av_free(datadir);
  796. #endif
  797. freeenv_utf8(env_ffmpeg_datadir);
  798. freeenv_utf8(env_home);
  799. return f;
  800. }
  801. int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
  802. {
  803. int ret = avformat_match_stream_specifier(s, st, spec);
  804. if (ret < 0)
  805. av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
  806. return ret;
  807. }
  808. int filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_id,
  809. AVFormatContext *s, AVStream *st, const AVCodec *codec,
  810. AVDictionary **dst)
  811. {
  812. AVDictionary *ret = NULL;
  813. const AVDictionaryEntry *t = NULL;
  814. int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
  815. : AV_OPT_FLAG_DECODING_PARAM;
  816. char prefix = 0;
  817. const AVClass *cc = avcodec_get_class();
  818. if (!codec)
  819. codec = s->oformat ? avcodec_find_encoder(codec_id)
  820. : avcodec_find_decoder(codec_id);
  821. switch (st->codecpar->codec_type) {
  822. case AVMEDIA_TYPE_VIDEO:
  823. prefix = 'v';
  824. flags |= AV_OPT_FLAG_VIDEO_PARAM;
  825. break;
  826. case AVMEDIA_TYPE_AUDIO:
  827. prefix = 'a';
  828. flags |= AV_OPT_FLAG_AUDIO_PARAM;
  829. break;
  830. case AVMEDIA_TYPE_SUBTITLE:
  831. prefix = 's';
  832. flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
  833. break;
  834. }
  835. while (t = av_dict_iterate(opts, t)) {
  836. const AVClass *priv_class;
  837. char *p = strchr(t->key, ':');
  838. /* check stream specification in opt name */
  839. if (p) {
  840. int err = check_stream_specifier(s, st, p + 1);
  841. if (err < 0) {
  842. av_dict_free(&ret);
  843. return err;
  844. } else if (!err)
  845. continue;
  846. *p = 0;
  847. }
  848. if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
  849. !codec ||
  850. ((priv_class = codec->priv_class) &&
  851. av_opt_find(&priv_class, t->key, NULL, flags,
  852. AV_OPT_SEARCH_FAKE_OBJ)))
  853. av_dict_set(&ret, t->key, t->value, 0);
  854. else if (t->key[0] == prefix &&
  855. av_opt_find(&cc, t->key + 1, NULL, flags,
  856. AV_OPT_SEARCH_FAKE_OBJ))
  857. av_dict_set(&ret, t->key + 1, t->value, 0);
  858. if (p)
  859. *p = ':';
  860. }
  861. *dst = ret;
  862. return 0;
  863. }
  864. int setup_find_stream_info_opts(AVFormatContext *s,
  865. AVDictionary *codec_opts,
  866. AVDictionary ***dst)
  867. {
  868. int ret;
  869. AVDictionary **opts;
  870. *dst = NULL;
  871. if (!s->nb_streams)
  872. return 0;
  873. opts = av_calloc(s->nb_streams, sizeof(*opts));
  874. if (!opts)
  875. return AVERROR(ENOMEM);
  876. for (int i = 0; i < s->nb_streams; i++) {
  877. ret = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id,
  878. s, s->streams[i], NULL, &opts[i]);
  879. if (ret < 0)
  880. goto fail;
  881. }
  882. *dst = opts;
  883. return 0;
  884. fail:
  885. for (int i = 0; i < s->nb_streams; i++)
  886. av_dict_free(&opts[i]);
  887. av_freep(&opts);
  888. return ret;
  889. }
  890. int grow_array(void **array, int elem_size, int *size, int new_size)
  891. {
  892. if (new_size >= INT_MAX / elem_size) {
  893. av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
  894. return AVERROR(ERANGE);
  895. }
  896. if (*size < new_size) {
  897. uint8_t *tmp = av_realloc_array(*array, new_size, elem_size);
  898. if (!tmp)
  899. return AVERROR(ENOMEM);
  900. memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
  901. *size = new_size;
  902. *array = tmp;
  903. return 0;
  904. }
  905. return 0;
  906. }
  907. void *allocate_array_elem(void *ptr, size_t elem_size, int *nb_elems)
  908. {
  909. void *new_elem;
  910. if (!(new_elem = av_mallocz(elem_size)) ||
  911. av_dynarray_add_nofree(ptr, nb_elems, new_elem) < 0)
  912. return NULL;
  913. return new_elem;
  914. }
  915. double get_rotation(const int32_t *displaymatrix)
  916. {
  917. double theta = 0;
  918. if (displaymatrix)
  919. theta = -round(av_display_rotation_get((int32_t*) displaymatrix));
  920. theta -= 360*floor(theta/360 + 0.9/360);
  921. if (fabs(theta - 90*round(theta/90)) > 2)
  922. av_log(NULL, AV_LOG_WARNING, "Odd rotation angle.\n"
  923. "If you want to help, upload a sample "
  924. "of this file to https://streams.videolan.org/upload/ "
  925. "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)");
  926. return theta;
  927. }