cmdutils.c 70 KB

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