cmdutils.c 50 KB

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