cmdutils.c 61 KB

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