cmdutils.c 50 KB

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