cmdutils.c 71 KB

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