cmdutils.c 61 KB

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