cmdutils.c 62 KB

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