cmdutils.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231
  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. char *str;
  257. dstcount = (int *)(so + 1);
  258. *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
  259. str = av_strdup(p ? p + 1 : "");
  260. if (!str)
  261. return AVERROR(ENOMEM);
  262. (*so)[*dstcount - 1].specifier = str;
  263. dst = &(*so)[*dstcount - 1].u;
  264. }
  265. if (po->flags & OPT_STRING) {
  266. char *str;
  267. str = av_strdup(arg);
  268. av_freep(dst);
  269. if (!str)
  270. return AVERROR(ENOMEM);
  271. *(char **)dst = str;
  272. } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
  273. *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
  274. } else if (po->flags & OPT_INT64) {
  275. *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
  276. } else if (po->flags & OPT_TIME) {
  277. *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
  278. } else if (po->flags & OPT_FLOAT) {
  279. *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
  280. } else if (po->flags & OPT_DOUBLE) {
  281. *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
  282. } else if (po->u.func_arg) {
  283. int ret = po->u.func_arg(optctx, opt, arg);
  284. if (ret < 0) {
  285. av_log(NULL, AV_LOG_ERROR,
  286. "Failed to set value '%s' for option '%s': %s\n",
  287. arg, opt, av_err2str(ret));
  288. return ret;
  289. }
  290. }
  291. if (po->flags & OPT_EXIT)
  292. exit_program(0);
  293. return 0;
  294. }
  295. int parse_option(void *optctx, const char *opt, const char *arg,
  296. const OptionDef *options)
  297. {
  298. const OptionDef *po;
  299. int ret;
  300. po = find_option(options, opt);
  301. if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
  302. /* handle 'no' bool option */
  303. po = find_option(options, opt + 2);
  304. if ((po->name && (po->flags & OPT_BOOL)))
  305. arg = "0";
  306. } else if (po->flags & OPT_BOOL)
  307. arg = "1";
  308. if (!po->name)
  309. po = find_option(options, "default");
  310. if (!po->name) {
  311. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
  312. return AVERROR(EINVAL);
  313. }
  314. if (po->flags & HAS_ARG && !arg) {
  315. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
  316. return AVERROR(EINVAL);
  317. }
  318. ret = write_option(optctx, po, opt, arg);
  319. if (ret < 0)
  320. return ret;
  321. return !!(po->flags & HAS_ARG);
  322. }
  323. void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
  324. void (*parse_arg_function)(void *, const char*))
  325. {
  326. const char *opt;
  327. int optindex, handleoptions = 1, ret;
  328. /* perform system-dependent conversions for arguments list */
  329. prepare_app_arguments(&argc, &argv);
  330. /* parse options */
  331. optindex = 1;
  332. while (optindex < argc) {
  333. opt = argv[optindex++];
  334. if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
  335. if (opt[1] == '-' && opt[2] == '\0') {
  336. handleoptions = 0;
  337. continue;
  338. }
  339. opt++;
  340. if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
  341. exit_program(1);
  342. optindex += ret;
  343. } else {
  344. if (parse_arg_function)
  345. parse_arg_function(optctx, opt);
  346. }
  347. }
  348. }
  349. int parse_optgroup(void *optctx, OptionGroup *g)
  350. {
  351. int i, ret;
  352. av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
  353. g->group_def->name, g->arg);
  354. for (i = 0; i < g->nb_opts; i++) {
  355. Option *o = &g->opts[i];
  356. if (g->group_def->flags &&
  357. !(g->group_def->flags & o->opt->flags)) {
  358. av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
  359. "%s %s -- you are trying to apply an input option to an "
  360. "output file or vice versa. Move this option before the "
  361. "file it belongs to.\n", o->key, o->opt->help,
  362. g->group_def->name, g->arg);
  363. return AVERROR(EINVAL);
  364. }
  365. av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
  366. o->key, o->opt->help, o->val);
  367. ret = write_option(optctx, o->opt, o->key, o->val);
  368. if (ret < 0)
  369. return ret;
  370. }
  371. av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
  372. return 0;
  373. }
  374. int locate_option(int argc, char **argv, const OptionDef *options,
  375. const char *optname)
  376. {
  377. const OptionDef *po;
  378. int i;
  379. for (i = 1; i < argc; i++) {
  380. const char *cur_opt = argv[i];
  381. if (*cur_opt++ != '-')
  382. continue;
  383. po = find_option(options, cur_opt);
  384. if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
  385. po = find_option(options, cur_opt + 2);
  386. if ((!po->name && !strcmp(cur_opt, optname)) ||
  387. (po->name && !strcmp(optname, po->name)))
  388. return i;
  389. if (!po->name || po->flags & HAS_ARG)
  390. i++;
  391. }
  392. return 0;
  393. }
  394. static void dump_argument(const char *a)
  395. {
  396. const unsigned char *p;
  397. for (p = a; *p; p++)
  398. if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
  399. *p == '_' || (*p >= 'a' && *p <= 'z')))
  400. break;
  401. if (!*p) {
  402. fputs(a, report_file);
  403. return;
  404. }
  405. fputc('"', report_file);
  406. for (p = a; *p; p++) {
  407. if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
  408. fprintf(report_file, "\\%c", *p);
  409. else if (*p < ' ' || *p > '~')
  410. fprintf(report_file, "\\x%02x", *p);
  411. else
  412. fputc(*p, report_file);
  413. }
  414. fputc('"', report_file);
  415. }
  416. void parse_loglevel(int argc, char **argv, const OptionDef *options)
  417. {
  418. int idx = locate_option(argc, argv, options, "loglevel");
  419. const char *env;
  420. if (!idx)
  421. idx = locate_option(argc, argv, options, "v");
  422. if (idx && argv[idx + 1])
  423. opt_loglevel(NULL, "loglevel", argv[idx + 1]);
  424. idx = locate_option(argc, argv, options, "report");
  425. if ((env = getenv("FFREPORT")) || idx) {
  426. init_report(env);
  427. if (report_file) {
  428. int i;
  429. fprintf(report_file, "Command line:\n");
  430. for (i = 0; i < argc; i++) {
  431. dump_argument(argv[i]);
  432. fputc(i < argc - 1 ? ' ' : '\n', report_file);
  433. }
  434. fflush(report_file);
  435. }
  436. }
  437. idx = locate_option(argc, argv, options, "hide_banner");
  438. if (idx)
  439. hide_banner = 1;
  440. }
  441. static const AVOption *opt_find(void *obj, const char *name, const char *unit,
  442. int opt_flags, int search_flags)
  443. {
  444. const AVOption *o = av_opt_find(obj, name, unit, opt_flags, search_flags);
  445. if(o && !o->flags)
  446. return NULL;
  447. return o;
  448. }
  449. #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
  450. int opt_default(void *optctx, const char *opt, const char *arg)
  451. {
  452. const AVOption *o;
  453. int consumed = 0;
  454. char opt_stripped[128];
  455. const char *p;
  456. const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
  457. #if CONFIG_AVRESAMPLE
  458. const AVClass *rc = avresample_get_class();
  459. #endif
  460. const AVClass *sc, *swr_class;
  461. if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug"))
  462. av_log_set_level(AV_LOG_DEBUG);
  463. if (!(p = strchr(opt, ':')))
  464. p = opt + strlen(opt);
  465. av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
  466. if ((o = opt_find(&cc, opt_stripped, NULL, 0,
  467. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
  468. ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
  469. (o = opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
  470. av_dict_set(&codec_opts, opt, arg, FLAGS);
  471. consumed = 1;
  472. }
  473. if ((o = opt_find(&fc, opt, NULL, 0,
  474. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  475. av_dict_set(&format_opts, opt, arg, FLAGS);
  476. if (consumed)
  477. av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt);
  478. consumed = 1;
  479. }
  480. #if CONFIG_SWSCALE
  481. sc = sws_get_class();
  482. if (!consumed && opt_find(&sc, opt, NULL, 0,
  483. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
  484. // XXX we only support sws_flags, not arbitrary sws options
  485. int ret = av_opt_set(sws_opts, opt, arg, 0);
  486. if (ret < 0) {
  487. av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
  488. return ret;
  489. }
  490. consumed = 1;
  491. }
  492. #else
  493. if (!consumed && !strcmp(opt, "sws_flags")) {
  494. av_log(NULL, AV_LOG_WARNING, "Ignoring %s %s, due to disabled swscale\n", opt, arg);
  495. consumed = 1;
  496. }
  497. #endif
  498. #if CONFIG_SWRESAMPLE
  499. swr_class = swr_get_class();
  500. if (!consumed && (o=opt_find(&swr_class, opt, NULL, 0,
  501. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  502. struct SwrContext *swr = swr_alloc();
  503. int ret = av_opt_set(swr, opt, arg, 0);
  504. swr_free(&swr);
  505. if (ret < 0) {
  506. av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
  507. return ret;
  508. }
  509. av_dict_set(&swr_opts, opt, arg, FLAGS);
  510. consumed = 1;
  511. }
  512. #endif
  513. #if CONFIG_AVRESAMPLE
  514. if ((o=opt_find(&rc, opt, NULL, 0,
  515. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  516. av_dict_set(&resample_opts, opt, arg, FLAGS);
  517. consumed = 1;
  518. }
  519. #endif
  520. if (consumed)
  521. return 0;
  522. return AVERROR_OPTION_NOT_FOUND;
  523. }
  524. /*
  525. * Check whether given option is a group separator.
  526. *
  527. * @return index of the group definition that matched or -1 if none
  528. */
  529. static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
  530. const char *opt)
  531. {
  532. int i;
  533. for (i = 0; i < nb_groups; i++) {
  534. const OptionGroupDef *p = &groups[i];
  535. if (p->sep && !strcmp(p->sep, opt))
  536. return i;
  537. }
  538. return -1;
  539. }
  540. /*
  541. * Finish parsing an option group.
  542. *
  543. * @param group_idx which group definition should this group belong to
  544. * @param arg argument of the group delimiting option
  545. */
  546. static void finish_group(OptionParseContext *octx, int group_idx,
  547. const char *arg)
  548. {
  549. OptionGroupList *l = &octx->groups[group_idx];
  550. OptionGroup *g;
  551. GROW_ARRAY(l->groups, l->nb_groups);
  552. g = &l->groups[l->nb_groups - 1];
  553. *g = octx->cur_group;
  554. g->arg = arg;
  555. g->group_def = l->group_def;
  556. #if CONFIG_SWSCALE
  557. g->sws_opts = sws_opts;
  558. #endif
  559. g->swr_opts = swr_opts;
  560. g->codec_opts = codec_opts;
  561. g->format_opts = format_opts;
  562. g->resample_opts = resample_opts;
  563. codec_opts = NULL;
  564. format_opts = NULL;
  565. resample_opts = NULL;
  566. #if CONFIG_SWSCALE
  567. sws_opts = NULL;
  568. #endif
  569. swr_opts = NULL;
  570. init_opts();
  571. memset(&octx->cur_group, 0, sizeof(octx->cur_group));
  572. }
  573. /*
  574. * Add an option instance to currently parsed group.
  575. */
  576. static void add_opt(OptionParseContext *octx, const OptionDef *opt,
  577. const char *key, const char *val)
  578. {
  579. int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
  580. OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
  581. GROW_ARRAY(g->opts, g->nb_opts);
  582. g->opts[g->nb_opts - 1].opt = opt;
  583. g->opts[g->nb_opts - 1].key = key;
  584. g->opts[g->nb_opts - 1].val = val;
  585. }
  586. static void init_parse_context(OptionParseContext *octx,
  587. const OptionGroupDef *groups, int nb_groups)
  588. {
  589. static const OptionGroupDef global_group = { "global" };
  590. int i;
  591. memset(octx, 0, sizeof(*octx));
  592. octx->nb_groups = nb_groups;
  593. octx->groups = av_mallocz_array(octx->nb_groups, sizeof(*octx->groups));
  594. if (!octx->groups)
  595. exit_program(1);
  596. for (i = 0; i < octx->nb_groups; i++)
  597. octx->groups[i].group_def = &groups[i];
  598. octx->global_opts.group_def = &global_group;
  599. octx->global_opts.arg = "";
  600. init_opts();
  601. }
  602. void uninit_parse_context(OptionParseContext *octx)
  603. {
  604. int i, j;
  605. for (i = 0; i < octx->nb_groups; i++) {
  606. OptionGroupList *l = &octx->groups[i];
  607. for (j = 0; j < l->nb_groups; j++) {
  608. av_freep(&l->groups[j].opts);
  609. av_dict_free(&l->groups[j].codec_opts);
  610. av_dict_free(&l->groups[j].format_opts);
  611. av_dict_free(&l->groups[j].resample_opts);
  612. #if CONFIG_SWSCALE
  613. sws_freeContext(l->groups[j].sws_opts);
  614. #endif
  615. av_dict_free(&l->groups[j].swr_opts);
  616. }
  617. av_freep(&l->groups);
  618. }
  619. av_freep(&octx->groups);
  620. av_freep(&octx->cur_group.opts);
  621. av_freep(&octx->global_opts.opts);
  622. uninit_opts();
  623. }
  624. int split_commandline(OptionParseContext *octx, int argc, char *argv[],
  625. const OptionDef *options,
  626. const OptionGroupDef *groups, int nb_groups)
  627. {
  628. int optindex = 1;
  629. int dashdash = -2;
  630. /* perform system-dependent conversions for arguments list */
  631. prepare_app_arguments(&argc, &argv);
  632. init_parse_context(octx, groups, nb_groups);
  633. av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
  634. while (optindex < argc) {
  635. const char *opt = argv[optindex++], *arg;
  636. const OptionDef *po;
  637. int ret;
  638. av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
  639. if (opt[0] == '-' && opt[1] == '-' && !opt[2]) {
  640. dashdash = optindex;
  641. continue;
  642. }
  643. /* unnamed group separators, e.g. output filename */
  644. if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
  645. finish_group(octx, 0, opt);
  646. av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
  647. continue;
  648. }
  649. opt++;
  650. #define GET_ARG(arg) \
  651. do { \
  652. arg = argv[optindex++]; \
  653. if (!arg) { \
  654. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
  655. return AVERROR(EINVAL); \
  656. } \
  657. } while (0)
  658. /* named group separators, e.g. -i */
  659. if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
  660. GET_ARG(arg);
  661. finish_group(octx, ret, arg);
  662. av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
  663. groups[ret].name, arg);
  664. continue;
  665. }
  666. /* normal options */
  667. po = find_option(options, opt);
  668. if (po->name) {
  669. if (po->flags & OPT_EXIT) {
  670. /* optional argument, e.g. -h */
  671. arg = argv[optindex++];
  672. } else if (po->flags & HAS_ARG) {
  673. GET_ARG(arg);
  674. } else {
  675. arg = "1";
  676. }
  677. add_opt(octx, po, opt, arg);
  678. av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
  679. "argument '%s'.\n", po->name, po->help, arg);
  680. continue;
  681. }
  682. /* AVOptions */
  683. if (argv[optindex]) {
  684. ret = opt_default(NULL, opt, argv[optindex]);
  685. if (ret >= 0) {
  686. av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
  687. "argument '%s'.\n", opt, argv[optindex]);
  688. optindex++;
  689. continue;
  690. } else if (ret != AVERROR_OPTION_NOT_FOUND) {
  691. av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
  692. "with argument '%s'.\n", opt, argv[optindex]);
  693. return ret;
  694. }
  695. }
  696. /* boolean -nofoo options */
  697. if (opt[0] == 'n' && opt[1] == 'o' &&
  698. (po = find_option(options, opt + 2)) &&
  699. po->name && po->flags & OPT_BOOL) {
  700. add_opt(octx, po, opt, "0");
  701. av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
  702. "argument 0.\n", po->name, po->help);
  703. continue;
  704. }
  705. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
  706. return AVERROR_OPTION_NOT_FOUND;
  707. }
  708. if (octx->cur_group.nb_opts || codec_opts || format_opts || resample_opts)
  709. av_log(NULL, AV_LOG_WARNING, "Trailing options were found on the "
  710. "commandline.\n");
  711. av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
  712. return 0;
  713. }
  714. int opt_cpuflags(void *optctx, const char *opt, const char *arg)
  715. {
  716. int ret;
  717. unsigned flags = av_get_cpu_flags();
  718. if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
  719. return ret;
  720. av_force_cpu_flags(flags);
  721. return 0;
  722. }
  723. int opt_loglevel(void *optctx, const char *opt, const char *arg)
  724. {
  725. const struct { const char *name; int level; } log_levels[] = {
  726. { "quiet" , AV_LOG_QUIET },
  727. { "panic" , AV_LOG_PANIC },
  728. { "fatal" , AV_LOG_FATAL },
  729. { "error" , AV_LOG_ERROR },
  730. { "warning", AV_LOG_WARNING },
  731. { "info" , AV_LOG_INFO },
  732. { "verbose", AV_LOG_VERBOSE },
  733. { "debug" , AV_LOG_DEBUG },
  734. };
  735. char *tail;
  736. int level;
  737. int flags;
  738. int i;
  739. flags = av_log_get_flags();
  740. tail = strstr(arg, "repeat");
  741. if (tail)
  742. flags &= ~AV_LOG_SKIP_REPEATED;
  743. else
  744. flags |= AV_LOG_SKIP_REPEATED;
  745. av_log_set_flags(flags);
  746. if (tail == arg)
  747. arg += 6 + (arg[6]=='+');
  748. if(tail && !*arg)
  749. return 0;
  750. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
  751. if (!strcmp(log_levels[i].name, arg)) {
  752. av_log_set_level(log_levels[i].level);
  753. return 0;
  754. }
  755. }
  756. level = strtol(arg, &tail, 10);
  757. if (*tail) {
  758. av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
  759. "Possible levels are numbers or:\n", arg);
  760. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
  761. av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
  762. exit_program(1);
  763. }
  764. av_log_set_level(level);
  765. return 0;
  766. }
  767. static void expand_filename_template(AVBPrint *bp, const char *template,
  768. struct tm *tm)
  769. {
  770. int c;
  771. while ((c = *(template++))) {
  772. if (c == '%') {
  773. if (!(c = *(template++)))
  774. break;
  775. switch (c) {
  776. case 'p':
  777. av_bprintf(bp, "%s", program_name);
  778. break;
  779. case 't':
  780. av_bprintf(bp, "%04d%02d%02d-%02d%02d%02d",
  781. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  782. tm->tm_hour, tm->tm_min, tm->tm_sec);
  783. break;
  784. case '%':
  785. av_bprint_chars(bp, c, 1);
  786. break;
  787. }
  788. } else {
  789. av_bprint_chars(bp, c, 1);
  790. }
  791. }
  792. }
  793. static int init_report(const char *env)
  794. {
  795. char *filename_template = NULL;
  796. char *key, *val;
  797. int ret, count = 0;
  798. time_t now;
  799. struct tm *tm;
  800. AVBPrint filename;
  801. if (report_file) /* already opened */
  802. return 0;
  803. time(&now);
  804. tm = localtime(&now);
  805. while (env && *env) {
  806. if ((ret = av_opt_get_key_value(&env, "=", ":", 0, &key, &val)) < 0) {
  807. if (count)
  808. av_log(NULL, AV_LOG_ERROR,
  809. "Failed to parse FFREPORT environment variable: %s\n",
  810. av_err2str(ret));
  811. break;
  812. }
  813. if (*env)
  814. env++;
  815. count++;
  816. if (!strcmp(key, "file")) {
  817. av_free(filename_template);
  818. filename_template = val;
  819. val = NULL;
  820. } else if (!strcmp(key, "level")) {
  821. char *tail;
  822. report_file_level = strtol(val, &tail, 10);
  823. if (*tail) {
  824. av_log(NULL, AV_LOG_FATAL, "Invalid report file level\n");
  825. exit_program(1);
  826. }
  827. } else {
  828. av_log(NULL, AV_LOG_ERROR, "Unknown key '%s' in FFREPORT\n", key);
  829. }
  830. av_free(val);
  831. av_free(key);
  832. }
  833. av_bprint_init(&filename, 0, 1);
  834. expand_filename_template(&filename,
  835. av_x_if_null(filename_template, "%p-%t.log"), tm);
  836. av_free(filename_template);
  837. if (!av_bprint_is_complete(&filename)) {
  838. av_log(NULL, AV_LOG_ERROR, "Out of memory building report file name\n");
  839. return AVERROR(ENOMEM);
  840. }
  841. report_file = fopen(filename.str, "w");
  842. if (!report_file) {
  843. int ret = AVERROR(errno);
  844. av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
  845. filename.str, strerror(errno));
  846. return ret;
  847. }
  848. av_log_set_callback(log_callback_report);
  849. av_log(NULL, AV_LOG_INFO,
  850. "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
  851. "Report written to \"%s\"\n",
  852. program_name,
  853. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  854. tm->tm_hour, tm->tm_min, tm->tm_sec,
  855. filename.str);
  856. av_bprint_finalize(&filename, NULL);
  857. return 0;
  858. }
  859. int opt_report(const char *opt)
  860. {
  861. return init_report(NULL);
  862. }
  863. int opt_max_alloc(void *optctx, const char *opt, const char *arg)
  864. {
  865. char *tail;
  866. size_t max;
  867. max = strtol(arg, &tail, 10);
  868. if (*tail) {
  869. av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
  870. exit_program(1);
  871. }
  872. av_max_alloc(max);
  873. return 0;
  874. }
  875. int opt_timelimit(void *optctx, const char *opt, const char *arg)
  876. {
  877. #if HAVE_SETRLIMIT
  878. int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
  879. struct rlimit rl = { lim, lim + 1 };
  880. if (setrlimit(RLIMIT_CPU, &rl))
  881. perror("setrlimit");
  882. #else
  883. av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
  884. #endif
  885. return 0;
  886. }
  887. void print_error(const char *filename, int err)
  888. {
  889. char errbuf[128];
  890. const char *errbuf_ptr = errbuf;
  891. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  892. errbuf_ptr = strerror(AVUNERROR(err));
  893. av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
  894. }
  895. static int warned_cfg = 0;
  896. #define INDENT 1
  897. #define SHOW_VERSION 2
  898. #define SHOW_CONFIG 4
  899. #define SHOW_COPYRIGHT 8
  900. #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
  901. if (CONFIG_##LIBNAME) { \
  902. const char *indent = flags & INDENT? " " : ""; \
  903. if (flags & SHOW_VERSION) { \
  904. unsigned int version = libname##_version(); \
  905. av_log(NULL, level, \
  906. "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
  907. indent, #libname, \
  908. LIB##LIBNAME##_VERSION_MAJOR, \
  909. LIB##LIBNAME##_VERSION_MINOR, \
  910. LIB##LIBNAME##_VERSION_MICRO, \
  911. version >> 16, version >> 8 & 0xff, version & 0xff); \
  912. } \
  913. if (flags & SHOW_CONFIG) { \
  914. const char *cfg = libname##_configuration(); \
  915. if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
  916. if (!warned_cfg) { \
  917. av_log(NULL, level, \
  918. "%sWARNING: library configuration mismatch\n", \
  919. indent); \
  920. warned_cfg = 1; \
  921. } \
  922. av_log(NULL, level, "%s%-11s configuration: %s\n", \
  923. indent, #libname, cfg); \
  924. } \
  925. } \
  926. } \
  927. static void print_all_libs_info(int flags, int level)
  928. {
  929. PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
  930. PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
  931. PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
  932. PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
  933. PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
  934. PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
  935. PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
  936. PRINT_LIB_INFO(swresample,SWRESAMPLE, flags, level);
  937. PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
  938. }
  939. static void print_program_info(int flags, int level)
  940. {
  941. const char *indent = flags & INDENT? " " : "";
  942. av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
  943. if (flags & SHOW_COPYRIGHT)
  944. av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
  945. program_birth_year, CONFIG_THIS_YEAR);
  946. av_log(NULL, level, "\n");
  947. av_log(NULL, level, "%sbuilt with %s\n", indent, CC_IDENT);
  948. av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
  949. }
  950. static void print_buildconf(int flags, int level)
  951. {
  952. const char *indent = flags & INDENT ? " " : "";
  953. char str[] = { FFMPEG_CONFIGURATION };
  954. char *conflist, *remove_tilde, *splitconf;
  955. // Change all the ' --' strings to '~--' so that
  956. // they can be identified as tokens.
  957. while ((conflist = strstr(str, " --")) != NULL) {
  958. strncpy(conflist, "~--", 3);
  959. }
  960. // Compensate for the weirdness this would cause
  961. // when passing 'pkg-config --static'.
  962. while ((remove_tilde = strstr(str, "pkg-config~")) != NULL) {
  963. strncpy(remove_tilde, "pkg-config ", 11);
  964. }
  965. splitconf = strtok(str, "~");
  966. av_log(NULL, level, "\n%sconfiguration:\n", indent);
  967. while (splitconf != NULL) {
  968. av_log(NULL, level, "%s%s%s\n", indent, indent, splitconf);
  969. splitconf = strtok(NULL, "~");
  970. }
  971. }
  972. void show_banner(int argc, char **argv, const OptionDef *options)
  973. {
  974. int idx = locate_option(argc, argv, options, "version");
  975. if (hide_banner || idx)
  976. return;
  977. print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
  978. print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
  979. print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
  980. }
  981. int show_version(void *optctx, const char *opt, const char *arg)
  982. {
  983. av_log_set_callback(log_callback_help);
  984. print_program_info (SHOW_COPYRIGHT, AV_LOG_INFO);
  985. print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
  986. return 0;
  987. }
  988. int show_buildconf(void *optctx, const char *opt, const char *arg)
  989. {
  990. av_log_set_callback(log_callback_help);
  991. print_buildconf (INDENT|0, AV_LOG_INFO);
  992. return 0;
  993. }
  994. int show_license(void *optctx, const char *opt, const char *arg)
  995. {
  996. #if CONFIG_NONFREE
  997. printf(
  998. "This version of %s has nonfree parts compiled in.\n"
  999. "Therefore it is not legally redistributable.\n",
  1000. program_name );
  1001. #elif CONFIG_GPLV3
  1002. printf(
  1003. "%s is free software; you can redistribute it and/or modify\n"
  1004. "it under the terms of the GNU General Public License as published by\n"
  1005. "the Free Software Foundation; either version 3 of the License, or\n"
  1006. "(at your option) any later version.\n"
  1007. "\n"
  1008. "%s is distributed in the hope that it will be useful,\n"
  1009. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1010. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1011. "GNU General Public License for more details.\n"
  1012. "\n"
  1013. "You should have received a copy of the GNU General Public License\n"
  1014. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  1015. program_name, program_name, program_name );
  1016. #elif CONFIG_GPL
  1017. printf(
  1018. "%s is free software; you can redistribute it and/or modify\n"
  1019. "it under the terms of the GNU General Public License as published by\n"
  1020. "the Free Software Foundation; either version 2 of the License, or\n"
  1021. "(at your option) any later version.\n"
  1022. "\n"
  1023. "%s is distributed in the hope that it will be useful,\n"
  1024. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1025. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1026. "GNU General Public License for more details.\n"
  1027. "\n"
  1028. "You should have received a copy of the GNU General Public License\n"
  1029. "along with %s; if not, write to the Free Software\n"
  1030. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  1031. program_name, program_name, program_name );
  1032. #elif CONFIG_LGPLV3
  1033. printf(
  1034. "%s is free software; you can redistribute it and/or modify\n"
  1035. "it under the terms of the GNU Lesser General Public License as published by\n"
  1036. "the Free Software Foundation; either version 3 of the License, or\n"
  1037. "(at your option) any later version.\n"
  1038. "\n"
  1039. "%s is distributed in the hope that it will be useful,\n"
  1040. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1041. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1042. "GNU Lesser General Public License for more details.\n"
  1043. "\n"
  1044. "You should have received a copy of the GNU Lesser General Public License\n"
  1045. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  1046. program_name, program_name, program_name );
  1047. #else
  1048. printf(
  1049. "%s is free software; you can redistribute it and/or\n"
  1050. "modify it under the terms of the GNU Lesser General Public\n"
  1051. "License as published by the Free Software Foundation; either\n"
  1052. "version 2.1 of the License, or (at your option) any later version.\n"
  1053. "\n"
  1054. "%s is distributed in the hope that it will be useful,\n"
  1055. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1056. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  1057. "Lesser General Public License for more details.\n"
  1058. "\n"
  1059. "You should have received a copy of the GNU Lesser General Public\n"
  1060. "License along with %s; if not, write to the Free Software\n"
  1061. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  1062. program_name, program_name, program_name );
  1063. #endif
  1064. return 0;
  1065. }
  1066. static int is_device(const AVClass *avclass)
  1067. {
  1068. if (!avclass)
  1069. return 0;
  1070. return AV_IS_INPUT_DEVICE(avclass->category) || AV_IS_OUTPUT_DEVICE(avclass->category);
  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. if (!topic)
  1594. return AVERROR(ENOMEM);
  1595. par = strchr(topic, '=');
  1596. if (par)
  1597. *par++ = 0;
  1598. if (!*topic) {
  1599. show_help_default(topic, par);
  1600. } else if (!strcmp(topic, "decoder")) {
  1601. show_help_codec(par, 0);
  1602. } else if (!strcmp(topic, "encoder")) {
  1603. show_help_codec(par, 1);
  1604. } else if (!strcmp(topic, "demuxer")) {
  1605. show_help_demuxer(par);
  1606. } else if (!strcmp(topic, "muxer")) {
  1607. show_help_muxer(par);
  1608. #if CONFIG_AVFILTER
  1609. } else if (!strcmp(topic, "filter")) {
  1610. show_help_filter(par);
  1611. #endif
  1612. } else {
  1613. show_help_default(topic, par);
  1614. }
  1615. av_freep(&topic);
  1616. return 0;
  1617. }
  1618. int read_yesno(void)
  1619. {
  1620. int c = getchar();
  1621. int yesno = (av_toupper(c) == 'Y');
  1622. while (c != '\n' && c != EOF)
  1623. c = getchar();
  1624. return yesno;
  1625. }
  1626. int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
  1627. {
  1628. int64_t ret;
  1629. FILE *f = av_fopen_utf8(filename, "rb");
  1630. if (!f) {
  1631. ret = AVERROR(errno);
  1632. av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
  1633. strerror(errno));
  1634. return ret;
  1635. }
  1636. ret = fseek(f, 0, SEEK_END);
  1637. if (ret == -1) {
  1638. ret = AVERROR(errno);
  1639. goto out;
  1640. }
  1641. ret = ftell(f);
  1642. if (ret < 0) {
  1643. ret = AVERROR(errno);
  1644. goto out;
  1645. }
  1646. *size = ret;
  1647. ret = fseek(f, 0, SEEK_SET);
  1648. if (ret == -1) {
  1649. ret = AVERROR(errno);
  1650. goto out;
  1651. }
  1652. *bufptr = av_malloc(*size + 1);
  1653. if (!*bufptr) {
  1654. av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
  1655. ret = AVERROR(ENOMEM);
  1656. goto out;
  1657. }
  1658. ret = fread(*bufptr, 1, *size, f);
  1659. if (ret < *size) {
  1660. av_free(*bufptr);
  1661. if (ferror(f)) {
  1662. ret = AVERROR(errno);
  1663. av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
  1664. filename, strerror(errno));
  1665. } else
  1666. ret = AVERROR_EOF;
  1667. } else {
  1668. ret = 0;
  1669. (*bufptr)[(*size)++] = '\0';
  1670. }
  1671. out:
  1672. if (ret < 0)
  1673. av_log(NULL, AV_LOG_ERROR, "IO error: %s\n", av_err2str(ret));
  1674. fclose(f);
  1675. return ret;
  1676. }
  1677. FILE *get_preset_file(char *filename, size_t filename_size,
  1678. const char *preset_name, int is_path,
  1679. const char *codec_name)
  1680. {
  1681. FILE *f = NULL;
  1682. int i;
  1683. const char *base[3] = { getenv("FFMPEG_DATADIR"),
  1684. getenv("HOME"),
  1685. FFMPEG_DATADIR, };
  1686. if (is_path) {
  1687. av_strlcpy(filename, preset_name, filename_size);
  1688. f = fopen(filename, "r");
  1689. } else {
  1690. #ifdef _WIN32
  1691. char datadir[MAX_PATH], *ls;
  1692. base[2] = NULL;
  1693. if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
  1694. {
  1695. for (ls = datadir; ls < datadir + strlen(datadir); ls++)
  1696. if (*ls == '\\') *ls = '/';
  1697. if (ls = strrchr(datadir, '/'))
  1698. {
  1699. *ls = 0;
  1700. strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
  1701. base[2] = datadir;
  1702. }
  1703. }
  1704. #endif
  1705. for (i = 0; i < 3 && !f; i++) {
  1706. if (!base[i])
  1707. continue;
  1708. snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
  1709. i != 1 ? "" : "/.ffmpeg", preset_name);
  1710. f = fopen(filename, "r");
  1711. if (!f && codec_name) {
  1712. snprintf(filename, filename_size,
  1713. "%s%s/%s-%s.ffpreset",
  1714. base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
  1715. preset_name);
  1716. f = fopen(filename, "r");
  1717. }
  1718. }
  1719. }
  1720. return f;
  1721. }
  1722. int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
  1723. {
  1724. int ret = avformat_match_stream_specifier(s, st, spec);
  1725. if (ret < 0)
  1726. av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
  1727. return ret;
  1728. }
  1729. AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
  1730. AVFormatContext *s, AVStream *st, AVCodec *codec)
  1731. {
  1732. AVDictionary *ret = NULL;
  1733. AVDictionaryEntry *t = NULL;
  1734. int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
  1735. : AV_OPT_FLAG_DECODING_PARAM;
  1736. char prefix = 0;
  1737. const AVClass *cc = avcodec_get_class();
  1738. if (!codec)
  1739. codec = s->oformat ? avcodec_find_encoder(codec_id)
  1740. : avcodec_find_decoder(codec_id);
  1741. switch (st->codec->codec_type) {
  1742. case AVMEDIA_TYPE_VIDEO:
  1743. prefix = 'v';
  1744. flags |= AV_OPT_FLAG_VIDEO_PARAM;
  1745. break;
  1746. case AVMEDIA_TYPE_AUDIO:
  1747. prefix = 'a';
  1748. flags |= AV_OPT_FLAG_AUDIO_PARAM;
  1749. break;
  1750. case AVMEDIA_TYPE_SUBTITLE:
  1751. prefix = 's';
  1752. flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
  1753. break;
  1754. }
  1755. while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
  1756. char *p = strchr(t->key, ':');
  1757. /* check stream specification in opt name */
  1758. if (p)
  1759. switch (check_stream_specifier(s, st, p + 1)) {
  1760. case 1: *p = 0; break;
  1761. case 0: continue;
  1762. default: exit_program(1);
  1763. }
  1764. if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
  1765. !codec ||
  1766. (codec->priv_class &&
  1767. av_opt_find(&codec->priv_class, t->key, NULL, flags,
  1768. AV_OPT_SEARCH_FAKE_OBJ)))
  1769. av_dict_set(&ret, t->key, t->value, 0);
  1770. else if (t->key[0] == prefix &&
  1771. av_opt_find(&cc, t->key + 1, NULL, flags,
  1772. AV_OPT_SEARCH_FAKE_OBJ))
  1773. av_dict_set(&ret, t->key + 1, t->value, 0);
  1774. if (p)
  1775. *p = ':';
  1776. }
  1777. return ret;
  1778. }
  1779. AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
  1780. AVDictionary *codec_opts)
  1781. {
  1782. int i;
  1783. AVDictionary **opts;
  1784. if (!s->nb_streams)
  1785. return NULL;
  1786. opts = av_mallocz_array(s->nb_streams, sizeof(*opts));
  1787. if (!opts) {
  1788. av_log(NULL, AV_LOG_ERROR,
  1789. "Could not alloc memory for stream options.\n");
  1790. return NULL;
  1791. }
  1792. for (i = 0; i < s->nb_streams; i++)
  1793. opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
  1794. s, s->streams[i], NULL);
  1795. return opts;
  1796. }
  1797. void *grow_array(void *array, int elem_size, int *size, int new_size)
  1798. {
  1799. if (new_size >= INT_MAX / elem_size) {
  1800. av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
  1801. exit_program(1);
  1802. }
  1803. if (*size < new_size) {
  1804. uint8_t *tmp = av_realloc_array(array, new_size, elem_size);
  1805. if (!tmp) {
  1806. av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
  1807. exit_program(1);
  1808. }
  1809. memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
  1810. *size = new_size;
  1811. return tmp;
  1812. }
  1813. return array;
  1814. }
  1815. #if CONFIG_AVDEVICE
  1816. static int print_device_sources(AVInputFormat *fmt, AVDictionary *opts)
  1817. {
  1818. int ret, i;
  1819. AVDeviceInfoList *device_list = NULL;
  1820. if (!fmt || !fmt->priv_class || !AV_IS_INPUT_DEVICE(fmt->priv_class->category))
  1821. return AVERROR(EINVAL);
  1822. printf("Audo-detected sources for %s:\n", fmt->name);
  1823. if (!fmt->get_device_list) {
  1824. ret = AVERROR(ENOSYS);
  1825. printf("Cannot list sources. Not implemented.\n");
  1826. goto fail;
  1827. }
  1828. if ((ret = avdevice_list_input_sources(fmt, NULL, opts, &device_list)) < 0) {
  1829. printf("Cannot list sources.\n");
  1830. goto fail;
  1831. }
  1832. for (i = 0; i < device_list->nb_devices; i++) {
  1833. printf("%s %s [%s]\n", device_list->default_device == i ? "*" : " ",
  1834. device_list->devices[i]->device_name, device_list->devices[i]->device_description);
  1835. }
  1836. fail:
  1837. avdevice_free_list_devices(&device_list);
  1838. return ret;
  1839. }
  1840. static int print_device_sinks(AVOutputFormat *fmt, AVDictionary *opts)
  1841. {
  1842. int ret, i;
  1843. AVDeviceInfoList *device_list = NULL;
  1844. if (!fmt || !fmt->priv_class || !AV_IS_OUTPUT_DEVICE(fmt->priv_class->category))
  1845. return AVERROR(EINVAL);
  1846. printf("Audo-detected sinks for %s:\n", fmt->name);
  1847. if (!fmt->get_device_list) {
  1848. ret = AVERROR(ENOSYS);
  1849. printf("Cannot list sinks. Not implemented.\n");
  1850. goto fail;
  1851. }
  1852. if ((ret = avdevice_list_output_sinks(fmt, NULL, opts, &device_list)) < 0) {
  1853. printf("Cannot list sinks.\n");
  1854. goto fail;
  1855. }
  1856. for (i = 0; i < device_list->nb_devices; i++) {
  1857. printf("%s %s [%s]\n", device_list->default_device == i ? "*" : " ",
  1858. device_list->devices[i]->device_name, device_list->devices[i]->device_description);
  1859. }
  1860. fail:
  1861. avdevice_free_list_devices(&device_list);
  1862. return ret;
  1863. }
  1864. static int show_sinks_sources_parse_arg(const char *arg, char **dev, AVDictionary **opts)
  1865. {
  1866. int ret;
  1867. if (arg) {
  1868. char *opts_str = NULL;
  1869. av_assert0(dev && opts);
  1870. *dev = av_strdup(arg);
  1871. if (!*dev)
  1872. return AVERROR(ENOMEM);
  1873. if ((opts_str = strchr(*dev, ','))) {
  1874. *(opts_str++) = '\0';
  1875. if (opts_str[0] && ((ret = av_dict_parse_string(opts, opts_str, "=", ":", 0)) < 0)) {
  1876. av_freep(dev);
  1877. return ret;
  1878. }
  1879. }
  1880. } else
  1881. printf("\nDevice name is not provided.\n"
  1882. "You can pass devicename[,opt1=val1[,opt2=val2...]] as an argument.\n\n");
  1883. return 0;
  1884. }
  1885. int show_sources(void *optctx, const char *opt, const char *arg)
  1886. {
  1887. AVInputFormat *fmt = NULL;
  1888. char *dev = NULL;
  1889. AVDictionary *opts = NULL;
  1890. int ret = 0;
  1891. int error_level = av_log_get_level();
  1892. av_log_set_level(AV_LOG_ERROR);
  1893. if ((ret = show_sinks_sources_parse_arg(arg, &dev, &opts)) < 0)
  1894. goto fail;
  1895. do {
  1896. fmt = av_input_audio_device_next(fmt);
  1897. if (fmt) {
  1898. if (!strcmp(fmt->name, "lavfi"))
  1899. continue; //it's pointless to probe lavfi
  1900. if (dev && !av_match_name(dev, fmt->name))
  1901. continue;
  1902. print_device_sources(fmt, opts);
  1903. }
  1904. } while (fmt);
  1905. do {
  1906. fmt = av_input_video_device_next(fmt);
  1907. if (fmt) {
  1908. if (dev && !av_match_name(dev, fmt->name))
  1909. continue;
  1910. print_device_sources(fmt, opts);
  1911. }
  1912. } while (fmt);
  1913. fail:
  1914. av_dict_free(&opts);
  1915. av_free(dev);
  1916. av_log_set_level(error_level);
  1917. return ret;
  1918. }
  1919. int show_sinks(void *optctx, const char *opt, const char *arg)
  1920. {
  1921. AVOutputFormat *fmt = NULL;
  1922. char *dev = NULL;
  1923. AVDictionary *opts = NULL;
  1924. int ret = 0;
  1925. int error_level = av_log_get_level();
  1926. av_log_set_level(AV_LOG_ERROR);
  1927. if ((ret = show_sinks_sources_parse_arg(arg, &dev, &opts)) < 0)
  1928. goto fail;
  1929. do {
  1930. fmt = av_output_audio_device_next(fmt);
  1931. if (fmt) {
  1932. if (dev && !av_match_name(dev, fmt->name))
  1933. continue;
  1934. print_device_sinks(fmt, opts);
  1935. }
  1936. } while (fmt);
  1937. do {
  1938. fmt = av_output_video_device_next(fmt);
  1939. if (fmt) {
  1940. if (dev && !av_match_name(dev, fmt->name))
  1941. continue;
  1942. print_device_sinks(fmt, opts);
  1943. }
  1944. } while (fmt);
  1945. fail:
  1946. av_dict_free(&opts);
  1947. av_free(dev);
  1948. av_log_set_level(error_level);
  1949. return ret;
  1950. }
  1951. #endif