cmdutils.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  1. /*
  2. * Various utilities for command line tools
  3. * Copyright (c) 2000-2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "libavformat/avformat.h"
  31. #include "libavfilter/avfilter.h"
  32. #include "libavdevice/avdevice.h"
  33. #include "libavresample/avresample.h"
  34. #include "libswscale/swscale.h"
  35. #include "libavutil/avassert.h"
  36. #include "libavutil/avstring.h"
  37. #include "libavutil/mathematics.h"
  38. #include "libavutil/imgutils.h"
  39. #include "libavutil/parseutils.h"
  40. #include "libavutil/pixdesc.h"
  41. #include "libavutil/eval.h"
  42. #include "libavutil/dict.h"
  43. #include "libavutil/opt.h"
  44. #include "libavutil/cpu.h"
  45. #include "avversion.h"
  46. #include "cmdutils.h"
  47. #if CONFIG_NETWORK
  48. #include "libavformat/network.h"
  49. #endif
  50. #if HAVE_SYS_RESOURCE_H
  51. #include <sys/time.h>
  52. #include <sys/resource.h>
  53. #endif
  54. struct SwsContext *sws_opts;
  55. AVDictionary *format_opts, *codec_opts, *resample_opts;
  56. static const int this_year = 2016;
  57. void init_opts(void)
  58. {
  59. #if CONFIG_SWSCALE
  60. sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
  61. NULL, NULL, NULL);
  62. #endif
  63. }
  64. void uninit_opts(void)
  65. {
  66. #if CONFIG_SWSCALE
  67. sws_freeContext(sws_opts);
  68. sws_opts = NULL;
  69. #endif
  70. av_dict_free(&format_opts);
  71. av_dict_free(&codec_opts);
  72. av_dict_free(&resample_opts);
  73. }
  74. void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
  75. {
  76. vfprintf(stdout, fmt, vl);
  77. }
  78. static void (*program_exit)(int ret);
  79. void register_exit(void (*cb)(int ret))
  80. {
  81. program_exit = cb;
  82. }
  83. void exit_program(int ret)
  84. {
  85. if (program_exit)
  86. program_exit(ret);
  87. exit(ret);
  88. }
  89. double parse_number_or_die(const char *context, const char *numstr, int type,
  90. double min, double max)
  91. {
  92. char *tail;
  93. const char *error;
  94. double d = av_strtod(numstr, &tail);
  95. if (*tail)
  96. error = "Expected number for %s but found: %s\n";
  97. else if (d < min || d > max)
  98. error = "The value for %s was %s which is not within %f - %f\n";
  99. else if (type == OPT_INT64 && (int64_t)d != d)
  100. error = "Expected int64 for %s but found %s\n";
  101. else if (type == OPT_INT && (int)d != d)
  102. error = "Expected int for %s but found %s\n";
  103. else
  104. return d;
  105. av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
  106. exit_program(1);
  107. return 0;
  108. }
  109. int64_t parse_time_or_die(const char *context, const char *timestr,
  110. int is_duration)
  111. {
  112. int64_t us;
  113. if (av_parse_time(&us, timestr, is_duration) < 0) {
  114. av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
  115. is_duration ? "duration" : "date", context, timestr);
  116. exit_program(1);
  117. }
  118. return us;
  119. }
  120. void show_help_options(const OptionDef *options, const char *msg, int req_flags,
  121. int rej_flags, int alt_flags)
  122. {
  123. const OptionDef *po;
  124. int first;
  125. first = 1;
  126. for (po = options; po->name != NULL; po++) {
  127. char buf[64];
  128. if (((po->flags & req_flags) != req_flags) ||
  129. (alt_flags && !(po->flags & alt_flags)) ||
  130. (po->flags & rej_flags))
  131. continue;
  132. if (first) {
  133. printf("%s\n", msg);
  134. first = 0;
  135. }
  136. av_strlcpy(buf, po->name, sizeof(buf));
  137. if (po->argname) {
  138. av_strlcat(buf, " ", sizeof(buf));
  139. av_strlcat(buf, po->argname, sizeof(buf));
  140. }
  141. printf("-%-17s %s\n", buf, po->help);
  142. }
  143. printf("\n");
  144. }
  145. void show_help_children(const AVClass *class, int flags)
  146. {
  147. const AVClass *child = NULL;
  148. av_opt_show2(&class, NULL, flags, 0);
  149. printf("\n");
  150. while (child = av_opt_child_class_next(class, child))
  151. show_help_children(child, flags);
  152. }
  153. static const OptionDef *find_option(const OptionDef *po, const char *name)
  154. {
  155. const char *p = strchr(name, ':');
  156. int len = p ? p - name : strlen(name);
  157. while (po->name) {
  158. if (!strncmp(name, po->name, len) && strlen(po->name) == len)
  159. break;
  160. po++;
  161. }
  162. return po;
  163. }
  164. /* _WIN32 means using the windows libc - cygwin doesn't define that
  165. * by default. HAVE_COMMANDLINETOARGVW is true on cygwin, while
  166. * it doesn't provide the actual command line via GetCommandLineW(). */
  167. #if HAVE_COMMANDLINETOARGVW && defined(_WIN32)
  168. #include <windows.h>
  169. #include <shellapi.h>
  170. /* Will be leaked on exit */
  171. static char** win32_argv_utf8 = NULL;
  172. static int win32_argc = 0;
  173. /**
  174. * Prepare command line arguments for executable.
  175. * For Windows - perform wide-char to UTF-8 conversion.
  176. * Input arguments should be main() function arguments.
  177. * @param argc_ptr Arguments number (including executable)
  178. * @param argv_ptr Arguments list.
  179. */
  180. static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  181. {
  182. char *argstr_flat;
  183. wchar_t **argv_w;
  184. int i, buffsize = 0, offset = 0;
  185. if (win32_argv_utf8) {
  186. *argc_ptr = win32_argc;
  187. *argv_ptr = win32_argv_utf8;
  188. return;
  189. }
  190. win32_argc = 0;
  191. argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
  192. if (win32_argc <= 0 || !argv_w)
  193. return;
  194. /* determine the UTF-8 buffer size (including NULL-termination symbols) */
  195. for (i = 0; i < win32_argc; i++)
  196. buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  197. NULL, 0, NULL, NULL);
  198. win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
  199. argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
  200. if (!win32_argv_utf8) {
  201. LocalFree(argv_w);
  202. return;
  203. }
  204. for (i = 0; i < win32_argc; i++) {
  205. win32_argv_utf8[i] = &argstr_flat[offset];
  206. offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  207. &argstr_flat[offset],
  208. buffsize - offset, NULL, NULL);
  209. }
  210. win32_argv_utf8[i] = NULL;
  211. LocalFree(argv_w);
  212. *argc_ptr = win32_argc;
  213. *argv_ptr = win32_argv_utf8;
  214. }
  215. #else
  216. static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  217. {
  218. /* nothing to do */
  219. }
  220. #endif /* HAVE_COMMANDLINETOARGVW */
  221. static int write_option(void *optctx, const OptionDef *po, const char *opt,
  222. const char *arg)
  223. {
  224. /* new-style options contain an offset into optctx, old-style address of
  225. * a global var*/
  226. void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
  227. (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
  228. int *dstcount;
  229. if (po->flags & OPT_SPEC) {
  230. SpecifierOpt **so = dst;
  231. char *p = strchr(opt, ':');
  232. char *str;
  233. dstcount = (int *)(so + 1);
  234. *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
  235. str = av_strdup(p ? p + 1 : "");
  236. if (!str)
  237. return AVERROR(ENOMEM);
  238. (*so)[*dstcount - 1].specifier = str;
  239. dst = &(*so)[*dstcount - 1].u;
  240. }
  241. if (po->flags & OPT_STRING) {
  242. char *str;
  243. str = av_strdup(arg);
  244. av_freep(dst);
  245. if (!str)
  246. return AVERROR(ENOMEM);
  247. *(char **)dst = str;
  248. } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
  249. *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
  250. } else if (po->flags & OPT_INT64) {
  251. *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
  252. } else if (po->flags & OPT_TIME) {
  253. *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
  254. } else if (po->flags & OPT_FLOAT) {
  255. *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
  256. } else if (po->flags & OPT_DOUBLE) {
  257. *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
  258. } else if (po->u.func_arg) {
  259. int ret = po->u.func_arg(optctx, opt, arg);
  260. if (ret < 0) {
  261. av_log(NULL, AV_LOG_ERROR,
  262. "Failed to set value '%s' for option '%s'\n", arg, opt);
  263. return ret;
  264. }
  265. }
  266. if (po->flags & OPT_EXIT)
  267. exit_program(0);
  268. return 0;
  269. }
  270. int parse_option(void *optctx, const char *opt, const char *arg,
  271. const OptionDef *options)
  272. {
  273. const OptionDef *po;
  274. int ret;
  275. po = find_option(options, opt);
  276. if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
  277. /* handle 'no' bool option */
  278. po = find_option(options, opt + 2);
  279. if ((po->name && (po->flags & OPT_BOOL)))
  280. arg = "0";
  281. } else if (po->flags & OPT_BOOL)
  282. arg = "1";
  283. if (!po->name)
  284. po = find_option(options, "default");
  285. if (!po->name) {
  286. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
  287. return AVERROR(EINVAL);
  288. }
  289. if (po->flags & HAS_ARG && !arg) {
  290. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
  291. return AVERROR(EINVAL);
  292. }
  293. ret = write_option(optctx, po, opt, arg);
  294. if (ret < 0)
  295. return ret;
  296. return !!(po->flags & HAS_ARG);
  297. }
  298. void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
  299. void (*parse_arg_function)(void *, const char*))
  300. {
  301. const char *opt;
  302. int optindex, handleoptions = 1, ret;
  303. /* perform system-dependent conversions for arguments list */
  304. prepare_app_arguments(&argc, &argv);
  305. /* parse options */
  306. optindex = 1;
  307. while (optindex < argc) {
  308. opt = argv[optindex++];
  309. if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
  310. if (opt[1] == '-' && opt[2] == '\0') {
  311. handleoptions = 0;
  312. continue;
  313. }
  314. opt++;
  315. if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
  316. exit_program(1);
  317. optindex += ret;
  318. } else {
  319. if (parse_arg_function)
  320. parse_arg_function(optctx, opt);
  321. }
  322. }
  323. }
  324. int parse_optgroup(void *optctx, OptionGroup *g)
  325. {
  326. int i, ret;
  327. av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
  328. g->group_def->name, g->arg);
  329. for (i = 0; i < g->nb_opts; i++) {
  330. Option *o = &g->opts[i];
  331. if (g->group_def->flags &&
  332. !(g->group_def->flags & o->opt->flags)) {
  333. av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
  334. "%s %s -- you are trying to apply an input option to an "
  335. "output file or vice versa. Move this option before the "
  336. "file it belongs to.\n", o->key, o->opt->help,
  337. g->group_def->name, g->arg);
  338. return AVERROR(EINVAL);
  339. }
  340. av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
  341. o->key, o->opt->help, o->val);
  342. ret = write_option(optctx, o->opt, o->key, o->val);
  343. if (ret < 0)
  344. return ret;
  345. }
  346. av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
  347. return 0;
  348. }
  349. int locate_option(int argc, char **argv, const OptionDef *options,
  350. const char *optname)
  351. {
  352. const OptionDef *po;
  353. int i;
  354. for (i = 1; i < argc; i++) {
  355. const char *cur_opt = argv[i];
  356. if (*cur_opt++ != '-')
  357. continue;
  358. po = find_option(options, cur_opt);
  359. if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
  360. po = find_option(options, cur_opt + 2);
  361. if ((!po->name && !strcmp(cur_opt, optname)) ||
  362. (po->name && !strcmp(optname, po->name)))
  363. return i;
  364. if (!po->name || po->flags & HAS_ARG)
  365. i++;
  366. }
  367. return 0;
  368. }
  369. void parse_loglevel(int argc, char **argv, const OptionDef *options)
  370. {
  371. int idx = locate_option(argc, argv, options, "loglevel");
  372. if (!idx)
  373. idx = locate_option(argc, argv, options, "v");
  374. if (idx && argv[idx + 1])
  375. opt_loglevel(NULL, "loglevel", argv[idx + 1]);
  376. }
  377. #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
  378. int opt_default(void *optctx, const char *opt, const char *arg)
  379. {
  380. const AVOption *o;
  381. char opt_stripped[128];
  382. const char *p;
  383. const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
  384. #if CONFIG_AVRESAMPLE
  385. const AVClass *rc = avresample_get_class();
  386. #endif
  387. #if CONFIG_SWSCALE
  388. const AVClass *sc = sws_get_class();
  389. #endif
  390. if (!(p = strchr(opt, ':')))
  391. p = opt + strlen(opt);
  392. av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
  393. if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
  394. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
  395. ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
  396. (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
  397. av_dict_set(&codec_opts, opt, arg, FLAGS);
  398. else if ((o = av_opt_find(&fc, opt, NULL, 0,
  399. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
  400. av_dict_set(&format_opts, opt, arg, FLAGS);
  401. #if CONFIG_AVRESAMPLE
  402. else if ((o = av_opt_find(&rc, opt, NULL, 0,
  403. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
  404. av_dict_set(&resample_opts, opt, arg, FLAGS);
  405. #endif
  406. #if CONFIG_SWSCALE
  407. else if ((o = av_opt_find(&sc, opt, NULL, 0,
  408. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  409. // XXX we only support sws_flags, not arbitrary sws options
  410. int ret = av_opt_set(sws_opts, opt, arg, 0);
  411. if (ret < 0) {
  412. av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
  413. return ret;
  414. }
  415. }
  416. #endif
  417. if (o)
  418. return 0;
  419. return AVERROR_OPTION_NOT_FOUND;
  420. }
  421. /*
  422. * Check whether given option is a group separator.
  423. *
  424. * @return index of the group definition that matched or -1 if none
  425. */
  426. static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
  427. const char *opt)
  428. {
  429. int i;
  430. for (i = 0; i < nb_groups; i++) {
  431. const OptionGroupDef *p = &groups[i];
  432. if (p->sep && !strcmp(p->sep, opt))
  433. return i;
  434. }
  435. return -1;
  436. }
  437. /*
  438. * Finish parsing an option group.
  439. *
  440. * @param group_idx which group definition should this group belong to
  441. * @param arg argument of the group delimiting option
  442. */
  443. static void finish_group(OptionParseContext *octx, int group_idx,
  444. const char *arg)
  445. {
  446. OptionGroupList *l = &octx->groups[group_idx];
  447. OptionGroup *g;
  448. GROW_ARRAY(l->groups, l->nb_groups);
  449. g = &l->groups[l->nb_groups - 1];
  450. *g = octx->cur_group;
  451. g->arg = arg;
  452. g->group_def = l->group_def;
  453. #if CONFIG_SWSCALE
  454. g->sws_opts = sws_opts;
  455. #endif
  456. g->codec_opts = codec_opts;
  457. g->format_opts = format_opts;
  458. g->resample_opts = resample_opts;
  459. codec_opts = NULL;
  460. format_opts = NULL;
  461. resample_opts = NULL;
  462. #if CONFIG_SWSCALE
  463. sws_opts = NULL;
  464. #endif
  465. init_opts();
  466. memset(&octx->cur_group, 0, sizeof(octx->cur_group));
  467. }
  468. /*
  469. * Add an option instance to currently parsed group.
  470. */
  471. static void add_opt(OptionParseContext *octx, const OptionDef *opt,
  472. const char *key, const char *val)
  473. {
  474. int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
  475. OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
  476. GROW_ARRAY(g->opts, g->nb_opts);
  477. g->opts[g->nb_opts - 1].opt = opt;
  478. g->opts[g->nb_opts - 1].key = key;
  479. g->opts[g->nb_opts - 1].val = val;
  480. }
  481. static void init_parse_context(OptionParseContext *octx,
  482. const OptionGroupDef *groups, int nb_groups)
  483. {
  484. static const OptionGroupDef global_group = { "global" };
  485. int i;
  486. memset(octx, 0, sizeof(*octx));
  487. octx->nb_groups = nb_groups;
  488. octx->groups = av_mallocz(sizeof(*octx->groups) * octx->nb_groups);
  489. if (!octx->groups)
  490. exit_program(1);
  491. for (i = 0; i < octx->nb_groups; i++)
  492. octx->groups[i].group_def = &groups[i];
  493. octx->global_opts.group_def = &global_group;
  494. octx->global_opts.arg = "";
  495. init_opts();
  496. }
  497. void uninit_parse_context(OptionParseContext *octx)
  498. {
  499. int i, j;
  500. for (i = 0; i < octx->nb_groups; i++) {
  501. OptionGroupList *l = &octx->groups[i];
  502. for (j = 0; j < l->nb_groups; j++) {
  503. av_freep(&l->groups[j].opts);
  504. av_dict_free(&l->groups[j].codec_opts);
  505. av_dict_free(&l->groups[j].format_opts);
  506. av_dict_free(&l->groups[j].resample_opts);
  507. #if CONFIG_SWSCALE
  508. sws_freeContext(l->groups[j].sws_opts);
  509. #endif
  510. }
  511. av_freep(&l->groups);
  512. }
  513. av_freep(&octx->groups);
  514. av_freep(&octx->cur_group.opts);
  515. av_freep(&octx->global_opts.opts);
  516. uninit_opts();
  517. }
  518. int split_commandline(OptionParseContext *octx, int argc, char *argv[],
  519. const OptionDef *options,
  520. const OptionGroupDef *groups, int nb_groups)
  521. {
  522. int optindex = 1;
  523. /* perform system-dependent conversions for arguments list */
  524. prepare_app_arguments(&argc, &argv);
  525. init_parse_context(octx, groups, nb_groups);
  526. av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
  527. while (optindex < argc) {
  528. const char *opt = argv[optindex++], *arg;
  529. const OptionDef *po;
  530. int ret;
  531. av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
  532. /* unnamed group separators, e.g. output filename */
  533. if (opt[0] != '-' || !opt[1]) {
  534. finish_group(octx, 0, opt);
  535. av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
  536. continue;
  537. }
  538. opt++;
  539. #define GET_ARG(arg) \
  540. do { \
  541. arg = argv[optindex++]; \
  542. if (!arg) { \
  543. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
  544. return AVERROR(EINVAL); \
  545. } \
  546. } while (0)
  547. /* named group separators, e.g. -i */
  548. if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
  549. GET_ARG(arg);
  550. finish_group(octx, ret, arg);
  551. av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
  552. groups[ret].name, arg);
  553. continue;
  554. }
  555. /* normal options */
  556. po = find_option(options, opt);
  557. if (po->name) {
  558. if (po->flags & OPT_EXIT) {
  559. /* optional argument, e.g. -h */
  560. arg = argv[optindex++];
  561. } else if (po->flags & HAS_ARG) {
  562. GET_ARG(arg);
  563. } else {
  564. arg = "1";
  565. }
  566. add_opt(octx, po, opt, arg);
  567. av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
  568. "argument '%s'.\n", po->name, po->help, arg);
  569. continue;
  570. }
  571. /* AVOptions */
  572. if (argv[optindex]) {
  573. ret = opt_default(NULL, opt, argv[optindex]);
  574. if (ret >= 0) {
  575. av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
  576. "argument '%s'.\n", opt, argv[optindex]);
  577. optindex++;
  578. continue;
  579. } else if (ret != AVERROR_OPTION_NOT_FOUND) {
  580. av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
  581. "with argument '%s'.\n", opt, argv[optindex]);
  582. return ret;
  583. }
  584. }
  585. /* boolean -nofoo options */
  586. if (opt[0] == 'n' && opt[1] == 'o' &&
  587. (po = find_option(options, opt + 2)) &&
  588. po->name && po->flags & OPT_BOOL) {
  589. add_opt(octx, po, opt, "0");
  590. av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
  591. "argument 0.\n", po->name, po->help);
  592. continue;
  593. }
  594. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
  595. return AVERROR_OPTION_NOT_FOUND;
  596. }
  597. if (octx->cur_group.nb_opts || codec_opts || format_opts || resample_opts)
  598. av_log(NULL, AV_LOG_WARNING, "Trailing options were found on the "
  599. "commandline.\n");
  600. av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
  601. return 0;
  602. }
  603. int opt_cpuflags(void *optctx, const char *opt, const char *arg)
  604. {
  605. int flags = av_parse_cpu_flags(arg);
  606. if (flags < 0)
  607. return flags;
  608. av_set_cpu_flags_mask(flags);
  609. return 0;
  610. }
  611. int opt_loglevel(void *optctx, const char *opt, const char *arg)
  612. {
  613. const struct { const char *name; int level; } log_levels[] = {
  614. { "quiet" , AV_LOG_QUIET },
  615. { "panic" , AV_LOG_PANIC },
  616. { "fatal" , AV_LOG_FATAL },
  617. { "error" , AV_LOG_ERROR },
  618. { "warning", AV_LOG_WARNING },
  619. { "info" , AV_LOG_INFO },
  620. { "verbose", AV_LOG_VERBOSE },
  621. { "debug" , AV_LOG_DEBUG },
  622. { "trace" , AV_LOG_TRACE },
  623. };
  624. char *tail;
  625. int level;
  626. int i;
  627. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
  628. if (!strcmp(log_levels[i].name, arg)) {
  629. av_log_set_level(log_levels[i].level);
  630. return 0;
  631. }
  632. }
  633. level = strtol(arg, &tail, 10);
  634. if (*tail) {
  635. av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
  636. "Possible levels are numbers or:\n", arg);
  637. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
  638. av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
  639. exit_program(1);
  640. }
  641. av_log_set_level(level);
  642. return 0;
  643. }
  644. int opt_timelimit(void *optctx, const char *opt, const char *arg)
  645. {
  646. #if HAVE_SETRLIMIT
  647. int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
  648. struct rlimit rl = { lim, lim + 1 };
  649. if (setrlimit(RLIMIT_CPU, &rl))
  650. perror("setrlimit");
  651. #else
  652. av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
  653. #endif
  654. return 0;
  655. }
  656. void print_error(const char *filename, int err)
  657. {
  658. char errbuf[128];
  659. const char *errbuf_ptr = errbuf;
  660. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  661. errbuf_ptr = strerror(AVUNERROR(err));
  662. av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
  663. }
  664. static int warned_cfg = 0;
  665. #define INDENT 1
  666. #define SHOW_VERSION 2
  667. #define SHOW_CONFIG 4
  668. #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
  669. if (CONFIG_##LIBNAME) { \
  670. const char *indent = flags & INDENT? " " : ""; \
  671. if (flags & SHOW_VERSION) { \
  672. unsigned int version = libname##_version(); \
  673. av_log(NULL, level, \
  674. "%slib%-10s %2d.%3d.%2d / %2d.%3d.%2d\n", \
  675. indent, #libname, \
  676. LIB##LIBNAME##_VERSION_MAJOR, \
  677. LIB##LIBNAME##_VERSION_MINOR, \
  678. LIB##LIBNAME##_VERSION_MICRO, \
  679. version >> 16, version >> 8 & 0xff, version & 0xff); \
  680. } \
  681. if (flags & SHOW_CONFIG) { \
  682. const char *cfg = libname##_configuration(); \
  683. if (strcmp(LIBAV_CONFIGURATION, cfg)) { \
  684. if (!warned_cfg) { \
  685. av_log(NULL, level, \
  686. "%sWARNING: library configuration mismatch\n", \
  687. indent); \
  688. warned_cfg = 1; \
  689. } \
  690. av_log(NULL, level, "%s%-11s configuration: %s\n", \
  691. indent, #libname, cfg); \
  692. } \
  693. } \
  694. } \
  695. static void print_all_libs_info(int flags, int level)
  696. {
  697. PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
  698. PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
  699. PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
  700. PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
  701. PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
  702. PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
  703. PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
  704. }
  705. void show_banner(void)
  706. {
  707. av_log(NULL, AV_LOG_INFO,
  708. "%s version " LIBAV_VERSION ", Copyright (c) %d-%d the Libav developers\n",
  709. program_name, program_birth_year, this_year);
  710. av_log(NULL, AV_LOG_INFO, " built on %s %s with %s\n",
  711. __DATE__, __TIME__, CC_IDENT);
  712. av_log(NULL, AV_LOG_VERBOSE, " configuration: " LIBAV_CONFIGURATION "\n");
  713. print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_VERBOSE);
  714. print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_VERBOSE);
  715. }
  716. int show_version(void *optctx, const char *opt, const char *arg)
  717. {
  718. av_log_set_callback(log_callback_help);
  719. printf("%s " LIBAV_VERSION "\n", program_name);
  720. print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
  721. return 0;
  722. }
  723. int show_license(void *optctx, const char *opt, const char *arg)
  724. {
  725. printf(
  726. #if CONFIG_NONFREE
  727. "This version of %s has nonfree parts compiled in.\n"
  728. "Therefore it is not legally redistributable.\n",
  729. program_name
  730. #elif CONFIG_GPLV3
  731. "%s is free software; you can redistribute it and/or modify\n"
  732. "it under the terms of the GNU General Public License as published by\n"
  733. "the Free Software Foundation; either version 3 of the License, or\n"
  734. "(at your option) any later version.\n"
  735. "\n"
  736. "%s is distributed in the hope that it will be useful,\n"
  737. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  738. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  739. "GNU General Public License for more details.\n"
  740. "\n"
  741. "You should have received a copy of the GNU General Public License\n"
  742. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  743. program_name, program_name, program_name
  744. #elif CONFIG_GPL
  745. "%s is free software; you can redistribute it and/or modify\n"
  746. "it under the terms of the GNU General Public License as published by\n"
  747. "the Free Software Foundation; either version 2 of the License, or\n"
  748. "(at your option) any later version.\n"
  749. "\n"
  750. "%s is distributed in the hope that it will be useful,\n"
  751. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  752. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  753. "GNU General Public License for more details.\n"
  754. "\n"
  755. "You should have received a copy of the GNU General Public License\n"
  756. "along with %s; if not, write to the Free Software\n"
  757. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  758. program_name, program_name, program_name
  759. #elif CONFIG_LGPLV3
  760. "%s is free software; you can redistribute it and/or modify\n"
  761. "it under the terms of the GNU Lesser General Public License as published by\n"
  762. "the Free Software Foundation; either version 3 of the License, or\n"
  763. "(at your option) any later version.\n"
  764. "\n"
  765. "%s is distributed in the hope that it will be useful,\n"
  766. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  767. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  768. "GNU Lesser General Public License for more details.\n"
  769. "\n"
  770. "You should have received a copy of the GNU Lesser General Public License\n"
  771. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  772. program_name, program_name, program_name
  773. #else
  774. "%s is free software; you can redistribute it and/or\n"
  775. "modify it under the terms of the GNU Lesser General Public\n"
  776. "License as published by the Free Software Foundation; either\n"
  777. "version 2.1 of the License, or (at your option) any later version.\n"
  778. "\n"
  779. "%s is distributed in the hope that it will be useful,\n"
  780. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  781. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  782. "Lesser General Public License for more details.\n"
  783. "\n"
  784. "You should have received a copy of the GNU Lesser General Public\n"
  785. "License along with %s; if not, write to the Free Software\n"
  786. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  787. program_name, program_name, program_name
  788. #endif
  789. );
  790. return 0;
  791. }
  792. int show_formats(void *optctx, const char *opt, const char *arg)
  793. {
  794. AVInputFormat *ifmt = NULL;
  795. AVOutputFormat *ofmt = NULL;
  796. const char *last_name;
  797. printf("File formats:\n"
  798. " D. = Demuxing supported\n"
  799. " .E = Muxing supported\n"
  800. " --\n");
  801. last_name = "000";
  802. for (;;) {
  803. int decode = 0;
  804. int encode = 0;
  805. const char *name = NULL;
  806. const char *long_name = NULL;
  807. while ((ofmt = av_oformat_next(ofmt))) {
  808. if ((!name || strcmp(ofmt->name, name) < 0) &&
  809. strcmp(ofmt->name, last_name) > 0) {
  810. name = ofmt->name;
  811. long_name = ofmt->long_name;
  812. encode = 1;
  813. }
  814. }
  815. while ((ifmt = av_iformat_next(ifmt))) {
  816. if ((!name || strcmp(ifmt->name, name) < 0) &&
  817. strcmp(ifmt->name, last_name) > 0) {
  818. name = ifmt->name;
  819. long_name = ifmt->long_name;
  820. encode = 0;
  821. }
  822. if (name && strcmp(ifmt->name, name) == 0)
  823. decode = 1;
  824. }
  825. if (!name)
  826. break;
  827. last_name = name;
  828. printf(" %s%s %-15s %s\n",
  829. decode ? "D" : " ",
  830. encode ? "E" : " ",
  831. name,
  832. long_name ? long_name:" ");
  833. }
  834. return 0;
  835. }
  836. #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
  837. if (codec->field) { \
  838. const type *p = c->field; \
  839. \
  840. printf(" Supported " list_name ":"); \
  841. while (*p != term) { \
  842. get_name(*p); \
  843. printf(" %s", name); \
  844. p++; \
  845. } \
  846. printf("\n"); \
  847. } \
  848. static void print_codec(const AVCodec *c)
  849. {
  850. int encoder = av_codec_is_encoder(c);
  851. printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
  852. c->long_name ? c->long_name : "");
  853. printf(" General capabilities: ");
  854. if (c->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND)
  855. printf("horizband ");
  856. if (c->capabilities & AV_CODEC_CAP_DR1)
  857. printf("dr1 ");
  858. if (c->capabilities & AV_CODEC_CAP_TRUNCATED)
  859. printf("trunc ");
  860. if (c->capabilities & AV_CODEC_CAP_DELAY)
  861. printf("delay ");
  862. if (c->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)
  863. printf("small ");
  864. if (c->capabilities & AV_CODEC_CAP_SUBFRAMES)
  865. printf("subframes ");
  866. if (c->capabilities & AV_CODEC_CAP_EXPERIMENTAL)
  867. printf("exp ");
  868. if (c->capabilities & AV_CODEC_CAP_CHANNEL_CONF)
  869. printf("chconf ");
  870. if (c->capabilities & AV_CODEC_CAP_PARAM_CHANGE)
  871. printf("paramchange ");
  872. if (c->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
  873. printf("variable ");
  874. if (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
  875. AV_CODEC_CAP_SLICE_THREADS |
  876. AV_CODEC_CAP_AUTO_THREADS))
  877. printf("threads ");
  878. if (!c->capabilities)
  879. printf("none");
  880. printf("\n");
  881. if (c->type == AVMEDIA_TYPE_VIDEO) {
  882. printf(" Threading capabilities: ");
  883. switch (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
  884. AV_CODEC_CAP_SLICE_THREADS |
  885. AV_CODEC_CAP_AUTO_THREADS)) {
  886. case AV_CODEC_CAP_FRAME_THREADS |
  887. AV_CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
  888. case AV_CODEC_CAP_FRAME_THREADS: printf("frame"); break;
  889. case AV_CODEC_CAP_SLICE_THREADS: printf("slice"); break;
  890. case AV_CODEC_CAP_AUTO_THREADS : printf("auto"); break;
  891. default: printf("none"); break;
  892. }
  893. printf("\n");
  894. }
  895. if (c->supported_framerates) {
  896. const AVRational *fps = c->supported_framerates;
  897. printf(" Supported framerates:");
  898. while (fps->num) {
  899. printf(" %d/%d", fps->num, fps->den);
  900. fps++;
  901. }
  902. printf("\n");
  903. }
  904. PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
  905. AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
  906. PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
  907. GET_SAMPLE_RATE_NAME);
  908. PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
  909. AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
  910. PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
  911. 0, GET_CH_LAYOUT_DESC);
  912. if (c->priv_class) {
  913. show_help_children(c->priv_class,
  914. AV_OPT_FLAG_ENCODING_PARAM |
  915. AV_OPT_FLAG_DECODING_PARAM);
  916. }
  917. }
  918. static char get_media_type_char(enum AVMediaType type)
  919. {
  920. switch (type) {
  921. case AVMEDIA_TYPE_VIDEO: return 'V';
  922. case AVMEDIA_TYPE_AUDIO: return 'A';
  923. case AVMEDIA_TYPE_SUBTITLE: return 'S';
  924. default: return '?';
  925. }
  926. }
  927. static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
  928. int encoder)
  929. {
  930. while ((prev = av_codec_next(prev))) {
  931. if (prev->id == id &&
  932. (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
  933. return prev;
  934. }
  935. return NULL;
  936. }
  937. static void print_codecs_for_id(enum AVCodecID id, int encoder)
  938. {
  939. const AVCodec *codec = NULL;
  940. printf(" (%s: ", encoder ? "encoders" : "decoders");
  941. while ((codec = next_codec_for_id(id, codec, encoder)))
  942. printf("%s ", codec->name);
  943. printf(")");
  944. }
  945. int show_codecs(void *optctx, const char *opt, const char *arg)
  946. {
  947. const AVCodecDescriptor *desc = NULL;
  948. printf("Codecs:\n"
  949. " D..... = Decoding supported\n"
  950. " .E.... = Encoding supported\n"
  951. " ..V... = Video codec\n"
  952. " ..A... = Audio codec\n"
  953. " ..S... = Subtitle codec\n"
  954. " ...I.. = Intra frame-only codec\n"
  955. " ....L. = Lossy compression\n"
  956. " .....S = Lossless compression\n"
  957. " -------\n");
  958. while ((desc = avcodec_descriptor_next(desc))) {
  959. const AVCodec *codec = NULL;
  960. printf(avcodec_find_decoder(desc->id) ? "D" : ".");
  961. printf(avcodec_find_encoder(desc->id) ? "E" : ".");
  962. printf("%c", get_media_type_char(desc->type));
  963. printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
  964. printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
  965. printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
  966. printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
  967. /* print decoders/encoders when there's more than one or their
  968. * names are different from codec name */
  969. while ((codec = next_codec_for_id(desc->id, codec, 0))) {
  970. if (strcmp(codec->name, desc->name)) {
  971. print_codecs_for_id(desc->id, 0);
  972. break;
  973. }
  974. }
  975. codec = NULL;
  976. while ((codec = next_codec_for_id(desc->id, codec, 1))) {
  977. if (strcmp(codec->name, desc->name)) {
  978. print_codecs_for_id(desc->id, 1);
  979. break;
  980. }
  981. }
  982. printf("\n");
  983. }
  984. return 0;
  985. }
  986. static void print_codecs(int encoder)
  987. {
  988. const AVCodecDescriptor *desc = NULL;
  989. printf("%s:\n"
  990. " V... = Video\n"
  991. " A... = Audio\n"
  992. " S... = Subtitle\n"
  993. " .F.. = Frame-level multithreading\n"
  994. " ..S. = Slice-level multithreading\n"
  995. " ...X = Codec is experimental\n"
  996. " ---\n",
  997. encoder ? "Encoders" : "Decoders");
  998. while ((desc = avcodec_descriptor_next(desc))) {
  999. const AVCodec *codec = NULL;
  1000. while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
  1001. printf("%c", get_media_type_char(desc->type));
  1002. printf((codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) ? "F" : ".");
  1003. printf((codec->capabilities & AV_CODEC_CAP_SLICE_THREADS) ? "S" : ".");
  1004. printf((codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
  1005. printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
  1006. if (strcmp(codec->name, desc->name))
  1007. printf(" (codec %s)", desc->name);
  1008. printf("\n");
  1009. }
  1010. }
  1011. }
  1012. int show_decoders(void *optctx, const char *opt, const char *arg)
  1013. {
  1014. print_codecs(0);
  1015. return 0;
  1016. }
  1017. int show_encoders(void *optctx, const char *opt, const char *arg)
  1018. {
  1019. print_codecs(1);
  1020. return 0;
  1021. }
  1022. int show_bsfs(void *optctx, const char *opt, const char *arg)
  1023. {
  1024. const AVBitStreamFilter *bsf = NULL;
  1025. void *opaque = NULL;
  1026. printf("Bitstream filters:\n");
  1027. while ((bsf = av_bsf_next(&opaque)))
  1028. printf("%s\n", bsf->name);
  1029. printf("\n");
  1030. return 0;
  1031. }
  1032. int show_protocols(void *optctx, const char *opt, const char *arg)
  1033. {
  1034. void *opaque = NULL;
  1035. const char *name;
  1036. printf("Supported file protocols:\n"
  1037. "Input:\n");
  1038. while ((name = avio_enum_protocols(&opaque, 0)))
  1039. printf("%s\n", name);
  1040. printf("Output:\n");
  1041. while ((name = avio_enum_protocols(&opaque, 1)))
  1042. printf("%s\n", name);
  1043. return 0;
  1044. }
  1045. int show_filters(void *optctx, const char *opt, const char *arg)
  1046. {
  1047. #if CONFIG_AVFILTER
  1048. const AVFilter *filter = NULL;
  1049. printf("Filters:\n");
  1050. while ((filter = avfilter_next(filter)))
  1051. printf("%-16s %s\n", filter->name, filter->description);
  1052. #else
  1053. printf("No filters available: libavfilter disabled\n");
  1054. #endif
  1055. return 0;
  1056. }
  1057. int show_pix_fmts(void *optctx, const char *opt, const char *arg)
  1058. {
  1059. const AVPixFmtDescriptor *pix_desc = NULL;
  1060. printf("Pixel formats:\n"
  1061. "I.... = Supported Input format for conversion\n"
  1062. ".O... = Supported Output format for conversion\n"
  1063. "..H.. = Hardware accelerated format\n"
  1064. "...P. = Paletted format\n"
  1065. "....B = Bitstream format\n"
  1066. "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
  1067. "-----\n");
  1068. #if !CONFIG_SWSCALE
  1069. # define sws_isSupportedInput(x) 0
  1070. # define sws_isSupportedOutput(x) 0
  1071. #endif
  1072. while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
  1073. enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
  1074. printf("%c%c%c%c%c %-16s %d %2d\n",
  1075. sws_isSupportedInput (pix_fmt) ? 'I' : '.',
  1076. sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
  1077. pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
  1078. pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
  1079. pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
  1080. pix_desc->name,
  1081. pix_desc->nb_components,
  1082. av_get_bits_per_pixel(pix_desc));
  1083. }
  1084. return 0;
  1085. }
  1086. int show_sample_fmts(void *optctx, const char *opt, const char *arg)
  1087. {
  1088. int i;
  1089. char fmt_str[128];
  1090. for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
  1091. printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
  1092. return 0;
  1093. }
  1094. static void show_help_codec(const char *name, int encoder)
  1095. {
  1096. const AVCodecDescriptor *desc;
  1097. const AVCodec *codec;
  1098. if (!name) {
  1099. av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
  1100. return;
  1101. }
  1102. codec = encoder ? avcodec_find_encoder_by_name(name) :
  1103. avcodec_find_decoder_by_name(name);
  1104. if (codec)
  1105. print_codec(codec);
  1106. else if ((desc = avcodec_descriptor_get_by_name(name))) {
  1107. int printed = 0;
  1108. while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
  1109. printed = 1;
  1110. print_codec(codec);
  1111. }
  1112. if (!printed) {
  1113. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to Libav, "
  1114. "but no %s for it are available. Libav might need to be "
  1115. "recompiled with additional external libraries.\n",
  1116. name, encoder ? "encoders" : "decoders");
  1117. }
  1118. } else {
  1119. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by Libav.\n",
  1120. name);
  1121. }
  1122. }
  1123. static void show_help_demuxer(const char *name)
  1124. {
  1125. const AVInputFormat *fmt = av_find_input_format(name);
  1126. if (!fmt) {
  1127. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1128. return;
  1129. }
  1130. printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
  1131. if (fmt->extensions)
  1132. printf(" Common extensions: %s.\n", fmt->extensions);
  1133. if (fmt->priv_class)
  1134. show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
  1135. }
  1136. static void show_help_muxer(const char *name)
  1137. {
  1138. const AVCodecDescriptor *desc;
  1139. const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
  1140. if (!fmt) {
  1141. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1142. return;
  1143. }
  1144. printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
  1145. if (fmt->extensions)
  1146. printf(" Common extensions: %s.\n", fmt->extensions);
  1147. if (fmt->mime_type)
  1148. printf(" Mime type: %s.\n", fmt->mime_type);
  1149. if (fmt->video_codec != AV_CODEC_ID_NONE &&
  1150. (desc = avcodec_descriptor_get(fmt->video_codec))) {
  1151. printf(" Default video codec: %s.\n", desc->name);
  1152. }
  1153. if (fmt->audio_codec != AV_CODEC_ID_NONE &&
  1154. (desc = avcodec_descriptor_get(fmt->audio_codec))) {
  1155. printf(" Default audio codec: %s.\n", desc->name);
  1156. }
  1157. if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
  1158. (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
  1159. printf(" Default subtitle codec: %s.\n", desc->name);
  1160. }
  1161. if (fmt->priv_class)
  1162. show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
  1163. }
  1164. #if CONFIG_AVFILTER
  1165. static void show_help_filter(const char *name)
  1166. {
  1167. const AVFilter *f = avfilter_get_by_name(name);
  1168. int i, count;
  1169. if (!name) {
  1170. av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
  1171. return;
  1172. } else if (!f) {
  1173. av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
  1174. return;
  1175. }
  1176. printf("Filter %s [%s]:\n", f->name, f->description);
  1177. if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
  1178. printf(" slice threading supported\n");
  1179. printf(" Inputs:\n");
  1180. count = avfilter_pad_count(f->inputs);
  1181. for (i = 0; i < count; i++) {
  1182. printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
  1183. media_type_string(avfilter_pad_get_type(f->inputs, i)));
  1184. }
  1185. if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
  1186. printf(" dynamic (depending on the options)\n");
  1187. printf(" Outputs:\n");
  1188. count = avfilter_pad_count(f->outputs);
  1189. for (i = 0; i < count; i++) {
  1190. printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
  1191. media_type_string(avfilter_pad_get_type(f->outputs, i)));
  1192. }
  1193. if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
  1194. printf(" dynamic (depending on the options)\n");
  1195. if (f->priv_class)
  1196. show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM |
  1197. AV_OPT_FLAG_AUDIO_PARAM);
  1198. }
  1199. #endif
  1200. int show_help(void *optctx, const char *opt, const char *arg)
  1201. {
  1202. char *topic, *par;
  1203. av_log_set_callback(log_callback_help);
  1204. topic = av_strdup(arg ? arg : "");
  1205. if (!topic)
  1206. return AVERROR(ENOMEM);
  1207. par = strchr(topic, '=');
  1208. if (par)
  1209. *par++ = 0;
  1210. if (!*topic) {
  1211. show_help_default(topic, par);
  1212. } else if (!strcmp(topic, "decoder")) {
  1213. show_help_codec(par, 0);
  1214. } else if (!strcmp(topic, "encoder")) {
  1215. show_help_codec(par, 1);
  1216. } else if (!strcmp(topic, "demuxer")) {
  1217. show_help_demuxer(par);
  1218. } else if (!strcmp(topic, "muxer")) {
  1219. show_help_muxer(par);
  1220. #if CONFIG_AVFILTER
  1221. } else if (!strcmp(topic, "filter")) {
  1222. show_help_filter(par);
  1223. #endif
  1224. } else {
  1225. show_help_default(topic, par);
  1226. }
  1227. av_freep(&topic);
  1228. return 0;
  1229. }
  1230. int read_yesno(void)
  1231. {
  1232. int c = getchar();
  1233. int yesno = (av_toupper(c) == 'Y');
  1234. while (c != '\n' && c != EOF)
  1235. c = getchar();
  1236. return yesno;
  1237. }
  1238. void init_pts_correction(PtsCorrectionContext *ctx)
  1239. {
  1240. ctx->num_faulty_pts = ctx->num_faulty_dts = 0;
  1241. ctx->last_pts = ctx->last_dts = INT64_MIN;
  1242. }
  1243. int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t reordered_pts,
  1244. int64_t dts)
  1245. {
  1246. int64_t pts = AV_NOPTS_VALUE;
  1247. if (dts != AV_NOPTS_VALUE) {
  1248. ctx->num_faulty_dts += dts <= ctx->last_dts;
  1249. ctx->last_dts = dts;
  1250. }
  1251. if (reordered_pts != AV_NOPTS_VALUE) {
  1252. ctx->num_faulty_pts += reordered_pts <= ctx->last_pts;
  1253. ctx->last_pts = reordered_pts;
  1254. }
  1255. if ((ctx->num_faulty_pts<=ctx->num_faulty_dts || dts == AV_NOPTS_VALUE)
  1256. && reordered_pts != AV_NOPTS_VALUE)
  1257. pts = reordered_pts;
  1258. else
  1259. pts = dts;
  1260. return pts;
  1261. }
  1262. FILE *get_preset_file(char *filename, size_t filename_size,
  1263. const char *preset_name, int is_path,
  1264. const char *codec_name)
  1265. {
  1266. FILE *f = NULL;
  1267. int i;
  1268. const char *base[3] = { getenv("AVCONV_DATADIR"),
  1269. getenv("HOME"),
  1270. AVCONV_DATADIR, };
  1271. if (is_path) {
  1272. av_strlcpy(filename, preset_name, filename_size);
  1273. f = fopen(filename, "r");
  1274. } else {
  1275. for (i = 0; i < 3 && !f; i++) {
  1276. if (!base[i])
  1277. continue;
  1278. snprintf(filename, filename_size, "%s%s/%s.avpreset", base[i],
  1279. i != 1 ? "" : "/.avconv", preset_name);
  1280. f = fopen(filename, "r");
  1281. if (!f && codec_name) {
  1282. snprintf(filename, filename_size,
  1283. "%s%s/%s-%s.avpreset",
  1284. base[i], i != 1 ? "" : "/.avconv", codec_name,
  1285. preset_name);
  1286. f = fopen(filename, "r");
  1287. }
  1288. }
  1289. }
  1290. return f;
  1291. }
  1292. int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
  1293. {
  1294. if (*spec <= '9' && *spec >= '0') /* opt:index */
  1295. return strtol(spec, NULL, 0) == st->index;
  1296. else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
  1297. *spec == 't') { /* opt:[vasdt] */
  1298. enum AVMediaType type;
  1299. switch (*spec++) {
  1300. case 'v': type = AVMEDIA_TYPE_VIDEO; break;
  1301. case 'a': type = AVMEDIA_TYPE_AUDIO; break;
  1302. case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
  1303. case 'd': type = AVMEDIA_TYPE_DATA; break;
  1304. case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
  1305. default: av_assert0(0);
  1306. }
  1307. if (type != st->codecpar->codec_type)
  1308. return 0;
  1309. if (*spec++ == ':') { /* possibly followed by :index */
  1310. int i, index = strtol(spec, NULL, 0);
  1311. for (i = 0; i < s->nb_streams; i++)
  1312. if (s->streams[i]->codecpar->codec_type == type && index-- == 0)
  1313. return i == st->index;
  1314. return 0;
  1315. }
  1316. return 1;
  1317. } else if (*spec == 'p' && *(spec + 1) == ':') {
  1318. int prog_id, i, j;
  1319. char *endptr;
  1320. spec += 2;
  1321. prog_id = strtol(spec, &endptr, 0);
  1322. for (i = 0; i < s->nb_programs; i++) {
  1323. if (s->programs[i]->id != prog_id)
  1324. continue;
  1325. if (*endptr++ == ':') {
  1326. int stream_idx = strtol(endptr, NULL, 0);
  1327. return stream_idx >= 0 &&
  1328. stream_idx < s->programs[i]->nb_stream_indexes &&
  1329. st->index == s->programs[i]->stream_index[stream_idx];
  1330. }
  1331. for (j = 0; j < s->programs[i]->nb_stream_indexes; j++)
  1332. if (st->index == s->programs[i]->stream_index[j])
  1333. return 1;
  1334. }
  1335. return 0;
  1336. } else if (*spec == 'i' && *(spec + 1) == ':') {
  1337. int stream_id;
  1338. char *endptr;
  1339. spec += 2;
  1340. stream_id = strtol(spec, &endptr, 0);
  1341. return stream_id == st->id;
  1342. } else if (*spec == 'm' && *(spec + 1) == ':') {
  1343. AVDictionaryEntry *tag;
  1344. char *key, *val;
  1345. int ret;
  1346. spec += 2;
  1347. val = strchr(spec, ':');
  1348. key = val ? av_strndup(spec, val - spec) : av_strdup(spec);
  1349. if (!key)
  1350. return AVERROR(ENOMEM);
  1351. tag = av_dict_get(st->metadata, key, NULL, 0);
  1352. if (tag) {
  1353. if (!val || !strcmp(tag->value, val + 1))
  1354. ret = 1;
  1355. else
  1356. ret = 0;
  1357. } else
  1358. ret = 0;
  1359. av_freep(&key);
  1360. return ret;
  1361. } else if (*spec == 'u') {
  1362. AVCodecParameters *par = st->codecpar;
  1363. int val;
  1364. switch (par->codec_type) {
  1365. case AVMEDIA_TYPE_AUDIO:
  1366. val = par->sample_rate && par->channels;
  1367. if (par->format == AV_SAMPLE_FMT_NONE)
  1368. return 0;
  1369. break;
  1370. case AVMEDIA_TYPE_VIDEO:
  1371. val = par->width && par->height;
  1372. if (par->format == AV_PIX_FMT_NONE)
  1373. return 0;
  1374. break;
  1375. case AVMEDIA_TYPE_UNKNOWN:
  1376. val = 0;
  1377. break;
  1378. default:
  1379. val = 1;
  1380. break;
  1381. }
  1382. return par->codec_id != AV_CODEC_ID_NONE && val != 0;
  1383. } else if (!*spec) /* empty specifier, matches everything */
  1384. return 1;
  1385. av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
  1386. return AVERROR(EINVAL);
  1387. }
  1388. AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
  1389. AVFormatContext *s, AVStream *st, AVCodec *codec)
  1390. {
  1391. AVDictionary *ret = NULL;
  1392. AVDictionaryEntry *t = NULL;
  1393. int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
  1394. : AV_OPT_FLAG_DECODING_PARAM;
  1395. char prefix = 0;
  1396. const AVClass *cc = avcodec_get_class();
  1397. if (!codec)
  1398. codec = s->oformat ? avcodec_find_encoder(codec_id)
  1399. : avcodec_find_decoder(codec_id);
  1400. switch (st->codecpar->codec_type) {
  1401. case AVMEDIA_TYPE_VIDEO:
  1402. prefix = 'v';
  1403. flags |= AV_OPT_FLAG_VIDEO_PARAM;
  1404. break;
  1405. case AVMEDIA_TYPE_AUDIO:
  1406. prefix = 'a';
  1407. flags |= AV_OPT_FLAG_AUDIO_PARAM;
  1408. break;
  1409. case AVMEDIA_TYPE_SUBTITLE:
  1410. prefix = 's';
  1411. flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
  1412. break;
  1413. }
  1414. while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
  1415. char *p = strchr(t->key, ':');
  1416. /* check stream specification in opt name */
  1417. if (p)
  1418. switch (check_stream_specifier(s, st, p + 1)) {
  1419. case 1: *p = 0; break;
  1420. case 0: continue;
  1421. default: return NULL;
  1422. }
  1423. if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
  1424. (codec && codec->priv_class &&
  1425. av_opt_find(&codec->priv_class, t->key, NULL, flags,
  1426. AV_OPT_SEARCH_FAKE_OBJ)))
  1427. av_dict_set(&ret, t->key, t->value, 0);
  1428. else if (t->key[0] == prefix &&
  1429. av_opt_find(&cc, t->key + 1, NULL, flags,
  1430. AV_OPT_SEARCH_FAKE_OBJ))
  1431. av_dict_set(&ret, t->key + 1, t->value, 0);
  1432. if (p)
  1433. *p = ':';
  1434. }
  1435. return ret;
  1436. }
  1437. AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
  1438. AVDictionary *codec_opts)
  1439. {
  1440. int i;
  1441. AVDictionary **opts;
  1442. if (!s->nb_streams)
  1443. return NULL;
  1444. opts = av_mallocz(s->nb_streams * sizeof(*opts));
  1445. if (!opts) {
  1446. av_log(NULL, AV_LOG_ERROR,
  1447. "Could not alloc memory for stream options.\n");
  1448. return NULL;
  1449. }
  1450. for (i = 0; i < s->nb_streams; i++)
  1451. opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id,
  1452. s, s->streams[i], NULL);
  1453. return opts;
  1454. }
  1455. void *grow_array(void *array, int elem_size, int *size, int new_size)
  1456. {
  1457. if (new_size >= INT_MAX / elem_size) {
  1458. av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
  1459. exit_program(1);
  1460. }
  1461. if (*size < new_size) {
  1462. uint8_t *tmp = av_realloc(array, new_size*elem_size);
  1463. if (!tmp) {
  1464. av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
  1465. exit_program(1);
  1466. }
  1467. memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
  1468. *size = new_size;
  1469. return tmp;
  1470. }
  1471. return array;
  1472. }
  1473. const char *media_type_string(enum AVMediaType media_type)
  1474. {
  1475. switch (media_type) {
  1476. case AVMEDIA_TYPE_VIDEO: return "video";
  1477. case AVMEDIA_TYPE_AUDIO: return "audio";
  1478. case AVMEDIA_TYPE_DATA: return "data";
  1479. case AVMEDIA_TYPE_SUBTITLE: return "subtitle";
  1480. case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
  1481. default: return "unknown";
  1482. }
  1483. }