cmdutils.c 62 KB

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