cmdutils.c 63 KB

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