cmdutils.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  2. * Various utilities for command line tools
  3. * Copyright (c) 2000-2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "libswscale/swscale.h"
  33. #include "libpostproc/postprocess.h"
  34. #include "libavutil/avstring.h"
  35. #include "libavutil/parseutils.h"
  36. #include "libavutil/pixdesc.h"
  37. #include "libavutil/eval.h"
  38. #include "libavcodec/opt.h"
  39. #include "cmdutils.h"
  40. #include "version.h"
  41. #if CONFIG_NETWORK
  42. #include "libavformat/network.h"
  43. #endif
  44. #if HAVE_SYS_RESOURCE_H
  45. #include <sys/resource.h>
  46. #endif
  47. const char **opt_names;
  48. const char **opt_values;
  49. static int opt_name_count;
  50. AVCodecContext *avcodec_opts[AVMEDIA_TYPE_NB];
  51. AVFormatContext *avformat_opts;
  52. struct SwsContext *sws_opts;
  53. static const int this_year = 2011;
  54. void init_opts(void)
  55. {
  56. int i;
  57. for (i = 0; i < AVMEDIA_TYPE_NB; i++)
  58. avcodec_opts[i] = avcodec_alloc_context2(i);
  59. avformat_opts = avformat_alloc_context();
  60. #if CONFIG_SWSCALE
  61. sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC, NULL, NULL, NULL);
  62. #endif
  63. }
  64. void uninit_opts(void)
  65. {
  66. int i;
  67. for (i = 0; i < AVMEDIA_TYPE_NB; i++)
  68. av_freep(&avcodec_opts[i]);
  69. av_freep(&avformat_opts->key);
  70. av_freep(&avformat_opts);
  71. #if CONFIG_SWSCALE
  72. av_freep(&sws_opts);
  73. #endif
  74. for (i = 0; i < opt_name_count; i++) {
  75. //opt_values are only stored for codec-specific options in which case
  76. //both the name and value are dup'd
  77. if (opt_values[i]) {
  78. av_freep(&opt_names[i]);
  79. av_freep(&opt_values[i]);
  80. }
  81. }
  82. av_freep(&opt_names);
  83. av_freep(&opt_values);
  84. }
  85. void log_callback_help(void* ptr, int level, const char* fmt, va_list vl)
  86. {
  87. vfprintf(stdout, fmt, vl);
  88. }
  89. double parse_number_or_die(const char *context, const char *numstr, int type, double min, double max)
  90. {
  91. char *tail;
  92. const char *error;
  93. double d = av_strtod(numstr, &tail);
  94. if (*tail)
  95. error= "Expected number for %s but found: %s\n";
  96. else if (d < min || d > max)
  97. error= "The value for %s was %s which is not within %f - %f\n";
  98. else if(type == OPT_INT64 && (int64_t)d != d)
  99. error= "Expected int64 for %s but found %s\n";
  100. else
  101. return d;
  102. fprintf(stderr, error, context, numstr, min, max);
  103. exit(1);
  104. }
  105. int64_t parse_time_or_die(const char *context, const char *timestr, int is_duration)
  106. {
  107. int64_t us;
  108. if (av_parse_time(&us, timestr, is_duration) < 0) {
  109. fprintf(stderr, "Invalid %s specification for %s: %s\n",
  110. is_duration ? "duration" : "date", context, timestr);
  111. exit(1);
  112. }
  113. return us;
  114. }
  115. void show_help_options(const OptionDef *options, const char *msg, int mask, int value)
  116. {
  117. const OptionDef *po;
  118. int first;
  119. first = 1;
  120. for(po = options; po->name != NULL; po++) {
  121. char buf[64];
  122. if ((po->flags & mask) == value) {
  123. if (first) {
  124. printf("%s", msg);
  125. first = 0;
  126. }
  127. av_strlcpy(buf, po->name, sizeof(buf));
  128. if (po->flags & HAS_ARG) {
  129. av_strlcat(buf, " ", sizeof(buf));
  130. av_strlcat(buf, po->argname, sizeof(buf));
  131. }
  132. printf("-%-17s %s\n", buf, po->help);
  133. }
  134. }
  135. }
  136. static const OptionDef* find_option(const OptionDef *po, const char *name){
  137. while (po->name != NULL) {
  138. if (!strcmp(name, po->name))
  139. break;
  140. po++;
  141. }
  142. return po;
  143. }
  144. void parse_options(int argc, char **argv, const OptionDef *options,
  145. void (* parse_arg_function)(const char*))
  146. {
  147. const char *opt, *arg;
  148. int optindex, handleoptions=1;
  149. const OptionDef *po;
  150. /* parse options */
  151. optindex = 1;
  152. while (optindex < argc) {
  153. opt = argv[optindex++];
  154. if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
  155. int bool_val = 1;
  156. if (opt[1] == '-' && opt[2] == '\0') {
  157. handleoptions = 0;
  158. continue;
  159. }
  160. opt++;
  161. po= find_option(options, opt);
  162. if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
  163. /* handle 'no' bool option */
  164. po = find_option(options, opt + 2);
  165. if (!(po->name && (po->flags & OPT_BOOL)))
  166. goto unknown_opt;
  167. bool_val = 0;
  168. }
  169. if (!po->name)
  170. po= find_option(options, "default");
  171. if (!po->name) {
  172. unknown_opt:
  173. fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
  174. exit(1);
  175. }
  176. arg = NULL;
  177. if (po->flags & HAS_ARG) {
  178. arg = argv[optindex++];
  179. if (!arg) {
  180. fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
  181. exit(1);
  182. }
  183. }
  184. if (po->flags & OPT_STRING) {
  185. char *str;
  186. str = av_strdup(arg);
  187. *po->u.str_arg = str;
  188. } else if (po->flags & OPT_BOOL) {
  189. *po->u.int_arg = bool_val;
  190. } else if (po->flags & OPT_INT) {
  191. *po->u.int_arg = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
  192. } else if (po->flags & OPT_INT64) {
  193. *po->u.int64_arg = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
  194. } else if (po->flags & OPT_FLOAT) {
  195. *po->u.float_arg = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
  196. } else if (po->flags & OPT_FUNC2) {
  197. if (po->u.func2_arg(opt, arg) < 0) {
  198. fprintf(stderr, "%s: failed to set value '%s' for option '%s'\n", argv[0], arg, opt);
  199. exit(1);
  200. }
  201. } else {
  202. po->u.func_arg(arg);
  203. }
  204. if(po->flags & OPT_EXIT)
  205. exit(0);
  206. } else {
  207. if (parse_arg_function)
  208. parse_arg_function(opt);
  209. }
  210. }
  211. }
  212. int opt_default(const char *opt, const char *arg){
  213. int type;
  214. int ret= 0;
  215. const AVOption *o= NULL;
  216. int opt_types[]={AV_OPT_FLAG_VIDEO_PARAM, AV_OPT_FLAG_AUDIO_PARAM, 0, AV_OPT_FLAG_SUBTITLE_PARAM, 0};
  217. for(type=0; *avcodec_opts && type<AVMEDIA_TYPE_NB && ret>= 0; type++){
  218. const AVOption *o2 = av_find_opt(avcodec_opts[0], opt, NULL, opt_types[type], opt_types[type]);
  219. if(o2)
  220. ret = av_set_string3(avcodec_opts[type], opt, arg, 1, &o);
  221. }
  222. if(!o && avformat_opts)
  223. ret = av_set_string3(avformat_opts, opt, arg, 1, &o);
  224. if(!o && sws_opts)
  225. ret = av_set_string3(sws_opts, opt, arg, 1, &o);
  226. if(!o){
  227. if (opt[0] == 'a' && avcodec_opts[AVMEDIA_TYPE_AUDIO])
  228. ret = av_set_string3(avcodec_opts[AVMEDIA_TYPE_AUDIO], opt+1, arg, 1, &o);
  229. else if(opt[0] == 'v' && avcodec_opts[AVMEDIA_TYPE_VIDEO])
  230. ret = av_set_string3(avcodec_opts[AVMEDIA_TYPE_VIDEO], opt+1, arg, 1, &o);
  231. else if(opt[0] == 's' && avcodec_opts[AVMEDIA_TYPE_SUBTITLE])
  232. ret = av_set_string3(avcodec_opts[AVMEDIA_TYPE_SUBTITLE], opt+1, arg, 1, &o);
  233. }
  234. if (o && ret < 0) {
  235. fprintf(stderr, "Invalid value '%s' for option '%s'\n", arg, opt);
  236. exit(1);
  237. }
  238. if (!o) {
  239. AVCodec *p = NULL;
  240. AVOutputFormat *oformat = NULL;
  241. while ((p=av_codec_next(p))){
  242. AVClass *c= p->priv_class;
  243. if(c && av_find_opt(&c, opt, NULL, 0, 0))
  244. break;
  245. }
  246. if (!p) {
  247. while ((oformat = av_oformat_next(oformat))) {
  248. const AVClass *c = oformat->priv_class;
  249. if (c && av_find_opt(&c, opt, NULL, 0, 0))
  250. break;
  251. }
  252. }
  253. if(!p && !oformat){
  254. fprintf(stderr, "Unrecognized option '%s'\n", opt);
  255. exit(1);
  256. }
  257. }
  258. // av_log(NULL, AV_LOG_ERROR, "%s:%s: %f 0x%0X\n", opt, arg, av_get_double(avcodec_opts, opt, NULL), (int)av_get_int(avcodec_opts, opt, NULL));
  259. //FIXME we should always use avcodec_opts, ... for storing options so there will not be any need to keep track of what i set over this
  260. opt_values= av_realloc(opt_values, sizeof(void*)*(opt_name_count+1));
  261. opt_values[opt_name_count]= o ? NULL : av_strdup(arg);
  262. opt_names= av_realloc(opt_names, sizeof(void*)*(opt_name_count+1));
  263. opt_names[opt_name_count++]= o ? o->name : av_strdup(opt);
  264. if ((*avcodec_opts && avcodec_opts[0]->debug) || (avformat_opts && avformat_opts->debug))
  265. av_log_set_level(AV_LOG_DEBUG);
  266. return 0;
  267. }
  268. int opt_loglevel(const char *opt, const char *arg)
  269. {
  270. const struct { const char *name; int level; } log_levels[] = {
  271. { "quiet" , AV_LOG_QUIET },
  272. { "panic" , AV_LOG_PANIC },
  273. { "fatal" , AV_LOG_FATAL },
  274. { "error" , AV_LOG_ERROR },
  275. { "warning", AV_LOG_WARNING },
  276. { "info" , AV_LOG_INFO },
  277. { "verbose", AV_LOG_VERBOSE },
  278. { "debug" , AV_LOG_DEBUG },
  279. };
  280. char *tail;
  281. int level;
  282. int i;
  283. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
  284. if (!strcmp(log_levels[i].name, arg)) {
  285. av_log_set_level(log_levels[i].level);
  286. return 0;
  287. }
  288. }
  289. level = strtol(arg, &tail, 10);
  290. if (*tail) {
  291. fprintf(stderr, "Invalid loglevel \"%s\". "
  292. "Possible levels are numbers or:\n", arg);
  293. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
  294. fprintf(stderr, "\"%s\"\n", log_levels[i].name);
  295. exit(1);
  296. }
  297. av_log_set_level(level);
  298. return 0;
  299. }
  300. int opt_timelimit(const char *opt, const char *arg)
  301. {
  302. #if HAVE_SETRLIMIT
  303. int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
  304. struct rlimit rl = { lim, lim + 1 };
  305. if (setrlimit(RLIMIT_CPU, &rl))
  306. perror("setrlimit");
  307. #else
  308. fprintf(stderr, "Warning: -%s not implemented on this OS\n", opt);
  309. #endif
  310. return 0;
  311. }
  312. void set_context_opts(void *ctx, void *opts_ctx, int flags, AVCodec *codec)
  313. {
  314. int i;
  315. void *priv_ctx=NULL;
  316. if(!strcmp("AVCodecContext", (*(AVClass**)ctx)->class_name)){
  317. AVCodecContext *avctx= ctx;
  318. if(codec && codec->priv_class && avctx->priv_data){
  319. priv_ctx= avctx->priv_data;
  320. }
  321. } else if (!strcmp("AVFormatContext", (*(AVClass**)ctx)->class_name)) {
  322. AVFormatContext *avctx = ctx;
  323. if (avctx->oformat && avctx->oformat->priv_class) {
  324. priv_ctx = avctx->priv_data;
  325. }
  326. }
  327. for(i=0; i<opt_name_count; i++){
  328. char buf[256];
  329. const AVOption *opt;
  330. const char *str= av_get_string(opts_ctx, opt_names[i], &opt, buf, sizeof(buf));
  331. /* if an option with name opt_names[i] is present in opts_ctx then str is non-NULL */
  332. if(str && ((opt->flags & flags) == flags))
  333. av_set_string3(ctx, opt_names[i], str, 1, NULL);
  334. /* We need to use a differnt system to pass options to the private context because
  335. it is not known which codec and thus context kind that will be when parsing options
  336. we thus use opt_values directly instead of opts_ctx */
  337. if(!str && priv_ctx && av_get_string(priv_ctx, opt_names[i], &opt, buf, sizeof(buf))){
  338. av_set_string3(priv_ctx, opt_names[i], opt_values[i], 1, NULL);
  339. }
  340. }
  341. }
  342. void print_error(const char *filename, int err)
  343. {
  344. char errbuf[128];
  345. const char *errbuf_ptr = errbuf;
  346. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  347. errbuf_ptr = strerror(AVUNERROR(err));
  348. fprintf(stderr, "%s: %s\n", filename, errbuf_ptr);
  349. }
  350. static int warned_cfg = 0;
  351. #define INDENT 1
  352. #define SHOW_VERSION 2
  353. #define SHOW_CONFIG 4
  354. #define PRINT_LIB_INFO(outstream,libname,LIBNAME,flags) \
  355. if (CONFIG_##LIBNAME) { \
  356. const char *indent = flags & INDENT? " " : ""; \
  357. if (flags & SHOW_VERSION) { \
  358. unsigned int version = libname##_version(); \
  359. fprintf(outstream, "%slib%-9s %2d.%3d.%2d / %2d.%3d.%2d\n", \
  360. indent, #libname, \
  361. LIB##LIBNAME##_VERSION_MAJOR, \
  362. LIB##LIBNAME##_VERSION_MINOR, \
  363. LIB##LIBNAME##_VERSION_MICRO, \
  364. version >> 16, version >> 8 & 0xff, version & 0xff); \
  365. } \
  366. if (flags & SHOW_CONFIG) { \
  367. const char *cfg = libname##_configuration(); \
  368. if (strcmp(LIBAV_CONFIGURATION, cfg)) { \
  369. if (!warned_cfg) { \
  370. fprintf(outstream, \
  371. "%sWARNING: library configuration mismatch\n", \
  372. indent); \
  373. warned_cfg = 1; \
  374. } \
  375. fprintf(stderr, "%s%-11s configuration: %s\n", \
  376. indent, #libname, cfg); \
  377. } \
  378. } \
  379. } \
  380. static void print_all_libs_info(FILE* outstream, int flags)
  381. {
  382. PRINT_LIB_INFO(outstream, avutil, AVUTIL, flags);
  383. PRINT_LIB_INFO(outstream, avcodec, AVCODEC, flags);
  384. PRINT_LIB_INFO(outstream, avformat, AVFORMAT, flags);
  385. PRINT_LIB_INFO(outstream, avdevice, AVDEVICE, flags);
  386. PRINT_LIB_INFO(outstream, avfilter, AVFILTER, flags);
  387. PRINT_LIB_INFO(outstream, swscale, SWSCALE, flags);
  388. PRINT_LIB_INFO(outstream, postproc, POSTPROC, flags);
  389. }
  390. void show_banner(void)
  391. {
  392. fprintf(stderr, "%s version " LIBAV_VERSION ", Copyright (c) %d-%d the Libav developers\n",
  393. program_name, program_birth_year, this_year);
  394. fprintf(stderr, " built on %s %s with %s %s\n",
  395. __DATE__, __TIME__, CC_TYPE, CC_VERSION);
  396. fprintf(stderr, " configuration: " LIBAV_CONFIGURATION "\n");
  397. print_all_libs_info(stderr, INDENT|SHOW_CONFIG);
  398. print_all_libs_info(stderr, INDENT|SHOW_VERSION);
  399. }
  400. void show_version(void) {
  401. printf("%s " LIBAV_VERSION "\n", program_name);
  402. print_all_libs_info(stdout, SHOW_VERSION);
  403. }
  404. void show_license(void)
  405. {
  406. printf(
  407. #if CONFIG_NONFREE
  408. "This version of %s has nonfree parts compiled in.\n"
  409. "Therefore it is not legally redistributable.\n",
  410. program_name
  411. #elif CONFIG_GPLV3
  412. "%s is free software; you can redistribute it and/or modify\n"
  413. "it under the terms of the GNU General Public License as published by\n"
  414. "the Free Software Foundation; either version 3 of the License, or\n"
  415. "(at your option) any later version.\n"
  416. "\n"
  417. "%s is distributed in the hope that it will be useful,\n"
  418. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  419. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  420. "GNU General Public License for more details.\n"
  421. "\n"
  422. "You should have received a copy of the GNU General Public License\n"
  423. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  424. program_name, program_name, program_name
  425. #elif CONFIG_GPL
  426. "%s is free software; you can redistribute it and/or modify\n"
  427. "it under the terms of the GNU General Public License as published by\n"
  428. "the Free Software Foundation; either version 2 of the License, or\n"
  429. "(at your option) any later version.\n"
  430. "\n"
  431. "%s is distributed in the hope that it will be useful,\n"
  432. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  433. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  434. "GNU General Public License for more details.\n"
  435. "\n"
  436. "You should have received a copy of the GNU General Public License\n"
  437. "along with %s; if not, write to the Free Software\n"
  438. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  439. program_name, program_name, program_name
  440. #elif CONFIG_LGPLV3
  441. "%s is free software; you can redistribute it and/or modify\n"
  442. "it under the terms of the GNU Lesser General Public License as published by\n"
  443. "the Free Software Foundation; either version 3 of the License, or\n"
  444. "(at your option) any later version.\n"
  445. "\n"
  446. "%s is distributed in the hope that it will be useful,\n"
  447. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  448. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  449. "GNU Lesser General Public License for more details.\n"
  450. "\n"
  451. "You should have received a copy of the GNU Lesser General Public License\n"
  452. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  453. program_name, program_name, program_name
  454. #else
  455. "%s is free software; you can redistribute it and/or\n"
  456. "modify it under the terms of the GNU Lesser General Public\n"
  457. "License as published by the Free Software Foundation; either\n"
  458. "version 2.1 of the License, or (at your option) any later version.\n"
  459. "\n"
  460. "%s is distributed in the hope that it will be useful,\n"
  461. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  462. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  463. "Lesser General Public License for more details.\n"
  464. "\n"
  465. "You should have received a copy of the GNU Lesser General Public\n"
  466. "License along with %s; if not, write to the Free Software\n"
  467. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  468. program_name, program_name, program_name
  469. #endif
  470. );
  471. }
  472. void list_fmts(void (*get_fmt_string)(char *buf, int buf_size, int fmt), int nb_fmts)
  473. {
  474. int i;
  475. char fmt_str[128];
  476. for (i=-1; i < nb_fmts; i++) {
  477. get_fmt_string (fmt_str, sizeof(fmt_str), i);
  478. fprintf(stdout, "%s\n", fmt_str);
  479. }
  480. }
  481. void show_formats(void)
  482. {
  483. AVInputFormat *ifmt=NULL;
  484. AVOutputFormat *ofmt=NULL;
  485. const char *last_name;
  486. printf(
  487. "File formats:\n"
  488. " D. = Demuxing supported\n"
  489. " .E = Muxing supported\n"
  490. " --\n");
  491. last_name= "000";
  492. for(;;){
  493. int decode=0;
  494. int encode=0;
  495. const char *name=NULL;
  496. const char *long_name=NULL;
  497. while((ofmt= av_oformat_next(ofmt))) {
  498. if((name == NULL || strcmp(ofmt->name, name)<0) &&
  499. strcmp(ofmt->name, last_name)>0){
  500. name= ofmt->name;
  501. long_name= ofmt->long_name;
  502. encode=1;
  503. }
  504. }
  505. while((ifmt= av_iformat_next(ifmt))) {
  506. if((name == NULL || strcmp(ifmt->name, name)<0) &&
  507. strcmp(ifmt->name, last_name)>0){
  508. name= ifmt->name;
  509. long_name= ifmt->long_name;
  510. encode=0;
  511. }
  512. if(name && strcmp(ifmt->name, name)==0)
  513. decode=1;
  514. }
  515. if(name==NULL)
  516. break;
  517. last_name= name;
  518. printf(
  519. " %s%s %-15s %s\n",
  520. decode ? "D":" ",
  521. encode ? "E":" ",
  522. name,
  523. long_name ? long_name:" ");
  524. }
  525. }
  526. void show_codecs(void)
  527. {
  528. AVCodec *p=NULL, *p2;
  529. const char *last_name;
  530. printf(
  531. "Codecs:\n"
  532. " D..... = Decoding supported\n"
  533. " .E.... = Encoding supported\n"
  534. " ..V... = Video codec\n"
  535. " ..A... = Audio codec\n"
  536. " ..S... = Subtitle codec\n"
  537. " ...S.. = Supports draw_horiz_band\n"
  538. " ....D. = Supports direct rendering method 1\n"
  539. " .....T = Supports weird frame truncation\n"
  540. " ------\n");
  541. last_name= "000";
  542. for(;;){
  543. int decode=0;
  544. int encode=0;
  545. int cap=0;
  546. const char *type_str;
  547. p2=NULL;
  548. while((p= av_codec_next(p))) {
  549. if((p2==NULL || strcmp(p->name, p2->name)<0) &&
  550. strcmp(p->name, last_name)>0){
  551. p2= p;
  552. decode= encode= cap=0;
  553. }
  554. if(p2 && strcmp(p->name, p2->name)==0){
  555. if(p->decode) decode=1;
  556. if(p->encode) encode=1;
  557. cap |= p->capabilities;
  558. }
  559. }
  560. if(p2==NULL)
  561. break;
  562. last_name= p2->name;
  563. switch(p2->type) {
  564. case AVMEDIA_TYPE_VIDEO:
  565. type_str = "V";
  566. break;
  567. case AVMEDIA_TYPE_AUDIO:
  568. type_str = "A";
  569. break;
  570. case AVMEDIA_TYPE_SUBTITLE:
  571. type_str = "S";
  572. break;
  573. default:
  574. type_str = "?";
  575. break;
  576. }
  577. printf(
  578. " %s%s%s%s%s%s %-15s %s",
  579. decode ? "D": (/*p2->decoder ? "d":*/" "),
  580. encode ? "E":" ",
  581. type_str,
  582. cap & CODEC_CAP_DRAW_HORIZ_BAND ? "S":" ",
  583. cap & CODEC_CAP_DR1 ? "D":" ",
  584. cap & CODEC_CAP_TRUNCATED ? "T":" ",
  585. p2->name,
  586. p2->long_name ? p2->long_name : "");
  587. /* if(p2->decoder && decode==0)
  588. printf(" use %s for decoding", p2->decoder->name);*/
  589. printf("\n");
  590. }
  591. printf("\n");
  592. printf(
  593. "Note, the names of encoders and decoders do not always match, so there are\n"
  594. "several cases where the above table shows encoder only or decoder only entries\n"
  595. "even though both encoding and decoding are supported. For example, the h263\n"
  596. "decoder corresponds to the h263 and h263p encoders, for file formats it is even\n"
  597. "worse.\n");
  598. }
  599. void show_bsfs(void)
  600. {
  601. AVBitStreamFilter *bsf=NULL;
  602. printf("Bitstream filters:\n");
  603. while((bsf = av_bitstream_filter_next(bsf)))
  604. printf("%s\n", bsf->name);
  605. printf("\n");
  606. }
  607. void show_protocols(void)
  608. {
  609. void *opaque = NULL;
  610. const char *name;
  611. printf("Supported file protocols:\n"
  612. "Input:\n");
  613. while ((name = avio_enum_protocols(&opaque, 0)))
  614. printf("%s\n", name);
  615. printf("Output:\n");
  616. while ((name = avio_enum_protocols(&opaque, 1)))
  617. printf("%s\n", name);
  618. }
  619. void show_filters(void)
  620. {
  621. AVFilter av_unused(**filter) = NULL;
  622. printf("Filters:\n");
  623. #if CONFIG_AVFILTER
  624. while ((filter = av_filter_next(filter)) && *filter)
  625. printf("%-16s %s\n", (*filter)->name, (*filter)->description);
  626. #endif
  627. }
  628. void show_pix_fmts(void)
  629. {
  630. enum PixelFormat pix_fmt;
  631. printf(
  632. "Pixel formats:\n"
  633. "I.... = Supported Input format for conversion\n"
  634. ".O... = Supported Output format for conversion\n"
  635. "..H.. = Hardware accelerated format\n"
  636. "...P. = Paletted format\n"
  637. "....B = Bitstream format\n"
  638. "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
  639. "-----\n");
  640. #if !CONFIG_SWSCALE
  641. # define sws_isSupportedInput(x) 0
  642. # define sws_isSupportedOutput(x) 0
  643. #endif
  644. for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) {
  645. const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
  646. printf("%c%c%c%c%c %-16s %d %2d\n",
  647. sws_isSupportedInput (pix_fmt) ? 'I' : '.',
  648. sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
  649. pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
  650. pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
  651. pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
  652. pix_desc->name,
  653. pix_desc->nb_components,
  654. av_get_bits_per_pixel(pix_desc));
  655. }
  656. }
  657. int read_yesno(void)
  658. {
  659. int c = getchar();
  660. int yesno = (toupper(c) == 'Y');
  661. while (c != '\n' && c != EOF)
  662. c = getchar();
  663. return yesno;
  664. }
  665. int read_file(const char *filename, char **bufptr, size_t *size)
  666. {
  667. FILE *f = fopen(filename, "rb");
  668. if (!f) {
  669. fprintf(stderr, "Cannot read file '%s': %s\n", filename, strerror(errno));
  670. return AVERROR(errno);
  671. }
  672. fseek(f, 0, SEEK_END);
  673. *size = ftell(f);
  674. fseek(f, 0, SEEK_SET);
  675. *bufptr = av_malloc(*size + 1);
  676. if (!*bufptr) {
  677. fprintf(stderr, "Could not allocate file buffer\n");
  678. fclose(f);
  679. return AVERROR(ENOMEM);
  680. }
  681. fread(*bufptr, 1, *size, f);
  682. (*bufptr)[*size++] = '\0';
  683. fclose(f);
  684. return 0;
  685. }
  686. void init_pts_correction(PtsCorrectionContext *ctx)
  687. {
  688. ctx->num_faulty_pts = ctx->num_faulty_dts = 0;
  689. ctx->last_pts = ctx->last_dts = INT64_MIN;
  690. }
  691. int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t reordered_pts, int64_t dts)
  692. {
  693. int64_t pts = AV_NOPTS_VALUE;
  694. if (dts != AV_NOPTS_VALUE) {
  695. ctx->num_faulty_dts += dts <= ctx->last_dts;
  696. ctx->last_dts = dts;
  697. }
  698. if (reordered_pts != AV_NOPTS_VALUE) {
  699. ctx->num_faulty_pts += reordered_pts <= ctx->last_pts;
  700. ctx->last_pts = reordered_pts;
  701. }
  702. if ((ctx->num_faulty_pts<=ctx->num_faulty_dts || dts == AV_NOPTS_VALUE)
  703. && reordered_pts != AV_NOPTS_VALUE)
  704. pts = reordered_pts;
  705. else
  706. pts = dts;
  707. return pts;
  708. }
  709. FILE *get_preset_file(char *filename, size_t filename_size,
  710. const char *preset_name, int is_path, const char *codec_name)
  711. {
  712. FILE *f = NULL;
  713. int i;
  714. const char *base[3]= { getenv("FFMPEG_DATADIR"),
  715. getenv("HOME"),
  716. FFMPEG_DATADIR,
  717. };
  718. if (is_path) {
  719. av_strlcpy(filename, preset_name, filename_size);
  720. f = fopen(filename, "r");
  721. } else {
  722. for (i = 0; i < 3 && !f; i++) {
  723. if (!base[i])
  724. continue;
  725. snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i], i != 1 ? "" : "/.ffmpeg", preset_name);
  726. f = fopen(filename, "r");
  727. if (!f && codec_name) {
  728. snprintf(filename, filename_size,
  729. "%s%s/%s-%s.ffpreset", base[i], i != 1 ? "" : "/.ffmpeg", codec_name, preset_name);
  730. f = fopen(filename, "r");
  731. }
  732. }
  733. }
  734. return f;
  735. }
  736. #if CONFIG_AVFILTER
  737. static int ffsink_init(AVFilterContext *ctx, const char *args, void *opaque)
  738. {
  739. FFSinkContext *priv = ctx->priv;
  740. if (!opaque)
  741. return AVERROR(EINVAL);
  742. *priv = *(FFSinkContext *)opaque;
  743. return 0;
  744. }
  745. static void null_end_frame(AVFilterLink *inlink) { }
  746. static int ffsink_query_formats(AVFilterContext *ctx)
  747. {
  748. FFSinkContext *priv = ctx->priv;
  749. enum PixelFormat pix_fmts[] = { priv->pix_fmt, PIX_FMT_NONE };
  750. avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
  751. return 0;
  752. }
  753. AVFilter ffsink = {
  754. .name = "ffsink",
  755. .priv_size = sizeof(FFSinkContext),
  756. .init = ffsink_init,
  757. .query_formats = ffsink_query_formats,
  758. .inputs = (AVFilterPad[]) {{ .name = "default",
  759. .type = AVMEDIA_TYPE_VIDEO,
  760. .end_frame = null_end_frame,
  761. .min_perms = AV_PERM_READ, },
  762. { .name = NULL }},
  763. .outputs = (AVFilterPad[]) {{ .name = NULL }},
  764. };
  765. int get_filtered_video_frame(AVFilterContext *ctx, AVFrame *frame,
  766. AVFilterBufferRef **picref_ptr, AVRational *tb)
  767. {
  768. int ret;
  769. AVFilterBufferRef *picref;
  770. if ((ret = avfilter_request_frame(ctx->inputs[0])) < 0)
  771. return ret;
  772. if (!(picref = ctx->inputs[0]->cur_buf))
  773. return AVERROR(ENOENT);
  774. *picref_ptr = picref;
  775. ctx->inputs[0]->cur_buf = NULL;
  776. *tb = ctx->inputs[0]->time_base;
  777. memcpy(frame->data, picref->data, sizeof(frame->data));
  778. memcpy(frame->linesize, picref->linesize, sizeof(frame->linesize));
  779. frame->interlaced_frame = picref->video->interlaced;
  780. frame->top_field_first = picref->video->top_field_first;
  781. return 1;
  782. }
  783. #endif /* CONFIG_AVFILTER */