cmdutils.c 71 KB

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