cmdutils.c 65 KB

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