cmdutils.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 "config.h"
  26. #include "libavformat/avformat.h"
  27. #include "libavfilter/avfilter.h"
  28. #include "libavdevice/avdevice.h"
  29. #include "libswscale/swscale.h"
  30. #include "libpostproc/postprocess.h"
  31. #include "libavutil/avstring.h"
  32. #include "cmdutils.h"
  33. #include "version.h"
  34. #ifdef CONFIG_NETWORK
  35. #include "libavformat/network.h"
  36. #endif
  37. #undef exit
  38. double parse_number_or_die(const char *context, const char *numstr, int type, double min, double max)
  39. {
  40. char *tail;
  41. const char *error;
  42. double d = strtod(numstr, &tail);
  43. if (*tail)
  44. error= "Expected number for %s but found: %s\n";
  45. else if (d < min || d > max)
  46. error= "The value for %s was %s which is not within %f - %f\n";
  47. else if(type == OPT_INT64 && (int64_t)d != d)
  48. error= "Expected int64 for %s but found %s\n";
  49. else
  50. return d;
  51. fprintf(stderr, error, context, numstr, min, max);
  52. exit(1);
  53. }
  54. int64_t parse_time_or_die(const char *context, const char *timestr, int is_duration)
  55. {
  56. int64_t us = parse_date(timestr, is_duration);
  57. if (us == INT64_MIN) {
  58. fprintf(stderr, "Invalid %s specification for %s: %s\n",
  59. is_duration ? "duration" : "date", context, timestr);
  60. exit(1);
  61. }
  62. return us;
  63. }
  64. void show_help_options(const OptionDef *options, const char *msg, int mask, int value)
  65. {
  66. const OptionDef *po;
  67. int first;
  68. first = 1;
  69. for(po = options; po->name != NULL; po++) {
  70. char buf[64];
  71. if ((po->flags & mask) == value) {
  72. if (first) {
  73. printf("%s", msg);
  74. first = 0;
  75. }
  76. av_strlcpy(buf, po->name, sizeof(buf));
  77. if (po->flags & HAS_ARG) {
  78. av_strlcat(buf, " ", sizeof(buf));
  79. av_strlcat(buf, po->argname, sizeof(buf));
  80. }
  81. printf("-%-17s %s\n", buf, po->help);
  82. }
  83. }
  84. }
  85. static const OptionDef* find_option(const OptionDef *po, const char *name){
  86. while (po->name != NULL) {
  87. if (!strcmp(name, po->name))
  88. break;
  89. po++;
  90. }
  91. return po;
  92. }
  93. void parse_options(int argc, char **argv, const OptionDef *options,
  94. void (* parse_arg_function)(const char*))
  95. {
  96. const char *opt, *arg;
  97. int optindex, handleoptions=1;
  98. const OptionDef *po;
  99. /* parse options */
  100. optindex = 1;
  101. while (optindex < argc) {
  102. opt = argv[optindex++];
  103. if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
  104. if (opt[1] == '-' && opt[2] == '\0') {
  105. handleoptions = 0;
  106. continue;
  107. }
  108. po= find_option(options, opt + 1);
  109. if (!po->name)
  110. po= find_option(options, "default");
  111. if (!po->name) {
  112. unknown_opt:
  113. fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
  114. exit(1);
  115. }
  116. arg = NULL;
  117. if (po->flags & HAS_ARG) {
  118. arg = argv[optindex++];
  119. if (!arg) {
  120. fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
  121. exit(1);
  122. }
  123. }
  124. if (po->flags & OPT_STRING) {
  125. char *str;
  126. str = av_strdup(arg);
  127. *po->u.str_arg = str;
  128. } else if (po->flags & OPT_BOOL) {
  129. *po->u.int_arg = 1;
  130. } else if (po->flags & OPT_INT) {
  131. *po->u.int_arg = parse_number_or_die(opt+1, arg, OPT_INT64, INT_MIN, INT_MAX);
  132. } else if (po->flags & OPT_INT64) {
  133. *po->u.int64_arg = parse_number_or_die(opt+1, arg, OPT_INT64, INT64_MIN, INT64_MAX);
  134. } else if (po->flags & OPT_FLOAT) {
  135. *po->u.float_arg = parse_number_or_die(opt+1, arg, OPT_FLOAT, -1.0/0.0, 1.0/0.0);
  136. } else if (po->flags & OPT_FUNC2) {
  137. if(po->u.func2_arg(opt+1, arg)<0)
  138. goto unknown_opt;
  139. } else {
  140. po->u.func_arg(arg);
  141. }
  142. if(po->flags & OPT_EXIT)
  143. exit(0);
  144. } else {
  145. if (parse_arg_function)
  146. parse_arg_function(opt);
  147. }
  148. }
  149. }
  150. void print_error(const char *filename, int err)
  151. {
  152. switch(err) {
  153. case AVERROR_NUMEXPECTED:
  154. fprintf(stderr, "%s: Incorrect image filename syntax.\n"
  155. "Use '%%d' to specify the image number:\n"
  156. " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
  157. " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
  158. filename);
  159. break;
  160. case AVERROR_INVALIDDATA:
  161. fprintf(stderr, "%s: Error while parsing header\n", filename);
  162. break;
  163. case AVERROR_NOFMT:
  164. fprintf(stderr, "%s: Unknown format\n", filename);
  165. break;
  166. case AVERROR(EIO):
  167. fprintf(stderr, "%s: I/O error occurred\n"
  168. "Usually that means that input file is truncated and/or corrupted.\n",
  169. filename);
  170. break;
  171. case AVERROR(ENOMEM):
  172. fprintf(stderr, "%s: memory allocation error occurred\n", filename);
  173. break;
  174. case AVERROR(ENOENT):
  175. fprintf(stderr, "%s: no such file or directory\n", filename);
  176. break;
  177. #ifdef CONFIG_NETWORK
  178. case AVERROR(FF_NETERROR(EPROTONOSUPPORT)):
  179. fprintf(stderr, "%s: Unsupported network protocol\n", filename);
  180. break;
  181. #endif
  182. default:
  183. fprintf(stderr, "%s: Error while opening file\n", filename);
  184. break;
  185. }
  186. }
  187. #define PRINT_LIB_VERSION(outstream,libname,LIBNAME,indent) \
  188. version= libname##_version(); \
  189. fprintf(outstream, "%slib%-10s %2d.%2d.%2d / %2d.%2d.%2d\n", indent? " " : "", #libname, \
  190. LIB##LIBNAME##_VERSION_MAJOR, LIB##LIBNAME##_VERSION_MINOR, LIB##LIBNAME##_VERSION_MICRO, \
  191. version >> 16, version >> 8 & 0xff, version & 0xff);
  192. void print_all_lib_versions(FILE* outstream, int indent)
  193. {
  194. unsigned int version;
  195. PRINT_LIB_VERSION(outstream, avutil, AVUTIL, indent);
  196. PRINT_LIB_VERSION(outstream, avcodec, AVCODEC, indent);
  197. PRINT_LIB_VERSION(outstream, avformat, AVFORMAT, indent);
  198. PRINT_LIB_VERSION(outstream, avdevice, AVDEVICE, indent);
  199. #if ENABLE_AVFILTER
  200. PRINT_LIB_VERSION(outstream, avfilter, AVFILTER, indent);
  201. #endif
  202. #if ENABLE_SWSCALE
  203. PRINT_LIB_VERSION(outstream, swscale, SWSCALE, indent);
  204. #endif
  205. #if ENABLE_POSTPROC
  206. PRINT_LIB_VERSION(outstream, postproc, POSTPROC, indent);
  207. #endif
  208. }
  209. void show_banner(void)
  210. {
  211. fprintf(stderr, "%s version " FFMPEG_VERSION ", Copyright (c) %d-2008 Fabrice Bellard, et al.\n",
  212. program_name, program_birth_year);
  213. fprintf(stderr, " configuration: " FFMPEG_CONFIGURATION "\n");
  214. print_all_lib_versions(stderr, 1);
  215. fprintf(stderr, " built on " __DATE__ " " __TIME__);
  216. #ifdef __GNUC__
  217. fprintf(stderr, ", gcc: " __VERSION__ "\n");
  218. #else
  219. fprintf(stderr, ", using a non-gcc compiler\n");
  220. #endif
  221. }
  222. void show_version(void) {
  223. printf("%s " FFMPEG_VERSION "\n", program_name);
  224. print_all_lib_versions(stdout, 0);
  225. }
  226. void show_license(void)
  227. {
  228. #ifdef CONFIG_NONFREE
  229. printf(
  230. "This version of %s has nonfree parts compiled in.\n"
  231. "Therefore it is not legally redistributable.\n",
  232. program_name
  233. );
  234. #elif CONFIG_GPL
  235. printf(
  236. "%s is free software; you can redistribute it and/or modify\n"
  237. "it under the terms of the GNU General Public License as published by\n"
  238. "the Free Software Foundation; either version 2 of the License, or\n"
  239. "(at your option) any later version.\n"
  240. "\n"
  241. "%s is distributed in the hope that it will be useful,\n"
  242. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  243. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  244. "GNU General Public License for more details.\n"
  245. "\n"
  246. "You should have received a copy of the GNU General Public License\n"
  247. "along with %s; if not, write to the Free Software\n"
  248. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  249. program_name, program_name, program_name
  250. );
  251. #else
  252. printf(
  253. "%s is free software; you can redistribute it and/or\n"
  254. "modify it under the terms of the GNU Lesser General Public\n"
  255. "License as published by the Free Software Foundation; either\n"
  256. "version 2.1 of the License, or (at your option) any later version.\n"
  257. "\n"
  258. "%s is distributed in the hope that it will be useful,\n"
  259. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  260. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  261. "Lesser General Public License for more details.\n"
  262. "\n"
  263. "You should have received a copy of the GNU Lesser General Public\n"
  264. "License along with %s; if not, write to the Free Software\n"
  265. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  266. program_name, program_name, program_name
  267. );
  268. #endif
  269. }
  270. void show_formats(void)
  271. {
  272. AVInputFormat *ifmt=NULL;
  273. AVOutputFormat *ofmt=NULL;
  274. URLProtocol *up=NULL;
  275. AVCodec *p=NULL, *p2;
  276. AVBitStreamFilter *bsf=NULL;
  277. const char *last_name;
  278. printf("File formats:\n");
  279. last_name= "000";
  280. for(;;){
  281. int decode=0;
  282. int encode=0;
  283. const char *name=NULL;
  284. const char *long_name=NULL;
  285. while((ofmt= av_oformat_next(ofmt))) {
  286. if((name == NULL || strcmp(ofmt->name, name)<0) &&
  287. strcmp(ofmt->name, last_name)>0){
  288. name= ofmt->name;
  289. long_name= ofmt->long_name;
  290. encode=1;
  291. }
  292. }
  293. while((ifmt= av_iformat_next(ifmt))) {
  294. if((name == NULL || strcmp(ifmt->name, name)<0) &&
  295. strcmp(ifmt->name, last_name)>0){
  296. name= ifmt->name;
  297. long_name= ifmt->long_name;
  298. encode=0;
  299. }
  300. if(name && strcmp(ifmt->name, name)==0)
  301. decode=1;
  302. }
  303. if(name==NULL)
  304. break;
  305. last_name= name;
  306. printf(
  307. " %s%s %-15s %s\n",
  308. decode ? "D":" ",
  309. encode ? "E":" ",
  310. name,
  311. long_name ? long_name:" ");
  312. }
  313. printf("\n");
  314. printf("Codecs:\n");
  315. last_name= "000";
  316. for(;;){
  317. int decode=0;
  318. int encode=0;
  319. int cap=0;
  320. const char *type_str;
  321. p2=NULL;
  322. while((p= av_codec_next(p))) {
  323. if((p2==NULL || strcmp(p->name, p2->name)<0) &&
  324. strcmp(p->name, last_name)>0){
  325. p2= p;
  326. decode= encode= cap=0;
  327. }
  328. if(p2 && strcmp(p->name, p2->name)==0){
  329. if(p->decode) decode=1;
  330. if(p->encode) encode=1;
  331. cap |= p->capabilities;
  332. }
  333. }
  334. if(p2==NULL)
  335. break;
  336. last_name= p2->name;
  337. switch(p2->type) {
  338. case CODEC_TYPE_VIDEO:
  339. type_str = "V";
  340. break;
  341. case CODEC_TYPE_AUDIO:
  342. type_str = "A";
  343. break;
  344. case CODEC_TYPE_SUBTITLE:
  345. type_str = "S";
  346. break;
  347. default:
  348. type_str = "?";
  349. break;
  350. }
  351. printf(
  352. " %s%s%s%s%s%s %-15s %s",
  353. decode ? "D": (/*p2->decoder ? "d":*/" "),
  354. encode ? "E":" ",
  355. type_str,
  356. cap & CODEC_CAP_DRAW_HORIZ_BAND ? "S":" ",
  357. cap & CODEC_CAP_DR1 ? "D":" ",
  358. cap & CODEC_CAP_TRUNCATED ? "T":" ",
  359. p2->name,
  360. p2->long_name ? p2->long_name : "");
  361. /* if(p2->decoder && decode==0)
  362. printf(" use %s for decoding", p2->decoder->name);*/
  363. printf("\n");
  364. }
  365. printf("\n");
  366. printf("Bitstream filters:\n");
  367. while((bsf = av_bitstream_filter_next(bsf)))
  368. printf(" %s", bsf->name);
  369. printf("\n");
  370. printf("Supported file protocols:\n");
  371. while((up = av_protocol_next(up)))
  372. printf(" %s:", up->name);
  373. printf("\n");
  374. printf("Frame size, frame rate abbreviations:\n ntsc pal qntsc qpal sntsc spal film ntsc-film sqcif qcif cif 4cif\n");
  375. printf("\n");
  376. printf(
  377. "Note, the names of encoders and decoders do not always match, so there are\n"
  378. "several cases where the above table shows encoder only or decoder only entries\n"
  379. "even though both encoding and decoding are supported. For example, the h263\n"
  380. "decoder corresponds to the h263 and h263p encoders, for file formats it is even\n"
  381. "worse.\n");
  382. }