cmdutils.c 45 KB

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