cmdutils.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  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 = 2013;
  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. int 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. return 0;
  1342. }
  1343. int show_pix_fmts(void *optctx, const char *opt, const char *arg)
  1344. {
  1345. const AVPixFmtDescriptor *pix_desc = NULL;
  1346. printf("Pixel formats:\n"
  1347. "I.... = Supported Input format for conversion\n"
  1348. ".O... = Supported Output format for conversion\n"
  1349. "..H.. = Hardware accelerated format\n"
  1350. "...P. = Paletted format\n"
  1351. "....B = Bitstream format\n"
  1352. "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
  1353. "-----\n");
  1354. #if !CONFIG_SWSCALE
  1355. # define sws_isSupportedInput(x) 0
  1356. # define sws_isSupportedOutput(x) 0
  1357. #endif
  1358. while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
  1359. enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
  1360. printf("%c%c%c%c%c %-16s %d %2d\n",
  1361. sws_isSupportedInput (pix_fmt) ? 'I' : '.',
  1362. sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
  1363. pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
  1364. pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
  1365. pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
  1366. pix_desc->name,
  1367. pix_desc->nb_components,
  1368. av_get_bits_per_pixel(pix_desc));
  1369. }
  1370. return 0;
  1371. }
  1372. int show_layouts(void *optctx, const char *opt, const char *arg)
  1373. {
  1374. int i = 0;
  1375. uint64_t layout, j;
  1376. const char *name, *descr;
  1377. printf("Individual channels:\n"
  1378. "NAME DESCRIPTION\n");
  1379. for (i = 0; i < 63; i++) {
  1380. name = av_get_channel_name((uint64_t)1 << i);
  1381. if (!name)
  1382. continue;
  1383. descr = av_get_channel_description((uint64_t)1 << i);
  1384. printf("%-12s%s\n", name, descr);
  1385. }
  1386. printf("\nStandard channel layouts:\n"
  1387. "NAME DECOMPOSITION\n");
  1388. for (i = 0; !av_get_standard_channel_layout(i, &layout, &name); i++) {
  1389. if (name) {
  1390. printf("%-12s", name);
  1391. for (j = 1; j; j <<= 1)
  1392. if ((layout & j))
  1393. printf("%s%s", (layout & (j - 1)) ? "+" : "", av_get_channel_name(j));
  1394. printf("\n");
  1395. }
  1396. }
  1397. return 0;
  1398. }
  1399. int show_sample_fmts(void *optctx, const char *opt, const char *arg)
  1400. {
  1401. int i;
  1402. char fmt_str[128];
  1403. for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
  1404. printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
  1405. return 0;
  1406. }
  1407. static void show_help_codec(const char *name, int encoder)
  1408. {
  1409. const AVCodecDescriptor *desc;
  1410. const AVCodec *codec;
  1411. if (!name) {
  1412. av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
  1413. return;
  1414. }
  1415. codec = encoder ? avcodec_find_encoder_by_name(name) :
  1416. avcodec_find_decoder_by_name(name);
  1417. if (codec)
  1418. print_codec(codec);
  1419. else if ((desc = avcodec_descriptor_get_by_name(name))) {
  1420. int printed = 0;
  1421. while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
  1422. printed = 1;
  1423. print_codec(codec);
  1424. }
  1425. if (!printed) {
  1426. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
  1427. "but no %s for it are available. FFmpeg might need to be "
  1428. "recompiled with additional external libraries.\n",
  1429. name, encoder ? "encoders" : "decoders");
  1430. }
  1431. } else {
  1432. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
  1433. name);
  1434. }
  1435. }
  1436. static void show_help_demuxer(const char *name)
  1437. {
  1438. const AVInputFormat *fmt = av_find_input_format(name);
  1439. if (!fmt) {
  1440. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1441. return;
  1442. }
  1443. printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
  1444. if (fmt->extensions)
  1445. printf(" Common extensions: %s.\n", fmt->extensions);
  1446. if (fmt->priv_class)
  1447. show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
  1448. }
  1449. static void show_help_muxer(const char *name)
  1450. {
  1451. const AVCodecDescriptor *desc;
  1452. const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
  1453. if (!fmt) {
  1454. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1455. return;
  1456. }
  1457. printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
  1458. if (fmt->extensions)
  1459. printf(" Common extensions: %s.\n", fmt->extensions);
  1460. if (fmt->mime_type)
  1461. printf(" Mime type: %s.\n", fmt->mime_type);
  1462. if (fmt->video_codec != AV_CODEC_ID_NONE &&
  1463. (desc = avcodec_descriptor_get(fmt->video_codec))) {
  1464. printf(" Default video codec: %s.\n", desc->name);
  1465. }
  1466. if (fmt->audio_codec != AV_CODEC_ID_NONE &&
  1467. (desc = avcodec_descriptor_get(fmt->audio_codec))) {
  1468. printf(" Default audio codec: %s.\n", desc->name);
  1469. }
  1470. if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
  1471. (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
  1472. printf(" Default subtitle codec: %s.\n", desc->name);
  1473. }
  1474. if (fmt->priv_class)
  1475. show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
  1476. }
  1477. #if CONFIG_AVFILTER
  1478. static void show_help_filter(const char *name)
  1479. {
  1480. #if CONFIG_AVFILTER
  1481. const AVFilter *f = avfilter_get_by_name(name);
  1482. int i, count;
  1483. if (!name) {
  1484. av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
  1485. return;
  1486. } else if (!f) {
  1487. av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
  1488. return;
  1489. }
  1490. printf("Filter %s\n", f->name);
  1491. if (f->description)
  1492. printf(" %s\n", f->description);
  1493. if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
  1494. printf(" slice threading supported\n");
  1495. printf(" Inputs:\n");
  1496. count = avfilter_pad_count(f->inputs);
  1497. for (i = 0; i < count; i++) {
  1498. printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
  1499. media_type_string(avfilter_pad_get_type(f->inputs, i)));
  1500. }
  1501. if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
  1502. printf(" dynamic (depending on the options)\n");
  1503. else if (!count)
  1504. printf(" none (source filter)\n");
  1505. printf(" Outputs:\n");
  1506. count = avfilter_pad_count(f->outputs);
  1507. for (i = 0; i < count; i++) {
  1508. printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
  1509. media_type_string(avfilter_pad_get_type(f->outputs, i)));
  1510. }
  1511. if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
  1512. printf(" dynamic (depending on the options)\n");
  1513. else if (!count)
  1514. printf(" none (sink filter)\n");
  1515. if (f->priv_class)
  1516. show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM |
  1517. AV_OPT_FLAG_AUDIO_PARAM);
  1518. if (f->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)
  1519. printf("This filter has support for timeline through the 'enable' option.\n");
  1520. #else
  1521. av_log(NULL, AV_LOG_ERROR, "Build without libavfilter; "
  1522. "can not to satisfy request\n");
  1523. #endif
  1524. }
  1525. #endif
  1526. int show_help(void *optctx, const char *opt, const char *arg)
  1527. {
  1528. char *topic, *par;
  1529. av_log_set_callback(log_callback_help);
  1530. topic = av_strdup(arg ? arg : "");
  1531. par = strchr(topic, '=');
  1532. if (par)
  1533. *par++ = 0;
  1534. if (!*topic) {
  1535. show_help_default(topic, par);
  1536. } else if (!strcmp(topic, "decoder")) {
  1537. show_help_codec(par, 0);
  1538. } else if (!strcmp(topic, "encoder")) {
  1539. show_help_codec(par, 1);
  1540. } else if (!strcmp(topic, "demuxer")) {
  1541. show_help_demuxer(par);
  1542. } else if (!strcmp(topic, "muxer")) {
  1543. show_help_muxer(par);
  1544. #if CONFIG_AVFILTER
  1545. } else if (!strcmp(topic, "filter")) {
  1546. show_help_filter(par);
  1547. #endif
  1548. } else {
  1549. show_help_default(topic, par);
  1550. }
  1551. av_freep(&topic);
  1552. return 0;
  1553. }
  1554. int read_yesno(void)
  1555. {
  1556. int c = getchar();
  1557. int yesno = (av_toupper(c) == 'Y');
  1558. while (c != '\n' && c != EOF)
  1559. c = getchar();
  1560. return yesno;
  1561. }
  1562. int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
  1563. {
  1564. int ret;
  1565. FILE *f = fopen(filename, "rb");
  1566. if (!f) {
  1567. av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
  1568. strerror(errno));
  1569. return AVERROR(errno);
  1570. }
  1571. fseek(f, 0, SEEK_END);
  1572. *size = ftell(f);
  1573. fseek(f, 0, SEEK_SET);
  1574. if (*size == (size_t)-1) {
  1575. av_log(NULL, AV_LOG_ERROR, "IO error: %s\n", strerror(errno));
  1576. fclose(f);
  1577. return AVERROR(errno);
  1578. }
  1579. *bufptr = av_malloc(*size + 1);
  1580. if (!*bufptr) {
  1581. av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
  1582. fclose(f);
  1583. return AVERROR(ENOMEM);
  1584. }
  1585. ret = fread(*bufptr, 1, *size, f);
  1586. if (ret < *size) {
  1587. av_free(*bufptr);
  1588. if (ferror(f)) {
  1589. av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
  1590. filename, strerror(errno));
  1591. ret = AVERROR(errno);
  1592. } else
  1593. ret = AVERROR_EOF;
  1594. } else {
  1595. ret = 0;
  1596. (*bufptr)[(*size)++] = '\0';
  1597. }
  1598. fclose(f);
  1599. return ret;
  1600. }
  1601. FILE *get_preset_file(char *filename, size_t filename_size,
  1602. const char *preset_name, int is_path,
  1603. const char *codec_name)
  1604. {
  1605. FILE *f = NULL;
  1606. int i;
  1607. const char *base[3] = { getenv("FFMPEG_DATADIR"),
  1608. getenv("HOME"),
  1609. FFMPEG_DATADIR, };
  1610. if (is_path) {
  1611. av_strlcpy(filename, preset_name, filename_size);
  1612. f = fopen(filename, "r");
  1613. } else {
  1614. #ifdef _WIN32
  1615. char datadir[MAX_PATH], *ls;
  1616. base[2] = NULL;
  1617. if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
  1618. {
  1619. for (ls = datadir; ls < datadir + strlen(datadir); ls++)
  1620. if (*ls == '\\') *ls = '/';
  1621. if (ls = strrchr(datadir, '/'))
  1622. {
  1623. *ls = 0;
  1624. strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
  1625. base[2] = datadir;
  1626. }
  1627. }
  1628. #endif
  1629. for (i = 0; i < 3 && !f; i++) {
  1630. if (!base[i])
  1631. continue;
  1632. snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
  1633. i != 1 ? "" : "/.ffmpeg", preset_name);
  1634. f = fopen(filename, "r");
  1635. if (!f && codec_name) {
  1636. snprintf(filename, filename_size,
  1637. "%s%s/%s-%s.ffpreset",
  1638. base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
  1639. preset_name);
  1640. f = fopen(filename, "r");
  1641. }
  1642. }
  1643. }
  1644. return f;
  1645. }
  1646. int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
  1647. {
  1648. int ret = avformat_match_stream_specifier(s, st, spec);
  1649. if (ret < 0)
  1650. av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
  1651. return ret;
  1652. }
  1653. AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
  1654. AVFormatContext *s, AVStream *st, AVCodec *codec)
  1655. {
  1656. AVDictionary *ret = NULL;
  1657. AVDictionaryEntry *t = NULL;
  1658. int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
  1659. : AV_OPT_FLAG_DECODING_PARAM;
  1660. char prefix = 0;
  1661. const AVClass *cc = avcodec_get_class();
  1662. if (!codec)
  1663. codec = s->oformat ? avcodec_find_encoder(codec_id)
  1664. : avcodec_find_decoder(codec_id);
  1665. switch (st->codec->codec_type) {
  1666. case AVMEDIA_TYPE_VIDEO:
  1667. prefix = 'v';
  1668. flags |= AV_OPT_FLAG_VIDEO_PARAM;
  1669. break;
  1670. case AVMEDIA_TYPE_AUDIO:
  1671. prefix = 'a';
  1672. flags |= AV_OPT_FLAG_AUDIO_PARAM;
  1673. break;
  1674. case AVMEDIA_TYPE_SUBTITLE:
  1675. prefix = 's';
  1676. flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
  1677. break;
  1678. }
  1679. while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
  1680. char *p = strchr(t->key, ':');
  1681. /* check stream specification in opt name */
  1682. if (p)
  1683. switch (check_stream_specifier(s, st, p + 1)) {
  1684. case 1: *p = 0; break;
  1685. case 0: continue;
  1686. default: return NULL;
  1687. }
  1688. if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
  1689. (codec && codec->priv_class &&
  1690. av_opt_find(&codec->priv_class, t->key, NULL, flags,
  1691. AV_OPT_SEARCH_FAKE_OBJ)))
  1692. av_dict_set(&ret, t->key, t->value, 0);
  1693. else if (t->key[0] == prefix &&
  1694. av_opt_find(&cc, t->key + 1, NULL, flags,
  1695. AV_OPT_SEARCH_FAKE_OBJ))
  1696. av_dict_set(&ret, t->key + 1, t->value, 0);
  1697. if (p)
  1698. *p = ':';
  1699. }
  1700. return ret;
  1701. }
  1702. AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
  1703. AVDictionary *codec_opts)
  1704. {
  1705. int i;
  1706. AVDictionary **opts;
  1707. if (!s->nb_streams)
  1708. return NULL;
  1709. opts = av_mallocz(s->nb_streams * sizeof(*opts));
  1710. if (!opts) {
  1711. av_log(NULL, AV_LOG_ERROR,
  1712. "Could not alloc memory for stream options.\n");
  1713. return NULL;
  1714. }
  1715. for (i = 0; i < s->nb_streams; i++)
  1716. opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
  1717. s, s->streams[i], NULL);
  1718. return opts;
  1719. }
  1720. void *grow_array(void *array, int elem_size, int *size, int new_size)
  1721. {
  1722. if (new_size >= INT_MAX / elem_size) {
  1723. av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
  1724. exit_program(1);
  1725. }
  1726. if (*size < new_size) {
  1727. uint8_t *tmp = av_realloc(array, new_size*elem_size);
  1728. if (!tmp) {
  1729. av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
  1730. exit_program(1);
  1731. }
  1732. memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
  1733. *size = new_size;
  1734. return tmp;
  1735. }
  1736. return array;
  1737. }