cmdutils.c 72 KB

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