cmdutils.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  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 "libavfilter/avfilter.h"
  33. #include "libavdevice/avdevice.h"
  34. #include "libavresample/avresample.h"
  35. #include "libswscale/swscale.h"
  36. #include "libswresample/swresample.h"
  37. #include "libpostproc/postprocess.h"
  38. #include "libavutil/attributes.h"
  39. #include "libavutil/avassert.h"
  40. #include "libavutil/avstring.h"
  41. #include "libavutil/bprint.h"
  42. #include "libavutil/display.h"
  43. #include "libavutil/mathematics.h"
  44. #include "libavutil/imgutils.h"
  45. #include "libavutil/libm.h"
  46. #include "libavutil/parseutils.h"
  47. #include "libavutil/pixdesc.h"
  48. #include "libavutil/eval.h"
  49. #include "libavutil/dict.h"
  50. #include "libavutil/opt.h"
  51. #include "libavutil/cpu.h"
  52. #include "libavutil/ffversion.h"
  53. #include "libavutil/version.h"
  54. #include "cmdutils.h"
  55. #if CONFIG_NETWORK
  56. #include "libavformat/network.h"
  57. #endif
  58. #if HAVE_SYS_RESOURCE_H
  59. #include <sys/time.h>
  60. #include <sys/resource.h>
  61. #endif
  62. #ifdef _WIN32
  63. #include <windows.h>
  64. #endif
  65. static int init_report(const char *env);
  66. AVDictionary *sws_dict;
  67. AVDictionary *swr_opts;
  68. AVDictionary *format_opts, *codec_opts, *resample_opts;
  69. static FILE *report_file;
  70. static int report_file_level = AV_LOG_DEBUG;
  71. int hide_banner = 0;
  72. enum show_muxdemuxers {
  73. SHOW_DEFAULT,
  74. SHOW_DEMUXERS,
  75. SHOW_MUXERS,
  76. };
  77. void init_opts(void)
  78. {
  79. av_dict_set(&sws_dict, "flags", "bicubic", 0);
  80. }
  81. void uninit_opts(void)
  82. {
  83. av_dict_free(&swr_opts);
  84. av_dict_free(&sws_dict);
  85. av_dict_free(&format_opts);
  86. av_dict_free(&codec_opts);
  87. av_dict_free(&resample_opts);
  88. }
  89. void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
  90. {
  91. vfprintf(stdout, fmt, vl);
  92. }
  93. static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
  94. {
  95. va_list vl2;
  96. char line[1024];
  97. static int print_prefix = 1;
  98. va_copy(vl2, vl);
  99. av_log_default_callback(ptr, level, fmt, vl);
  100. av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
  101. va_end(vl2);
  102. if (report_file_level >= level) {
  103. fputs(line, report_file);
  104. fflush(report_file);
  105. }
  106. }
  107. void init_dynload(void)
  108. {
  109. #ifdef _WIN32
  110. /* Calling SetDllDirectory with the empty string (but not NULL) removes the
  111. * current working directory from the DLL search path as a security pre-caution. */
  112. SetDllDirectory("");
  113. #endif
  114. }
  115. static void (*program_exit)(int ret);
  116. void register_exit(void (*cb)(int ret))
  117. {
  118. program_exit = cb;
  119. }
  120. void exit_program(int ret)
  121. {
  122. if (program_exit)
  123. program_exit(ret);
  124. exit(ret);
  125. }
  126. double parse_number_or_die(const char *context, const char *numstr, int type,
  127. double min, double max)
  128. {
  129. char *tail;
  130. const char *error;
  131. double d = av_strtod(numstr, &tail);
  132. if (*tail)
  133. error = "Expected number for %s but found: %s\n";
  134. else if (d < min || d > max)
  135. error = "The value for %s was %s which is not within %f - %f\n";
  136. else if (type == OPT_INT64 && (int64_t)d != d)
  137. error = "Expected int64 for %s but found %s\n";
  138. else if (type == OPT_INT && (int)d != d)
  139. error = "Expected int for %s but found %s\n";
  140. else
  141. return d;
  142. av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
  143. exit_program(1);
  144. return 0;
  145. }
  146. int64_t parse_time_or_die(const char *context, const char *timestr,
  147. int is_duration)
  148. {
  149. int64_t us;
  150. if (av_parse_time(&us, timestr, is_duration) < 0) {
  151. av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
  152. is_duration ? "duration" : "date", context, timestr);
  153. exit_program(1);
  154. }
  155. return us;
  156. }
  157. void show_help_options(const OptionDef *options, const char *msg, int req_flags,
  158. int rej_flags, int alt_flags)
  159. {
  160. const OptionDef *po;
  161. int first;
  162. first = 1;
  163. for (po = options; po->name; po++) {
  164. char buf[64];
  165. if (((po->flags & req_flags) != req_flags) ||
  166. (alt_flags && !(po->flags & alt_flags)) ||
  167. (po->flags & rej_flags))
  168. continue;
  169. if (first) {
  170. printf("%s\n", msg);
  171. first = 0;
  172. }
  173. av_strlcpy(buf, po->name, sizeof(buf));
  174. if (po->argname) {
  175. av_strlcat(buf, " ", sizeof(buf));
  176. av_strlcat(buf, po->argname, sizeof(buf));
  177. }
  178. printf("-%-17s %s\n", buf, po->help);
  179. }
  180. printf("\n");
  181. }
  182. void show_help_children(const AVClass *class, int flags)
  183. {
  184. const AVClass *child = NULL;
  185. if (class->option) {
  186. av_opt_show2(&class, NULL, flags, 0);
  187. printf("\n");
  188. }
  189. while (child = av_opt_child_class_next(class, child))
  190. show_help_children(child, flags);
  191. }
  192. static const OptionDef *find_option(const OptionDef *po, const char *name)
  193. {
  194. const char *p = strchr(name, ':');
  195. int len = p ? p - name : strlen(name);
  196. while (po->name) {
  197. if (!strncmp(name, po->name, len) && strlen(po->name) == len)
  198. break;
  199. po++;
  200. }
  201. return po;
  202. }
  203. /* _WIN32 means using the windows libc - cygwin doesn't define that
  204. * by default. HAVE_COMMANDLINETOARGVW is true on cygwin, while
  205. * it doesn't provide the actual command line via GetCommandLineW(). */
  206. #if HAVE_COMMANDLINETOARGVW && defined(_WIN32)
  207. #include <shellapi.h>
  208. /* Will be leaked on exit */
  209. static char** win32_argv_utf8 = NULL;
  210. static int win32_argc = 0;
  211. /**
  212. * Prepare command line arguments for executable.
  213. * For Windows - perform wide-char to UTF-8 conversion.
  214. * Input arguments should be main() function arguments.
  215. * @param argc_ptr Arguments number (including executable)
  216. * @param argv_ptr Arguments list.
  217. */
  218. static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  219. {
  220. char *argstr_flat;
  221. wchar_t **argv_w;
  222. int i, buffsize = 0, offset = 0;
  223. if (win32_argv_utf8) {
  224. *argc_ptr = win32_argc;
  225. *argv_ptr = win32_argv_utf8;
  226. return;
  227. }
  228. win32_argc = 0;
  229. argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
  230. if (win32_argc <= 0 || !argv_w)
  231. return;
  232. /* determine the UTF-8 buffer size (including NULL-termination symbols) */
  233. for (i = 0; i < win32_argc; i++)
  234. buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  235. NULL, 0, NULL, NULL);
  236. win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
  237. argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
  238. if (!win32_argv_utf8) {
  239. LocalFree(argv_w);
  240. return;
  241. }
  242. for (i = 0; i < win32_argc; i++) {
  243. win32_argv_utf8[i] = &argstr_flat[offset];
  244. offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  245. &argstr_flat[offset],
  246. buffsize - offset, NULL, NULL);
  247. }
  248. win32_argv_utf8[i] = NULL;
  249. LocalFree(argv_w);
  250. *argc_ptr = win32_argc;
  251. *argv_ptr = win32_argv_utf8;
  252. }
  253. #else
  254. static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  255. {
  256. /* nothing to do */
  257. }
  258. #endif /* HAVE_COMMANDLINETOARGVW */
  259. static int write_option(void *optctx, const OptionDef *po, const char *opt,
  260. const char *arg)
  261. {
  262. /* new-style options contain an offset into optctx, old-style address of
  263. * a global var*/
  264. void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
  265. (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
  266. int *dstcount;
  267. if (po->flags & OPT_SPEC) {
  268. SpecifierOpt **so = dst;
  269. char *p = strchr(opt, ':');
  270. char *str;
  271. dstcount = (int *)(so + 1);
  272. *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
  273. str = av_strdup(p ? p + 1 : "");
  274. if (!str)
  275. return AVERROR(ENOMEM);
  276. (*so)[*dstcount - 1].specifier = str;
  277. dst = &(*so)[*dstcount - 1].u;
  278. }
  279. if (po->flags & OPT_STRING) {
  280. char *str;
  281. str = av_strdup(arg);
  282. av_freep(dst);
  283. if (!str)
  284. return AVERROR(ENOMEM);
  285. *(char **)dst = str;
  286. } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
  287. *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
  288. } else if (po->flags & OPT_INT64) {
  289. *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
  290. } else if (po->flags & OPT_TIME) {
  291. *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
  292. } else if (po->flags & OPT_FLOAT) {
  293. *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
  294. } else if (po->flags & OPT_DOUBLE) {
  295. *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
  296. } else if (po->u.func_arg) {
  297. int ret = po->u.func_arg(optctx, opt, arg);
  298. if (ret < 0) {
  299. av_log(NULL, AV_LOG_ERROR,
  300. "Failed to set value '%s' for option '%s': %s\n",
  301. arg, opt, av_err2str(ret));
  302. return ret;
  303. }
  304. }
  305. if (po->flags & OPT_EXIT)
  306. exit_program(0);
  307. return 0;
  308. }
  309. int parse_option(void *optctx, const char *opt, const char *arg,
  310. const OptionDef *options)
  311. {
  312. const OptionDef *po;
  313. int ret;
  314. po = find_option(options, opt);
  315. if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
  316. /* handle 'no' bool option */
  317. po = find_option(options, opt + 2);
  318. if ((po->name && (po->flags & OPT_BOOL)))
  319. arg = "0";
  320. } else if (po->flags & OPT_BOOL)
  321. arg = "1";
  322. if (!po->name)
  323. po = find_option(options, "default");
  324. if (!po->name) {
  325. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
  326. return AVERROR(EINVAL);
  327. }
  328. if (po->flags & HAS_ARG && !arg) {
  329. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
  330. return AVERROR(EINVAL);
  331. }
  332. ret = write_option(optctx, po, opt, arg);
  333. if (ret < 0)
  334. return ret;
  335. return !!(po->flags & HAS_ARG);
  336. }
  337. void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
  338. void (*parse_arg_function)(void *, const char*))
  339. {
  340. const char *opt;
  341. int optindex, handleoptions = 1, ret;
  342. /* perform system-dependent conversions for arguments list */
  343. prepare_app_arguments(&argc, &argv);
  344. /* parse options */
  345. optindex = 1;
  346. while (optindex < argc) {
  347. opt = argv[optindex++];
  348. if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
  349. if (opt[1] == '-' && opt[2] == '\0') {
  350. handleoptions = 0;
  351. continue;
  352. }
  353. opt++;
  354. if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
  355. exit_program(1);
  356. optindex += ret;
  357. } else {
  358. if (parse_arg_function)
  359. parse_arg_function(optctx, opt);
  360. }
  361. }
  362. }
  363. int parse_optgroup(void *optctx, OptionGroup *g)
  364. {
  365. int i, ret;
  366. av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
  367. g->group_def->name, g->arg);
  368. for (i = 0; i < g->nb_opts; i++) {
  369. Option *o = &g->opts[i];
  370. if (g->group_def->flags &&
  371. !(g->group_def->flags & o->opt->flags)) {
  372. av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
  373. "%s %s -- you are trying to apply an input option to an "
  374. "output file or vice versa. Move this option before the "
  375. "file it belongs to.\n", o->key, o->opt->help,
  376. g->group_def->name, g->arg);
  377. return AVERROR(EINVAL);
  378. }
  379. av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
  380. o->key, o->opt->help, o->val);
  381. ret = write_option(optctx, o->opt, o->key, o->val);
  382. if (ret < 0)
  383. return ret;
  384. }
  385. av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
  386. return 0;
  387. }
  388. int locate_option(int argc, char **argv, const OptionDef *options,
  389. const char *optname)
  390. {
  391. const OptionDef *po;
  392. int i;
  393. for (i = 1; i < argc; i++) {
  394. const char *cur_opt = argv[i];
  395. if (*cur_opt++ != '-')
  396. continue;
  397. po = find_option(options, cur_opt);
  398. if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
  399. po = find_option(options, cur_opt + 2);
  400. if ((!po->name && !strcmp(cur_opt, optname)) ||
  401. (po->name && !strcmp(optname, po->name)))
  402. return i;
  403. if (!po->name || po->flags & HAS_ARG)
  404. i++;
  405. }
  406. return 0;
  407. }
  408. static void dump_argument(const char *a)
  409. {
  410. const unsigned char *p;
  411. for (p = a; *p; p++)
  412. if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
  413. *p == '_' || (*p >= 'a' && *p <= 'z')))
  414. break;
  415. if (!*p) {
  416. fputs(a, report_file);
  417. return;
  418. }
  419. fputc('"', report_file);
  420. for (p = a; *p; p++) {
  421. if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
  422. fprintf(report_file, "\\%c", *p);
  423. else if (*p < ' ' || *p > '~')
  424. fprintf(report_file, "\\x%02x", *p);
  425. else
  426. fputc(*p, report_file);
  427. }
  428. fputc('"', report_file);
  429. }
  430. static void check_options(const OptionDef *po)
  431. {
  432. while (po->name) {
  433. if (po->flags & OPT_PERFILE)
  434. av_assert0(po->flags & (OPT_INPUT | OPT_OUTPUT));
  435. po++;
  436. }
  437. }
  438. void parse_loglevel(int argc, char **argv, const OptionDef *options)
  439. {
  440. int idx = locate_option(argc, argv, options, "loglevel");
  441. const char *env;
  442. check_options(options);
  443. if (!idx)
  444. idx = locate_option(argc, argv, options, "v");
  445. if (idx && argv[idx + 1])
  446. opt_loglevel(NULL, "loglevel", argv[idx + 1]);
  447. idx = locate_option(argc, argv, options, "report");
  448. if ((env = getenv("FFREPORT")) || idx) {
  449. init_report(env);
  450. if (report_file) {
  451. int i;
  452. fprintf(report_file, "Command line:\n");
  453. for (i = 0; i < argc; i++) {
  454. dump_argument(argv[i]);
  455. fputc(i < argc - 1 ? ' ' : '\n', report_file);
  456. }
  457. fflush(report_file);
  458. }
  459. }
  460. idx = locate_option(argc, argv, options, "hide_banner");
  461. if (idx)
  462. hide_banner = 1;
  463. }
  464. static const AVOption *opt_find(void *obj, const char *name, const char *unit,
  465. int opt_flags, int search_flags)
  466. {
  467. const AVOption *o = av_opt_find(obj, name, unit, opt_flags, search_flags);
  468. if(o && !o->flags)
  469. return NULL;
  470. return o;
  471. }
  472. #define FLAGS (o->type == AV_OPT_TYPE_FLAGS && (arg[0]=='-' || arg[0]=='+')) ? AV_DICT_APPEND : 0
  473. int opt_default(void *optctx, const char *opt, const char *arg)
  474. {
  475. const AVOption *o;
  476. int consumed = 0;
  477. char opt_stripped[128];
  478. const char *p;
  479. const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
  480. #if CONFIG_AVRESAMPLE
  481. const AVClass *rc = avresample_get_class();
  482. #endif
  483. #if CONFIG_SWSCALE
  484. const AVClass *sc = sws_get_class();
  485. #endif
  486. #if CONFIG_SWRESAMPLE
  487. const AVClass *swr_class = swr_get_class();
  488. #endif
  489. if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug"))
  490. av_log_set_level(AV_LOG_DEBUG);
  491. if (!(p = strchr(opt, ':')))
  492. p = opt + strlen(opt);
  493. av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
  494. if ((o = opt_find(&cc, opt_stripped, NULL, 0,
  495. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
  496. ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
  497. (o = opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
  498. av_dict_set(&codec_opts, opt, arg, FLAGS);
  499. consumed = 1;
  500. }
  501. if ((o = opt_find(&fc, opt, NULL, 0,
  502. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  503. av_dict_set(&format_opts, opt, arg, FLAGS);
  504. if (consumed)
  505. av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt);
  506. consumed = 1;
  507. }
  508. #if CONFIG_SWSCALE
  509. if (!consumed && (o = opt_find(&sc, opt, NULL, 0,
  510. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  511. struct SwsContext *sws = sws_alloc_context();
  512. int ret = av_opt_set(sws, opt, arg, 0);
  513. sws_freeContext(sws);
  514. if (!strcmp(opt, "srcw") || !strcmp(opt, "srch") ||
  515. !strcmp(opt, "dstw") || !strcmp(opt, "dsth") ||
  516. !strcmp(opt, "src_format") || !strcmp(opt, "dst_format")) {
  517. av_log(NULL, AV_LOG_ERROR, "Directly using swscale dimensions/format options is not supported, please use the -s or -pix_fmt options\n");
  518. return AVERROR(EINVAL);
  519. }
  520. if (ret < 0) {
  521. av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
  522. return ret;
  523. }
  524. av_dict_set(&sws_dict, opt, arg, FLAGS);
  525. consumed = 1;
  526. }
  527. #else
  528. if (!consumed && !strcmp(opt, "sws_flags")) {
  529. av_log(NULL, AV_LOG_WARNING, "Ignoring %s %s, due to disabled swscale\n", opt, arg);
  530. consumed = 1;
  531. }
  532. #endif
  533. #if CONFIG_SWRESAMPLE
  534. if (!consumed && (o=opt_find(&swr_class, opt, NULL, 0,
  535. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  536. struct SwrContext *swr = swr_alloc();
  537. int ret = av_opt_set(swr, opt, arg, 0);
  538. swr_free(&swr);
  539. if (ret < 0) {
  540. av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
  541. return ret;
  542. }
  543. av_dict_set(&swr_opts, opt, arg, FLAGS);
  544. consumed = 1;
  545. }
  546. #endif
  547. #if CONFIG_AVRESAMPLE
  548. if ((o=opt_find(&rc, opt, NULL, 0,
  549. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  550. av_dict_set(&resample_opts, opt, arg, FLAGS);
  551. consumed = 1;
  552. }
  553. #endif
  554. if (consumed)
  555. return 0;
  556. return AVERROR_OPTION_NOT_FOUND;
  557. }
  558. /*
  559. * Check whether given option is a group separator.
  560. *
  561. * @return index of the group definition that matched or -1 if none
  562. */
  563. static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
  564. const char *opt)
  565. {
  566. int i;
  567. for (i = 0; i < nb_groups; i++) {
  568. const OptionGroupDef *p = &groups[i];
  569. if (p->sep && !strcmp(p->sep, opt))
  570. return i;
  571. }
  572. return -1;
  573. }
  574. /*
  575. * Finish parsing an option group.
  576. *
  577. * @param group_idx which group definition should this group belong to
  578. * @param arg argument of the group delimiting option
  579. */
  580. static void finish_group(OptionParseContext *octx, int group_idx,
  581. const char *arg)
  582. {
  583. OptionGroupList *l = &octx->groups[group_idx];
  584. OptionGroup *g;
  585. GROW_ARRAY(l->groups, l->nb_groups);
  586. g = &l->groups[l->nb_groups - 1];
  587. *g = octx->cur_group;
  588. g->arg = arg;
  589. g->group_def = l->group_def;
  590. g->sws_dict = sws_dict;
  591. g->swr_opts = swr_opts;
  592. g->codec_opts = codec_opts;
  593. g->format_opts = format_opts;
  594. g->resample_opts = resample_opts;
  595. codec_opts = NULL;
  596. format_opts = NULL;
  597. resample_opts = NULL;
  598. sws_dict = NULL;
  599. swr_opts = NULL;
  600. init_opts();
  601. memset(&octx->cur_group, 0, sizeof(octx->cur_group));
  602. }
  603. /*
  604. * Add an option instance to currently parsed group.
  605. */
  606. static void add_opt(OptionParseContext *octx, const OptionDef *opt,
  607. const char *key, const char *val)
  608. {
  609. int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
  610. OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
  611. GROW_ARRAY(g->opts, g->nb_opts);
  612. g->opts[g->nb_opts - 1].opt = opt;
  613. g->opts[g->nb_opts - 1].key = key;
  614. g->opts[g->nb_opts - 1].val = val;
  615. }
  616. static void init_parse_context(OptionParseContext *octx,
  617. const OptionGroupDef *groups, int nb_groups)
  618. {
  619. static const OptionGroupDef global_group = { "global" };
  620. int i;
  621. memset(octx, 0, sizeof(*octx));
  622. octx->nb_groups = nb_groups;
  623. octx->groups = av_mallocz_array(octx->nb_groups, sizeof(*octx->groups));
  624. if (!octx->groups)
  625. exit_program(1);
  626. for (i = 0; i < octx->nb_groups; i++)
  627. octx->groups[i].group_def = &groups[i];
  628. octx->global_opts.group_def = &global_group;
  629. octx->global_opts.arg = "";
  630. init_opts();
  631. }
  632. void uninit_parse_context(OptionParseContext *octx)
  633. {
  634. int i, j;
  635. for (i = 0; i < octx->nb_groups; i++) {
  636. OptionGroupList *l = &octx->groups[i];
  637. for (j = 0; j < l->nb_groups; j++) {
  638. av_freep(&l->groups[j].opts);
  639. av_dict_free(&l->groups[j].codec_opts);
  640. av_dict_free(&l->groups[j].format_opts);
  641. av_dict_free(&l->groups[j].resample_opts);
  642. av_dict_free(&l->groups[j].sws_dict);
  643. av_dict_free(&l->groups[j].swr_opts);
  644. }
  645. av_freep(&l->groups);
  646. }
  647. av_freep(&octx->groups);
  648. av_freep(&octx->cur_group.opts);
  649. av_freep(&octx->global_opts.opts);
  650. uninit_opts();
  651. }
  652. int split_commandline(OptionParseContext *octx, int argc, char *argv[],
  653. const OptionDef *options,
  654. const OptionGroupDef *groups, int nb_groups)
  655. {
  656. int optindex = 1;
  657. int dashdash = -2;
  658. /* perform system-dependent conversions for arguments list */
  659. prepare_app_arguments(&argc, &argv);
  660. init_parse_context(octx, groups, nb_groups);
  661. av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
  662. while (optindex < argc) {
  663. const char *opt = argv[optindex++], *arg;
  664. const OptionDef *po;
  665. int ret;
  666. av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
  667. if (opt[0] == '-' && opt[1] == '-' && !opt[2]) {
  668. dashdash = optindex;
  669. continue;
  670. }
  671. /* unnamed group separators, e.g. output filename */
  672. if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
  673. finish_group(octx, 0, opt);
  674. av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
  675. continue;
  676. }
  677. opt++;
  678. #define GET_ARG(arg) \
  679. do { \
  680. arg = argv[optindex++]; \
  681. if (!arg) { \
  682. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
  683. return AVERROR(EINVAL); \
  684. } \
  685. } while (0)
  686. /* named group separators, e.g. -i */
  687. if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
  688. GET_ARG(arg);
  689. finish_group(octx, ret, arg);
  690. av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
  691. groups[ret].name, arg);
  692. continue;
  693. }
  694. /* normal options */
  695. po = find_option(options, opt);
  696. if (po->name) {
  697. if (po->flags & OPT_EXIT) {
  698. /* optional argument, e.g. -h */
  699. arg = argv[optindex++];
  700. } else if (po->flags & HAS_ARG) {
  701. GET_ARG(arg);
  702. } else {
  703. arg = "1";
  704. }
  705. add_opt(octx, po, opt, arg);
  706. av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
  707. "argument '%s'.\n", po->name, po->help, arg);
  708. continue;
  709. }
  710. /* AVOptions */
  711. if (argv[optindex]) {
  712. ret = opt_default(NULL, opt, argv[optindex]);
  713. if (ret >= 0) {
  714. av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
  715. "argument '%s'.\n", opt, argv[optindex]);
  716. optindex++;
  717. continue;
  718. } else if (ret != AVERROR_OPTION_NOT_FOUND) {
  719. av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
  720. "with argument '%s'.\n", opt, argv[optindex]);
  721. return ret;
  722. }
  723. }
  724. /* boolean -nofoo options */
  725. if (opt[0] == 'n' && opt[1] == 'o' &&
  726. (po = find_option(options, opt + 2)) &&
  727. po->name && po->flags & OPT_BOOL) {
  728. add_opt(octx, po, opt, "0");
  729. av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
  730. "argument 0.\n", po->name, po->help);
  731. continue;
  732. }
  733. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
  734. return AVERROR_OPTION_NOT_FOUND;
  735. }
  736. if (octx->cur_group.nb_opts || codec_opts || format_opts || resample_opts)
  737. av_log(NULL, AV_LOG_WARNING, "Trailing options were found on the "
  738. "commandline.\n");
  739. av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
  740. return 0;
  741. }
  742. int opt_cpuflags(void *optctx, const char *opt, const char *arg)
  743. {
  744. int ret;
  745. unsigned flags = av_get_cpu_flags();
  746. if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
  747. return ret;
  748. av_force_cpu_flags(flags);
  749. return 0;
  750. }
  751. int opt_loglevel(void *optctx, const char *opt, const char *arg)
  752. {
  753. const struct { const char *name; int level; } log_levels[] = {
  754. { "quiet" , AV_LOG_QUIET },
  755. { "panic" , AV_LOG_PANIC },
  756. { "fatal" , AV_LOG_FATAL },
  757. { "error" , AV_LOG_ERROR },
  758. { "warning", AV_LOG_WARNING },
  759. { "info" , AV_LOG_INFO },
  760. { "verbose", AV_LOG_VERBOSE },
  761. { "debug" , AV_LOG_DEBUG },
  762. { "trace" , AV_LOG_TRACE },
  763. };
  764. char *tail;
  765. int level;
  766. int flags;
  767. int i;
  768. flags = av_log_get_flags();
  769. tail = strstr(arg, "repeat");
  770. if (tail)
  771. flags &= ~AV_LOG_SKIP_REPEATED;
  772. else
  773. flags |= AV_LOG_SKIP_REPEATED;
  774. av_log_set_flags(flags);
  775. if (tail == arg)
  776. arg += 6 + (arg[6]=='+');
  777. if(tail && !*arg)
  778. return 0;
  779. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
  780. if (!strcmp(log_levels[i].name, arg)) {
  781. av_log_set_level(log_levels[i].level);
  782. return 0;
  783. }
  784. }
  785. level = strtol(arg, &tail, 10);
  786. if (*tail) {
  787. av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
  788. "Possible levels are numbers or:\n", arg);
  789. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
  790. av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
  791. exit_program(1);
  792. }
  793. av_log_set_level(level);
  794. return 0;
  795. }
  796. static void expand_filename_template(AVBPrint *bp, const char *template,
  797. struct tm *tm)
  798. {
  799. int c;
  800. while ((c = *(template++))) {
  801. if (c == '%') {
  802. if (!(c = *(template++)))
  803. break;
  804. switch (c) {
  805. case 'p':
  806. av_bprintf(bp, "%s", program_name);
  807. break;
  808. case 't':
  809. av_bprintf(bp, "%04d%02d%02d-%02d%02d%02d",
  810. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  811. tm->tm_hour, tm->tm_min, tm->tm_sec);
  812. break;
  813. case '%':
  814. av_bprint_chars(bp, c, 1);
  815. break;
  816. }
  817. } else {
  818. av_bprint_chars(bp, c, 1);
  819. }
  820. }
  821. }
  822. static int init_report(const char *env)
  823. {
  824. char *filename_template = NULL;
  825. char *key, *val;
  826. int ret, count = 0;
  827. time_t now;
  828. struct tm *tm;
  829. AVBPrint filename;
  830. if (report_file) /* already opened */
  831. return 0;
  832. time(&now);
  833. tm = localtime(&now);
  834. while (env && *env) {
  835. if ((ret = av_opt_get_key_value(&env, "=", ":", 0, &key, &val)) < 0) {
  836. if (count)
  837. av_log(NULL, AV_LOG_ERROR,
  838. "Failed to parse FFREPORT environment variable: %s\n",
  839. av_err2str(ret));
  840. break;
  841. }
  842. if (*env)
  843. env++;
  844. count++;
  845. if (!strcmp(key, "file")) {
  846. av_free(filename_template);
  847. filename_template = val;
  848. val = NULL;
  849. } else if (!strcmp(key, "level")) {
  850. char *tail;
  851. report_file_level = strtol(val, &tail, 10);
  852. if (*tail) {
  853. av_log(NULL, AV_LOG_FATAL, "Invalid report file level\n");
  854. exit_program(1);
  855. }
  856. } else {
  857. av_log(NULL, AV_LOG_ERROR, "Unknown key '%s' in FFREPORT\n", key);
  858. }
  859. av_free(val);
  860. av_free(key);
  861. }
  862. av_bprint_init(&filename, 0, 1);
  863. expand_filename_template(&filename,
  864. av_x_if_null(filename_template, "%p-%t.log"), tm);
  865. av_free(filename_template);
  866. if (!av_bprint_is_complete(&filename)) {
  867. av_log(NULL, AV_LOG_ERROR, "Out of memory building report file name\n");
  868. return AVERROR(ENOMEM);
  869. }
  870. report_file = fopen(filename.str, "w");
  871. if (!report_file) {
  872. int ret = AVERROR(errno);
  873. av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
  874. filename.str, strerror(errno));
  875. return ret;
  876. }
  877. av_log_set_callback(log_callback_report);
  878. av_log(NULL, AV_LOG_INFO,
  879. "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
  880. "Report written to \"%s\"\n",
  881. program_name,
  882. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  883. tm->tm_hour, tm->tm_min, tm->tm_sec,
  884. filename.str);
  885. av_bprint_finalize(&filename, NULL);
  886. return 0;
  887. }
  888. int opt_report(const char *opt)
  889. {
  890. return init_report(NULL);
  891. }
  892. int opt_max_alloc(void *optctx, const char *opt, const char *arg)
  893. {
  894. char *tail;
  895. size_t max;
  896. max = strtol(arg, &tail, 10);
  897. if (*tail) {
  898. av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
  899. exit_program(1);
  900. }
  901. av_max_alloc(max);
  902. return 0;
  903. }
  904. int opt_timelimit(void *optctx, const char *opt, const char *arg)
  905. {
  906. #if HAVE_SETRLIMIT
  907. int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
  908. struct rlimit rl = { lim, lim + 1 };
  909. if (setrlimit(RLIMIT_CPU, &rl))
  910. perror("setrlimit");
  911. #else
  912. av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
  913. #endif
  914. return 0;
  915. }
  916. void print_error(const char *filename, int err)
  917. {
  918. char errbuf[128];
  919. const char *errbuf_ptr = errbuf;
  920. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  921. errbuf_ptr = strerror(AVUNERROR(err));
  922. av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
  923. }
  924. static int warned_cfg = 0;
  925. #define INDENT 1
  926. #define SHOW_VERSION 2
  927. #define SHOW_CONFIG 4
  928. #define SHOW_COPYRIGHT 8
  929. #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
  930. if (CONFIG_##LIBNAME) { \
  931. const char *indent = flags & INDENT? " " : ""; \
  932. if (flags & SHOW_VERSION) { \
  933. unsigned int version = libname##_version(); \
  934. av_log(NULL, level, \
  935. "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
  936. indent, #libname, \
  937. LIB##LIBNAME##_VERSION_MAJOR, \
  938. LIB##LIBNAME##_VERSION_MINOR, \
  939. LIB##LIBNAME##_VERSION_MICRO, \
  940. AV_VERSION_MAJOR(version), AV_VERSION_MINOR(version),\
  941. AV_VERSION_MICRO(version)); \
  942. } \
  943. if (flags & SHOW_CONFIG) { \
  944. const char *cfg = libname##_configuration(); \
  945. if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
  946. if (!warned_cfg) { \
  947. av_log(NULL, level, \
  948. "%sWARNING: library configuration mismatch\n", \
  949. indent); \
  950. warned_cfg = 1; \
  951. } \
  952. av_log(NULL, level, "%s%-11s configuration: %s\n", \
  953. indent, #libname, cfg); \
  954. } \
  955. } \
  956. } \
  957. static void print_all_libs_info(int flags, int level)
  958. {
  959. PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
  960. PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
  961. PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
  962. PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
  963. PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
  964. PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
  965. PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
  966. PRINT_LIB_INFO(swresample, SWRESAMPLE, flags, level);
  967. PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
  968. }
  969. static void print_program_info(int flags, int level)
  970. {
  971. const char *indent = flags & INDENT? " " : "";
  972. av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
  973. if (flags & SHOW_COPYRIGHT)
  974. av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
  975. program_birth_year, CONFIG_THIS_YEAR);
  976. av_log(NULL, level, "\n");
  977. av_log(NULL, level, "%sbuilt with %s\n", indent, CC_IDENT);
  978. av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
  979. }
  980. static void print_buildconf(int flags, int level)
  981. {
  982. const char *indent = flags & INDENT ? " " : "";
  983. char str[] = { FFMPEG_CONFIGURATION };
  984. char *conflist, *remove_tilde, *splitconf;
  985. // Change all the ' --' strings to '~--' so that
  986. // they can be identified as tokens.
  987. while ((conflist = strstr(str, " --")) != NULL) {
  988. strncpy(conflist, "~--", 3);
  989. }
  990. // Compensate for the weirdness this would cause
  991. // when passing 'pkg-config --static'.
  992. while ((remove_tilde = strstr(str, "pkg-config~")) != NULL) {
  993. strncpy(remove_tilde, "pkg-config ", 11);
  994. }
  995. splitconf = strtok(str, "~");
  996. av_log(NULL, level, "\n%sconfiguration:\n", indent);
  997. while (splitconf != NULL) {
  998. av_log(NULL, level, "%s%s%s\n", indent, indent, splitconf);
  999. splitconf = strtok(NULL, "~");
  1000. }
  1001. }
  1002. void show_banner(int argc, char **argv, const OptionDef *options)
  1003. {
  1004. int idx = locate_option(argc, argv, options, "version");
  1005. if (hide_banner || idx)
  1006. return;
  1007. print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
  1008. print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
  1009. print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
  1010. }
  1011. int show_version(void *optctx, const char *opt, const char *arg)
  1012. {
  1013. av_log_set_callback(log_callback_help);
  1014. print_program_info (SHOW_COPYRIGHT, AV_LOG_INFO);
  1015. print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
  1016. return 0;
  1017. }
  1018. int show_buildconf(void *optctx, const char *opt, const char *arg)
  1019. {
  1020. av_log_set_callback(log_callback_help);
  1021. print_buildconf (INDENT|0, AV_LOG_INFO);
  1022. return 0;
  1023. }
  1024. int show_license(void *optctx, const char *opt, const char *arg)
  1025. {
  1026. #if CONFIG_NONFREE
  1027. printf(
  1028. "This version of %s has nonfree parts compiled in.\n"
  1029. "Therefore it is not legally redistributable.\n",
  1030. program_name );
  1031. #elif CONFIG_GPLV3
  1032. printf(
  1033. "%s is free software; you can redistribute it and/or modify\n"
  1034. "it under the terms of the GNU General Public License as published by\n"
  1035. "the Free Software Foundation; either version 3 of the License, or\n"
  1036. "(at your option) any later version.\n"
  1037. "\n"
  1038. "%s is distributed in the hope that it will be useful,\n"
  1039. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1040. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1041. "GNU General Public License for more details.\n"
  1042. "\n"
  1043. "You should have received a copy of the GNU General Public License\n"
  1044. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  1045. program_name, program_name, program_name );
  1046. #elif CONFIG_GPL
  1047. printf(
  1048. "%s is free software; you can redistribute it and/or modify\n"
  1049. "it under the terms of the GNU General Public License as published by\n"
  1050. "the Free Software Foundation; either version 2 of the License, or\n"
  1051. "(at your option) any later version.\n"
  1052. "\n"
  1053. "%s is distributed in the hope that it will be useful,\n"
  1054. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1055. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1056. "GNU General Public License for more details.\n"
  1057. "\n"
  1058. "You should have received a copy of the GNU General Public License\n"
  1059. "along with %s; if not, write to the Free Software\n"
  1060. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  1061. program_name, program_name, program_name );
  1062. #elif CONFIG_LGPLV3
  1063. printf(
  1064. "%s is free software; you can redistribute it and/or modify\n"
  1065. "it under the terms of the GNU Lesser General Public License as published by\n"
  1066. "the Free Software Foundation; either version 3 of the License, or\n"
  1067. "(at your option) any later version.\n"
  1068. "\n"
  1069. "%s is distributed in the hope that it will be useful,\n"
  1070. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1071. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1072. "GNU Lesser General Public License for more details.\n"
  1073. "\n"
  1074. "You should have received a copy of the GNU Lesser General Public License\n"
  1075. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  1076. program_name, program_name, program_name );
  1077. #else
  1078. printf(
  1079. "%s is free software; you can redistribute it and/or\n"
  1080. "modify it under the terms of the GNU Lesser General Public\n"
  1081. "License as published by the Free Software Foundation; either\n"
  1082. "version 2.1 of the License, or (at your option) any later version.\n"
  1083. "\n"
  1084. "%s is distributed in the hope that it will be useful,\n"
  1085. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1086. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  1087. "Lesser General Public License for more details.\n"
  1088. "\n"
  1089. "You should have received a copy of the GNU Lesser General Public\n"
  1090. "License along with %s; if not, write to the Free Software\n"
  1091. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  1092. program_name, program_name, program_name );
  1093. #endif
  1094. return 0;
  1095. }
  1096. static int is_device(const AVClass *avclass)
  1097. {
  1098. if (!avclass)
  1099. return 0;
  1100. return AV_IS_INPUT_DEVICE(avclass->category) || AV_IS_OUTPUT_DEVICE(avclass->category);
  1101. }
  1102. static int show_formats_devices(void *optctx, const char *opt, const char *arg, int device_only, int muxdemuxers)
  1103. {
  1104. AVInputFormat *ifmt = NULL;
  1105. AVOutputFormat *ofmt = NULL;
  1106. const char *last_name;
  1107. int is_dev;
  1108. printf("%s\n"
  1109. " D. = Demuxing supported\n"
  1110. " .E = Muxing supported\n"
  1111. " --\n", device_only ? "Devices:" : "File formats:");
  1112. last_name = "000";
  1113. for (;;) {
  1114. int decode = 0;
  1115. int encode = 0;
  1116. const char *name = NULL;
  1117. const char *long_name = NULL;
  1118. if (muxdemuxers !=SHOW_DEMUXERS) {
  1119. while ((ofmt = av_oformat_next(ofmt))) {
  1120. is_dev = is_device(ofmt->priv_class);
  1121. if (!is_dev && device_only)
  1122. continue;
  1123. if ((!name || strcmp(ofmt->name, name) < 0) &&
  1124. strcmp(ofmt->name, last_name) > 0) {
  1125. name = ofmt->name;
  1126. long_name = ofmt->long_name;
  1127. encode = 1;
  1128. }
  1129. }
  1130. }
  1131. if (muxdemuxers != SHOW_MUXERS) {
  1132. while ((ifmt = av_iformat_next(ifmt))) {
  1133. is_dev = is_device(ifmt->priv_class);
  1134. if (!is_dev && device_only)
  1135. continue;
  1136. if ((!name || strcmp(ifmt->name, name) < 0) &&
  1137. strcmp(ifmt->name, last_name) > 0) {
  1138. name = ifmt->name;
  1139. long_name = ifmt->long_name;
  1140. encode = 0;
  1141. }
  1142. if (name && strcmp(ifmt->name, name) == 0)
  1143. decode = 1;
  1144. }
  1145. }
  1146. if (!name)
  1147. break;
  1148. last_name = name;
  1149. printf(" %s%s %-15s %s\n",
  1150. decode ? "D" : " ",
  1151. encode ? "E" : " ",
  1152. name,
  1153. long_name ? long_name:" ");
  1154. }
  1155. return 0;
  1156. }
  1157. int show_formats(void *optctx, const char *opt, const char *arg)
  1158. {
  1159. return show_formats_devices(optctx, opt, arg, 0, SHOW_DEFAULT);
  1160. }
  1161. int show_muxers(void *optctx, const char *opt, const char *arg)
  1162. {
  1163. return show_formats_devices(optctx, opt, arg, 0, SHOW_MUXERS);
  1164. }
  1165. int show_demuxers(void *optctx, const char *opt, const char *arg)
  1166. {
  1167. return show_formats_devices(optctx, opt, arg, 0, SHOW_DEMUXERS);
  1168. }
  1169. int show_devices(void *optctx, const char *opt, const char *arg)
  1170. {
  1171. return show_formats_devices(optctx, opt, arg, 1, SHOW_DEFAULT);
  1172. }
  1173. #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
  1174. if (codec->field) { \
  1175. const type *p = codec->field; \
  1176. \
  1177. printf(" Supported " list_name ":"); \
  1178. while (*p != term) { \
  1179. get_name(*p); \
  1180. printf(" %s", name); \
  1181. p++; \
  1182. } \
  1183. printf("\n"); \
  1184. } \
  1185. static void print_codec(const AVCodec *c)
  1186. {
  1187. int encoder = av_codec_is_encoder(c);
  1188. printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
  1189. c->long_name ? c->long_name : "");
  1190. printf(" General capabilities: ");
  1191. if (c->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND)
  1192. printf("horizband ");
  1193. if (c->capabilities & AV_CODEC_CAP_DR1)
  1194. printf("dr1 ");
  1195. if (c->capabilities & AV_CODEC_CAP_TRUNCATED)
  1196. printf("trunc ");
  1197. if (c->capabilities & AV_CODEC_CAP_DELAY)
  1198. printf("delay ");
  1199. if (c->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)
  1200. printf("small ");
  1201. if (c->capabilities & AV_CODEC_CAP_SUBFRAMES)
  1202. printf("subframes ");
  1203. if (c->capabilities & AV_CODEC_CAP_EXPERIMENTAL)
  1204. printf("exp ");
  1205. if (c->capabilities & AV_CODEC_CAP_CHANNEL_CONF)
  1206. printf("chconf ");
  1207. if (c->capabilities & AV_CODEC_CAP_PARAM_CHANGE)
  1208. printf("paramchange ");
  1209. if (c->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
  1210. printf("variable ");
  1211. if (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
  1212. AV_CODEC_CAP_SLICE_THREADS |
  1213. AV_CODEC_CAP_AUTO_THREADS))
  1214. printf("threads ");
  1215. if (!c->capabilities)
  1216. printf("none");
  1217. printf("\n");
  1218. if (c->type == AVMEDIA_TYPE_VIDEO ||
  1219. c->type == AVMEDIA_TYPE_AUDIO) {
  1220. printf(" Threading capabilities: ");
  1221. switch (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
  1222. AV_CODEC_CAP_SLICE_THREADS |
  1223. AV_CODEC_CAP_AUTO_THREADS)) {
  1224. case AV_CODEC_CAP_FRAME_THREADS |
  1225. AV_CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
  1226. case AV_CODEC_CAP_FRAME_THREADS: printf("frame"); break;
  1227. case AV_CODEC_CAP_SLICE_THREADS: printf("slice"); break;
  1228. case AV_CODEC_CAP_AUTO_THREADS : printf("auto"); break;
  1229. default: printf("none"); break;
  1230. }
  1231. printf("\n");
  1232. }
  1233. if (c->supported_framerates) {
  1234. const AVRational *fps = c->supported_framerates;
  1235. printf(" Supported framerates:");
  1236. while (fps->num) {
  1237. printf(" %d/%d", fps->num, fps->den);
  1238. fps++;
  1239. }
  1240. printf("\n");
  1241. }
  1242. PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
  1243. AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
  1244. PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
  1245. GET_SAMPLE_RATE_NAME);
  1246. PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
  1247. AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
  1248. PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
  1249. 0, GET_CH_LAYOUT_DESC);
  1250. if (c->priv_class) {
  1251. show_help_children(c->priv_class,
  1252. AV_OPT_FLAG_ENCODING_PARAM |
  1253. AV_OPT_FLAG_DECODING_PARAM);
  1254. }
  1255. }
  1256. static char get_media_type_char(enum AVMediaType type)
  1257. {
  1258. switch (type) {
  1259. case AVMEDIA_TYPE_VIDEO: return 'V';
  1260. case AVMEDIA_TYPE_AUDIO: return 'A';
  1261. case AVMEDIA_TYPE_DATA: return 'D';
  1262. case AVMEDIA_TYPE_SUBTITLE: return 'S';
  1263. case AVMEDIA_TYPE_ATTACHMENT:return 'T';
  1264. default: return '?';
  1265. }
  1266. }
  1267. static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
  1268. int encoder)
  1269. {
  1270. while ((prev = av_codec_next(prev))) {
  1271. if (prev->id == id &&
  1272. (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
  1273. return prev;
  1274. }
  1275. return NULL;
  1276. }
  1277. static int compare_codec_desc(const void *a, const void *b)
  1278. {
  1279. const AVCodecDescriptor * const *da = a;
  1280. const AVCodecDescriptor * const *db = b;
  1281. return (*da)->type != (*db)->type ? FFDIFFSIGN((*da)->type, (*db)->type) :
  1282. strcmp((*da)->name, (*db)->name);
  1283. }
  1284. static unsigned get_codecs_sorted(const AVCodecDescriptor ***rcodecs)
  1285. {
  1286. const AVCodecDescriptor *desc = NULL;
  1287. const AVCodecDescriptor **codecs;
  1288. unsigned nb_codecs = 0, i = 0;
  1289. while ((desc = avcodec_descriptor_next(desc)))
  1290. nb_codecs++;
  1291. if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs)))) {
  1292. av_log(NULL, AV_LOG_ERROR, "Out of memory\n");
  1293. exit_program(1);
  1294. }
  1295. desc = NULL;
  1296. while ((desc = avcodec_descriptor_next(desc)))
  1297. codecs[i++] = desc;
  1298. av_assert0(i == nb_codecs);
  1299. qsort(codecs, nb_codecs, sizeof(*codecs), compare_codec_desc);
  1300. *rcodecs = codecs;
  1301. return nb_codecs;
  1302. }
  1303. static void print_codecs_for_id(enum AVCodecID id, int encoder)
  1304. {
  1305. const AVCodec *codec = NULL;
  1306. printf(" (%s: ", encoder ? "encoders" : "decoders");
  1307. while ((codec = next_codec_for_id(id, codec, encoder)))
  1308. printf("%s ", codec->name);
  1309. printf(")");
  1310. }
  1311. int show_codecs(void *optctx, const char *opt, const char *arg)
  1312. {
  1313. const AVCodecDescriptor **codecs;
  1314. unsigned i, nb_codecs = get_codecs_sorted(&codecs);
  1315. printf("Codecs:\n"
  1316. " D..... = Decoding supported\n"
  1317. " .E.... = Encoding supported\n"
  1318. " ..V... = Video codec\n"
  1319. " ..A... = Audio codec\n"
  1320. " ..S... = Subtitle codec\n"
  1321. " ...I.. = Intra frame-only codec\n"
  1322. " ....L. = Lossy compression\n"
  1323. " .....S = Lossless compression\n"
  1324. " -------\n");
  1325. for (i = 0; i < nb_codecs; i++) {
  1326. const AVCodecDescriptor *desc = codecs[i];
  1327. const AVCodec *codec = NULL;
  1328. if (strstr(desc->name, "_deprecated"))
  1329. continue;
  1330. printf(" ");
  1331. printf(avcodec_find_decoder(desc->id) ? "D" : ".");
  1332. printf(avcodec_find_encoder(desc->id) ? "E" : ".");
  1333. printf("%c", get_media_type_char(desc->type));
  1334. printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
  1335. printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
  1336. printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
  1337. printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
  1338. /* print decoders/encoders when there's more than one or their
  1339. * names are different from codec name */
  1340. while ((codec = next_codec_for_id(desc->id, codec, 0))) {
  1341. if (strcmp(codec->name, desc->name)) {
  1342. print_codecs_for_id(desc->id, 0);
  1343. break;
  1344. }
  1345. }
  1346. codec = NULL;
  1347. while ((codec = next_codec_for_id(desc->id, codec, 1))) {
  1348. if (strcmp(codec->name, desc->name)) {
  1349. print_codecs_for_id(desc->id, 1);
  1350. break;
  1351. }
  1352. }
  1353. printf("\n");
  1354. }
  1355. av_free(codecs);
  1356. return 0;
  1357. }
  1358. static void print_codecs(int encoder)
  1359. {
  1360. const AVCodecDescriptor **codecs;
  1361. unsigned i, nb_codecs = get_codecs_sorted(&codecs);
  1362. printf("%s:\n"
  1363. " V..... = Video\n"
  1364. " A..... = Audio\n"
  1365. " S..... = Subtitle\n"
  1366. " .F.... = Frame-level multithreading\n"
  1367. " ..S... = Slice-level multithreading\n"
  1368. " ...X.. = Codec is experimental\n"
  1369. " ....B. = Supports draw_horiz_band\n"
  1370. " .....D = Supports direct rendering method 1\n"
  1371. " ------\n",
  1372. encoder ? "Encoders" : "Decoders");
  1373. for (i = 0; i < nb_codecs; i++) {
  1374. const AVCodecDescriptor *desc = codecs[i];
  1375. const AVCodec *codec = NULL;
  1376. while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
  1377. printf(" %c", get_media_type_char(desc->type));
  1378. printf((codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) ? "F" : ".");
  1379. printf((codec->capabilities & AV_CODEC_CAP_SLICE_THREADS) ? "S" : ".");
  1380. printf((codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
  1381. printf((codec->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND)?"B" : ".");
  1382. printf((codec->capabilities & AV_CODEC_CAP_DR1) ? "D" : ".");
  1383. printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
  1384. if (strcmp(codec->name, desc->name))
  1385. printf(" (codec %s)", desc->name);
  1386. printf("\n");
  1387. }
  1388. }
  1389. av_free(codecs);
  1390. }
  1391. int show_decoders(void *optctx, const char *opt, const char *arg)
  1392. {
  1393. print_codecs(0);
  1394. return 0;
  1395. }
  1396. int show_encoders(void *optctx, const char *opt, const char *arg)
  1397. {
  1398. print_codecs(1);
  1399. return 0;
  1400. }
  1401. int show_bsfs(void *optctx, const char *opt, const char *arg)
  1402. {
  1403. const AVBitStreamFilter *bsf = NULL;
  1404. void *opaque = NULL;
  1405. printf("Bitstream filters:\n");
  1406. while ((bsf = av_bsf_iterate(&opaque)))
  1407. printf("%s\n", bsf->name);
  1408. printf("\n");
  1409. return 0;
  1410. }
  1411. int show_protocols(void *optctx, const char *opt, const char *arg)
  1412. {
  1413. void *opaque = NULL;
  1414. const char *name;
  1415. printf("Supported file protocols:\n"
  1416. "Input:\n");
  1417. while ((name = avio_enum_protocols(&opaque, 0)))
  1418. printf(" %s\n", name);
  1419. printf("Output:\n");
  1420. while ((name = avio_enum_protocols(&opaque, 1)))
  1421. printf(" %s\n", name);
  1422. return 0;
  1423. }
  1424. int show_filters(void *optctx, const char *opt, const char *arg)
  1425. {
  1426. #if CONFIG_AVFILTER
  1427. const AVFilter *filter = NULL;
  1428. char descr[64], *descr_cur;
  1429. int i, j;
  1430. const AVFilterPad *pad;
  1431. printf("Filters:\n"
  1432. " T.. = Timeline support\n"
  1433. " .S. = Slice threading\n"
  1434. " ..C = Command support\n"
  1435. " A = Audio input/output\n"
  1436. " V = Video input/output\n"
  1437. " N = Dynamic number and/or type of input/output\n"
  1438. " | = Source or sink filter\n");
  1439. while ((filter = avfilter_next(filter))) {
  1440. descr_cur = descr;
  1441. for (i = 0; i < 2; i++) {
  1442. if (i) {
  1443. *(descr_cur++) = '-';
  1444. *(descr_cur++) = '>';
  1445. }
  1446. pad = i ? filter->outputs : filter->inputs;
  1447. for (j = 0; pad && avfilter_pad_get_name(pad, j); j++) {
  1448. if (descr_cur >= descr + sizeof(descr) - 4)
  1449. break;
  1450. *(descr_cur++) = get_media_type_char(avfilter_pad_get_type(pad, j));
  1451. }
  1452. if (!j)
  1453. *(descr_cur++) = ((!i && (filter->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)) ||
  1454. ( i && (filter->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS))) ? 'N' : '|';
  1455. }
  1456. *descr_cur = 0;
  1457. printf(" %c%c%c %-17s %-10s %s\n",
  1458. filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE ? 'T' : '.',
  1459. filter->flags & AVFILTER_FLAG_SLICE_THREADS ? 'S' : '.',
  1460. filter->process_command ? 'C' : '.',
  1461. filter->name, descr, filter->description);
  1462. }
  1463. #else
  1464. printf("No filters available: libavfilter disabled\n");
  1465. #endif
  1466. return 0;
  1467. }
  1468. int show_colors(void *optctx, const char *opt, const char *arg)
  1469. {
  1470. const char *name;
  1471. const uint8_t *rgb;
  1472. int i;
  1473. printf("%-32s #RRGGBB\n", "name");
  1474. for (i = 0; name = av_get_known_color_name(i, &rgb); i++)
  1475. printf("%-32s #%02x%02x%02x\n", name, rgb[0], rgb[1], rgb[2]);
  1476. return 0;
  1477. }
  1478. int show_pix_fmts(void *optctx, const char *opt, const char *arg)
  1479. {
  1480. const AVPixFmtDescriptor *pix_desc = NULL;
  1481. printf("Pixel formats:\n"
  1482. "I.... = Supported Input format for conversion\n"
  1483. ".O... = Supported Output format for conversion\n"
  1484. "..H.. = Hardware accelerated format\n"
  1485. "...P. = Paletted format\n"
  1486. "....B = Bitstream format\n"
  1487. "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
  1488. "-----\n");
  1489. #if !CONFIG_SWSCALE
  1490. # define sws_isSupportedInput(x) 0
  1491. # define sws_isSupportedOutput(x) 0
  1492. #endif
  1493. while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
  1494. enum AVPixelFormat av_unused pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
  1495. printf("%c%c%c%c%c %-16s %d %2d\n",
  1496. sws_isSupportedInput (pix_fmt) ? 'I' : '.',
  1497. sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
  1498. pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
  1499. pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
  1500. pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
  1501. pix_desc->name,
  1502. pix_desc->nb_components,
  1503. av_get_bits_per_pixel(pix_desc));
  1504. }
  1505. return 0;
  1506. }
  1507. int show_layouts(void *optctx, const char *opt, const char *arg)
  1508. {
  1509. int i = 0;
  1510. uint64_t layout, j;
  1511. const char *name, *descr;
  1512. printf("Individual channels:\n"
  1513. "NAME DESCRIPTION\n");
  1514. for (i = 0; i < 63; i++) {
  1515. name = av_get_channel_name((uint64_t)1 << i);
  1516. if (!name)
  1517. continue;
  1518. descr = av_get_channel_description((uint64_t)1 << i);
  1519. printf("%-14s %s\n", name, descr);
  1520. }
  1521. printf("\nStandard channel layouts:\n"
  1522. "NAME DECOMPOSITION\n");
  1523. for (i = 0; !av_get_standard_channel_layout(i, &layout, &name); i++) {
  1524. if (name) {
  1525. printf("%-14s ", name);
  1526. for (j = 1; j; j <<= 1)
  1527. if ((layout & j))
  1528. printf("%s%s", (layout & (j - 1)) ? "+" : "", av_get_channel_name(j));
  1529. printf("\n");
  1530. }
  1531. }
  1532. return 0;
  1533. }
  1534. int show_sample_fmts(void *optctx, const char *opt, const char *arg)
  1535. {
  1536. int i;
  1537. char fmt_str[128];
  1538. for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
  1539. printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
  1540. return 0;
  1541. }
  1542. static void show_help_codec(const char *name, int encoder)
  1543. {
  1544. const AVCodecDescriptor *desc;
  1545. const AVCodec *codec;
  1546. if (!name) {
  1547. av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
  1548. return;
  1549. }
  1550. codec = encoder ? avcodec_find_encoder_by_name(name) :
  1551. avcodec_find_decoder_by_name(name);
  1552. if (codec)
  1553. print_codec(codec);
  1554. else if ((desc = avcodec_descriptor_get_by_name(name))) {
  1555. int printed = 0;
  1556. while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
  1557. printed = 1;
  1558. print_codec(codec);
  1559. }
  1560. if (!printed) {
  1561. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
  1562. "but no %s for it are available. FFmpeg might need to be "
  1563. "recompiled with additional external libraries.\n",
  1564. name, encoder ? "encoders" : "decoders");
  1565. }
  1566. } else {
  1567. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
  1568. name);
  1569. }
  1570. }
  1571. static void show_help_demuxer(const char *name)
  1572. {
  1573. const AVInputFormat *fmt = av_find_input_format(name);
  1574. if (!fmt) {
  1575. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1576. return;
  1577. }
  1578. printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
  1579. if (fmt->extensions)
  1580. printf(" Common extensions: %s.\n", fmt->extensions);
  1581. if (fmt->priv_class)
  1582. show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
  1583. }
  1584. static void show_help_muxer(const char *name)
  1585. {
  1586. const AVCodecDescriptor *desc;
  1587. const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
  1588. if (!fmt) {
  1589. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1590. return;
  1591. }
  1592. printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
  1593. if (fmt->extensions)
  1594. printf(" Common extensions: %s.\n", fmt->extensions);
  1595. if (fmt->mime_type)
  1596. printf(" Mime type: %s.\n", fmt->mime_type);
  1597. if (fmt->video_codec != AV_CODEC_ID_NONE &&
  1598. (desc = avcodec_descriptor_get(fmt->video_codec))) {
  1599. printf(" Default video codec: %s.\n", desc->name);
  1600. }
  1601. if (fmt->audio_codec != AV_CODEC_ID_NONE &&
  1602. (desc = avcodec_descriptor_get(fmt->audio_codec))) {
  1603. printf(" Default audio codec: %s.\n", desc->name);
  1604. }
  1605. if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
  1606. (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
  1607. printf(" Default subtitle codec: %s.\n", desc->name);
  1608. }
  1609. if (fmt->priv_class)
  1610. show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
  1611. }
  1612. #if CONFIG_AVFILTER
  1613. static void show_help_filter(const char *name)
  1614. {
  1615. #if CONFIG_AVFILTER
  1616. const AVFilter *f = avfilter_get_by_name(name);
  1617. int i, count;
  1618. if (!name) {
  1619. av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
  1620. return;
  1621. } else if (!f) {
  1622. av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
  1623. return;
  1624. }
  1625. printf("Filter %s\n", f->name);
  1626. if (f->description)
  1627. printf(" %s\n", f->description);
  1628. if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
  1629. printf(" slice threading supported\n");
  1630. printf(" Inputs:\n");
  1631. count = avfilter_pad_count(f->inputs);
  1632. for (i = 0; i < count; i++) {
  1633. printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
  1634. media_type_string(avfilter_pad_get_type(f->inputs, i)));
  1635. }
  1636. if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
  1637. printf(" dynamic (depending on the options)\n");
  1638. else if (!count)
  1639. printf(" none (source filter)\n");
  1640. printf(" Outputs:\n");
  1641. count = avfilter_pad_count(f->outputs);
  1642. for (i = 0; i < count; i++) {
  1643. printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
  1644. media_type_string(avfilter_pad_get_type(f->outputs, i)));
  1645. }
  1646. if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
  1647. printf(" dynamic (depending on the options)\n");
  1648. else if (!count)
  1649. printf(" none (sink filter)\n");
  1650. if (f->priv_class)
  1651. show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM |
  1652. AV_OPT_FLAG_AUDIO_PARAM);
  1653. if (f->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)
  1654. printf("This filter has support for timeline through the 'enable' option.\n");
  1655. #else
  1656. av_log(NULL, AV_LOG_ERROR, "Build without libavfilter; "
  1657. "can not to satisfy request\n");
  1658. #endif
  1659. }
  1660. #endif
  1661. static void show_help_bsf(const char *name)
  1662. {
  1663. const AVBitStreamFilter *bsf = av_bsf_get_by_name(name);
  1664. if (!bsf) {
  1665. av_log(NULL, AV_LOG_ERROR, "Unknown bit stream filter '%s'.\n", name);
  1666. return;
  1667. }
  1668. printf("Bit stream filter %s\n", bsf->name);
  1669. PRINT_CODEC_SUPPORTED(bsf, codec_ids, enum AVCodecID, "codecs",
  1670. AV_CODEC_ID_NONE, GET_CODEC_NAME);
  1671. if (bsf->priv_class)
  1672. show_help_children(bsf->priv_class, AV_OPT_FLAG_BSF_PARAM);
  1673. }
  1674. int show_help(void *optctx, const char *opt, const char *arg)
  1675. {
  1676. char *topic, *par;
  1677. av_log_set_callback(log_callback_help);
  1678. topic = av_strdup(arg ? arg : "");
  1679. if (!topic)
  1680. return AVERROR(ENOMEM);
  1681. par = strchr(topic, '=');
  1682. if (par)
  1683. *par++ = 0;
  1684. if (!*topic) {
  1685. show_help_default(topic, par);
  1686. } else if (!strcmp(topic, "decoder")) {
  1687. show_help_codec(par, 0);
  1688. } else if (!strcmp(topic, "encoder")) {
  1689. show_help_codec(par, 1);
  1690. } else if (!strcmp(topic, "demuxer")) {
  1691. show_help_demuxer(par);
  1692. } else if (!strcmp(topic, "muxer")) {
  1693. show_help_muxer(par);
  1694. #if CONFIG_AVFILTER
  1695. } else if (!strcmp(topic, "filter")) {
  1696. show_help_filter(par);
  1697. #endif
  1698. } else if (!strcmp(topic, "bsf")) {
  1699. show_help_bsf(par);
  1700. } else {
  1701. show_help_default(topic, par);
  1702. }
  1703. av_freep(&topic);
  1704. return 0;
  1705. }
  1706. int read_yesno(void)
  1707. {
  1708. int c = getchar();
  1709. int yesno = (av_toupper(c) == 'Y');
  1710. while (c != '\n' && c != EOF)
  1711. c = getchar();
  1712. return yesno;
  1713. }
  1714. FILE *get_preset_file(char *filename, size_t filename_size,
  1715. const char *preset_name, int is_path,
  1716. const char *codec_name)
  1717. {
  1718. FILE *f = NULL;
  1719. int i;
  1720. const char *base[3] = { getenv("FFMPEG_DATADIR"),
  1721. getenv("HOME"),
  1722. FFMPEG_DATADIR, };
  1723. if (is_path) {
  1724. av_strlcpy(filename, preset_name, filename_size);
  1725. f = fopen(filename, "r");
  1726. } else {
  1727. #ifdef _WIN32
  1728. char datadir[MAX_PATH], *ls;
  1729. base[2] = NULL;
  1730. if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
  1731. {
  1732. for (ls = datadir; ls < datadir + strlen(datadir); ls++)
  1733. if (*ls == '\\') *ls = '/';
  1734. if (ls = strrchr(datadir, '/'))
  1735. {
  1736. *ls = 0;
  1737. strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
  1738. base[2] = datadir;
  1739. }
  1740. }
  1741. #endif
  1742. for (i = 0; i < 3 && !f; i++) {
  1743. if (!base[i])
  1744. continue;
  1745. snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
  1746. i != 1 ? "" : "/.ffmpeg", preset_name);
  1747. f = fopen(filename, "r");
  1748. if (!f && codec_name) {
  1749. snprintf(filename, filename_size,
  1750. "%s%s/%s-%s.ffpreset",
  1751. base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
  1752. preset_name);
  1753. f = fopen(filename, "r");
  1754. }
  1755. }
  1756. }
  1757. return f;
  1758. }
  1759. int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
  1760. {
  1761. int ret = avformat_match_stream_specifier(s, st, spec);
  1762. if (ret < 0)
  1763. av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
  1764. return ret;
  1765. }
  1766. AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
  1767. AVFormatContext *s, AVStream *st, AVCodec *codec)
  1768. {
  1769. AVDictionary *ret = NULL;
  1770. AVDictionaryEntry *t = NULL;
  1771. int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
  1772. : AV_OPT_FLAG_DECODING_PARAM;
  1773. char prefix = 0;
  1774. const AVClass *cc = avcodec_get_class();
  1775. if (!codec)
  1776. codec = s->oformat ? avcodec_find_encoder(codec_id)
  1777. : avcodec_find_decoder(codec_id);
  1778. switch (st->codecpar->codec_type) {
  1779. case AVMEDIA_TYPE_VIDEO:
  1780. prefix = 'v';
  1781. flags |= AV_OPT_FLAG_VIDEO_PARAM;
  1782. break;
  1783. case AVMEDIA_TYPE_AUDIO:
  1784. prefix = 'a';
  1785. flags |= AV_OPT_FLAG_AUDIO_PARAM;
  1786. break;
  1787. case AVMEDIA_TYPE_SUBTITLE:
  1788. prefix = 's';
  1789. flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
  1790. break;
  1791. }
  1792. while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
  1793. char *p = strchr(t->key, ':');
  1794. /* check stream specification in opt name */
  1795. if (p)
  1796. switch (check_stream_specifier(s, st, p + 1)) {
  1797. case 1: *p = 0; break;
  1798. case 0: continue;
  1799. default: exit_program(1);
  1800. }
  1801. if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
  1802. !codec ||
  1803. (codec->priv_class &&
  1804. av_opt_find(&codec->priv_class, t->key, NULL, flags,
  1805. AV_OPT_SEARCH_FAKE_OBJ)))
  1806. av_dict_set(&ret, t->key, t->value, 0);
  1807. else if (t->key[0] == prefix &&
  1808. av_opt_find(&cc, t->key + 1, NULL, flags,
  1809. AV_OPT_SEARCH_FAKE_OBJ))
  1810. av_dict_set(&ret, t->key + 1, t->value, 0);
  1811. if (p)
  1812. *p = ':';
  1813. }
  1814. return ret;
  1815. }
  1816. AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
  1817. AVDictionary *codec_opts)
  1818. {
  1819. int i;
  1820. AVDictionary **opts;
  1821. if (!s->nb_streams)
  1822. return NULL;
  1823. opts = av_mallocz_array(s->nb_streams, sizeof(*opts));
  1824. if (!opts) {
  1825. av_log(NULL, AV_LOG_ERROR,
  1826. "Could not alloc memory for stream options.\n");
  1827. return NULL;
  1828. }
  1829. for (i = 0; i < s->nb_streams; i++)
  1830. opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id,
  1831. s, s->streams[i], NULL);
  1832. return opts;
  1833. }
  1834. void *grow_array(void *array, int elem_size, int *size, int new_size)
  1835. {
  1836. if (new_size >= INT_MAX / elem_size) {
  1837. av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
  1838. exit_program(1);
  1839. }
  1840. if (*size < new_size) {
  1841. uint8_t *tmp = av_realloc_array(array, new_size, elem_size);
  1842. if (!tmp) {
  1843. av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
  1844. exit_program(1);
  1845. }
  1846. memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
  1847. *size = new_size;
  1848. return tmp;
  1849. }
  1850. return array;
  1851. }
  1852. double get_rotation(AVStream *st)
  1853. {
  1854. uint8_t* displaymatrix = av_stream_get_side_data(st,
  1855. AV_PKT_DATA_DISPLAYMATRIX, NULL);
  1856. double theta = 0;
  1857. if (displaymatrix)
  1858. theta = -av_display_rotation_get((int32_t*) displaymatrix);
  1859. theta -= 360*floor(theta/360 + 0.9/360);
  1860. if (fabs(theta - 90*round(theta/90)) > 2)
  1861. av_log(NULL, AV_LOG_WARNING, "Odd rotation angle.\n"
  1862. "If you want to help, upload a sample "
  1863. "of this file to ftp://upload.ffmpeg.org/incoming/ "
  1864. "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)");
  1865. return theta;
  1866. }
  1867. #if CONFIG_AVDEVICE
  1868. static int print_device_sources(AVInputFormat *fmt, AVDictionary *opts)
  1869. {
  1870. int ret, i;
  1871. AVDeviceInfoList *device_list = NULL;
  1872. if (!fmt || !fmt->priv_class || !AV_IS_INPUT_DEVICE(fmt->priv_class->category))
  1873. return AVERROR(EINVAL);
  1874. printf("Auto-detected sources for %s:\n", fmt->name);
  1875. if (!fmt->get_device_list) {
  1876. ret = AVERROR(ENOSYS);
  1877. printf("Cannot list sources. Not implemented.\n");
  1878. goto fail;
  1879. }
  1880. if ((ret = avdevice_list_input_sources(fmt, NULL, opts, &device_list)) < 0) {
  1881. printf("Cannot list sources.\n");
  1882. goto fail;
  1883. }
  1884. for (i = 0; i < device_list->nb_devices; i++) {
  1885. printf("%s %s [%s]\n", device_list->default_device == i ? "*" : " ",
  1886. device_list->devices[i]->device_name, device_list->devices[i]->device_description);
  1887. }
  1888. fail:
  1889. avdevice_free_list_devices(&device_list);
  1890. return ret;
  1891. }
  1892. static int print_device_sinks(AVOutputFormat *fmt, AVDictionary *opts)
  1893. {
  1894. int ret, i;
  1895. AVDeviceInfoList *device_list = NULL;
  1896. if (!fmt || !fmt->priv_class || !AV_IS_OUTPUT_DEVICE(fmt->priv_class->category))
  1897. return AVERROR(EINVAL);
  1898. printf("Auto-detected sinks for %s:\n", fmt->name);
  1899. if (!fmt->get_device_list) {
  1900. ret = AVERROR(ENOSYS);
  1901. printf("Cannot list sinks. Not implemented.\n");
  1902. goto fail;
  1903. }
  1904. if ((ret = avdevice_list_output_sinks(fmt, NULL, opts, &device_list)) < 0) {
  1905. printf("Cannot list sinks.\n");
  1906. goto fail;
  1907. }
  1908. for (i = 0; i < device_list->nb_devices; i++) {
  1909. printf("%s %s [%s]\n", device_list->default_device == i ? "*" : " ",
  1910. device_list->devices[i]->device_name, device_list->devices[i]->device_description);
  1911. }
  1912. fail:
  1913. avdevice_free_list_devices(&device_list);
  1914. return ret;
  1915. }
  1916. static int show_sinks_sources_parse_arg(const char *arg, char **dev, AVDictionary **opts)
  1917. {
  1918. int ret;
  1919. if (arg) {
  1920. char *opts_str = NULL;
  1921. av_assert0(dev && opts);
  1922. *dev = av_strdup(arg);
  1923. if (!*dev)
  1924. return AVERROR(ENOMEM);
  1925. if ((opts_str = strchr(*dev, ','))) {
  1926. *(opts_str++) = '\0';
  1927. if (opts_str[0] && ((ret = av_dict_parse_string(opts, opts_str, "=", ":", 0)) < 0)) {
  1928. av_freep(dev);
  1929. return ret;
  1930. }
  1931. }
  1932. } else
  1933. printf("\nDevice name is not provided.\n"
  1934. "You can pass devicename[,opt1=val1[,opt2=val2...]] as an argument.\n\n");
  1935. return 0;
  1936. }
  1937. int show_sources(void *optctx, const char *opt, const char *arg)
  1938. {
  1939. AVInputFormat *fmt = NULL;
  1940. char *dev = NULL;
  1941. AVDictionary *opts = NULL;
  1942. int ret = 0;
  1943. int error_level = av_log_get_level();
  1944. av_log_set_level(AV_LOG_ERROR);
  1945. if ((ret = show_sinks_sources_parse_arg(arg, &dev, &opts)) < 0)
  1946. goto fail;
  1947. do {
  1948. fmt = av_input_audio_device_next(fmt);
  1949. if (fmt) {
  1950. if (!strcmp(fmt->name, "lavfi"))
  1951. continue; //it's pointless to probe lavfi
  1952. if (dev && !av_match_name(dev, fmt->name))
  1953. continue;
  1954. print_device_sources(fmt, opts);
  1955. }
  1956. } while (fmt);
  1957. do {
  1958. fmt = av_input_video_device_next(fmt);
  1959. if (fmt) {
  1960. if (dev && !av_match_name(dev, fmt->name))
  1961. continue;
  1962. print_device_sources(fmt, opts);
  1963. }
  1964. } while (fmt);
  1965. fail:
  1966. av_dict_free(&opts);
  1967. av_free(dev);
  1968. av_log_set_level(error_level);
  1969. return ret;
  1970. }
  1971. int show_sinks(void *optctx, const char *opt, const char *arg)
  1972. {
  1973. AVOutputFormat *fmt = NULL;
  1974. char *dev = NULL;
  1975. AVDictionary *opts = NULL;
  1976. int ret = 0;
  1977. int error_level = av_log_get_level();
  1978. av_log_set_level(AV_LOG_ERROR);
  1979. if ((ret = show_sinks_sources_parse_arg(arg, &dev, &opts)) < 0)
  1980. goto fail;
  1981. do {
  1982. fmt = av_output_audio_device_next(fmt);
  1983. if (fmt) {
  1984. if (dev && !av_match_name(dev, fmt->name))
  1985. continue;
  1986. print_device_sinks(fmt, opts);
  1987. }
  1988. } while (fmt);
  1989. do {
  1990. fmt = av_output_video_device_next(fmt);
  1991. if (fmt) {
  1992. if (dev && !av_match_name(dev, fmt->name))
  1993. continue;
  1994. print_device_sinks(fmt, opts);
  1995. }
  1996. } while (fmt);
  1997. fail:
  1998. av_dict_free(&opts);
  1999. av_free(dev);
  2000. av_log_set_level(error_level);
  2001. return ret;
  2002. }
  2003. #endif