cmdutils.c 69 KB

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