cmdutils.c 54 KB

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