cmdutils.c 70 KB

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