cmdutils.c 61 KB

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