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
22 #include <string.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <math.h>
27
28 /* Include only the enabled headers since some compilers (namely, Sun
29 Studio) will not omit unused inline functions and create undefined
30 references to libraries that are not being built. */
31
32 #include "config.h"
54 #ifdef _WIN32
55 #include <windows.h>
57 #endif
58
62
64
66 {
71 }
72
74 {
75 vfprintf(stdout, fmt, vl);
76 }
77
79 {
80 #if HAVE_SETDLLDIRECTORY && defined(_WIN32)
81 /* Calling SetDllDirectory with the empty string (but not NULL) removes the
82 * current working directory from the DLL search path as a security pre-caution. */
83 SetDllDirectory("");
84 #endif
85 }
86
88 double min,
double max,
double *dst)
89 {
90 char *tail;
93 if (*tail)
94 error =
"Expected number for %s but found: %s\n";
95 else if (d < min || d >
max)
96 error =
"The value for %s was %s which is not within %f - %f\n";
98 error =
"Expected int64 for %s but found %s\n";
100 error =
"Expected int for %s but found %s\n";
101 else {
103 return 0;
104 }
105
108 }
109
111 int rej_flags)
112 {
115
118 char buf[128];
119
120 if (((po->
flags & req_flags) != req_flags) ||
121 (po->
flags & rej_flags))
122 continue;
123
127 }
129
131 av_strlcat(buf,
"[:<stream_spec>]",
sizeof(buf));
134
137
139 }
141 }
142
144 {
150 }
151
154 }
155
157 {
160
162 const char *end;
164 break;
165 po++;
166 }
167 return po;
168 }
169
170 /* _WIN32 means using the windows libc - cygwin doesn't define that
171 * by default. HAVE_COMMANDLINETOARGVW is true on cygwin, while
172 * it doesn't provide the actual command line via GetCommandLineW(). */
173 #if HAVE_COMMANDLINETOARGVW && defined(_WIN32)
174 #include <shellapi.h>
175 /* Will be leaked on exit */
176 static char** win32_argv_utf8 =
NULL;
177 static int win32_argc = 0;
178
179 /**
180 * Prepare command line arguments for executable.
181 * For Windows - perform wide-char to UTF-8 conversion.
182 * Input arguments should be main() function arguments.
183 * @param argc_ptr Arguments number (including executable)
184 * @param argv_ptr Arguments list.
185 */
187 {
188 char *argstr_flat;
189 wchar_t **argv_w;
190 int i, buffsize = 0,
offset = 0;
191
192 if (win32_argv_utf8) {
193 *argc_ptr = win32_argc;
194 *argv_ptr = win32_argv_utf8;
195 return;
196 }
197
198 win32_argc = 0;
199 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
200 if (win32_argc <= 0 || !argv_w)
201 return;
202
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,
207
208 win32_argv_utf8 =
av_mallocz(
sizeof(
char *) * (win32_argc + 1) + buffsize);
209 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
210 if (!win32_argv_utf8) {
211 LocalFree(argv_w);
212 return;
213 }
214
215 for (
i = 0;
i < win32_argc;
i++) {
216 win32_argv_utf8[
i] = &argstr_flat[
offset];
217 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[
i], -1,
220 }
221 win32_argv_utf8[
i] =
NULL;
222 LocalFree(argv_w);
223
224 *argc_ptr = win32_argc;
225 *argv_ptr = win32_argv_utf8;
226 }
227 #else
229 {
230 /* nothing to do */
231 }
232 #endif /* HAVE_COMMANDLINETOARGVW */
233
235 {
237 return 0;
240 return 1;
241 }
242
245 {
246 /* new-style options contain an offset into optctx, old-style address of
247 * a global var*/
250 char *arg_allocated =
NULL;
251
253 double num;
255
257 opt++;
258
261 "Requested to load an argument from file for a bool option '%s'\n",
264 }
265
267 if (!arg_allocated) {
269 "Error reading the value for option '%s' from file: %s\n",
272 }
273
275 }
276
278 char *p = strchr(opt, ':');
279 char *str;
280
281 sol = dst;
285
287 if (!str) {
290 }
291 sol->opt[sol->nb_opt - 1].specifier = str;
292 dst = &sol->opt[sol->nb_opt - 1].u;
293 }
294
296 char *str;
297 if (arg_allocated) {
298 str = arg_allocated;
299 arg_allocated =
NULL;
300 } else
303
304 if (!str) {
307 }
308
309 *(char **)dst = str;
314
315 *(int *)dst = num;
320
328 }
333
334 *(float *)dst = num;
339
340 *(double *)dst = num;
341 } else {
343
347 "Failed to set value '%s' for option '%s': %s\n",
350 }
351 }
355 }
356
357 if (sol) {
358 sol->type = po->
type;
361 }
362
366 }
367
370 {
372 .
name =
"AVOption passthrough",
376 };
377
380
382 if (!po->
name && opt[0] ==
'n' && opt[1] ==
'o') {
383 /* handle 'no' bool option */
389
391 po = &opt_avoptions;
395 }
399 }
400
404
406 }
407
409 int (*parse_arg_function)(void *, const char*))
410 {
411 const char *opt;
412 int optindex, handleoptions = 1,
ret;
413
414 /* perform system-dependent conversions for arguments list */
416
417 /* parse options */
418 optindex = 1;
419 while (optindex < argc) {
420 opt = argv[optindex++];
421
422 if (handleoptions && opt[0] == '-' && opt[1] != '0円') {
423 if (opt[1] == '-' && opt[2] == '0円') {
424 handleoptions = 0;
425 continue;
426 }
427 opt++;
428
432 } else {
433 if (parse_arg_function) {
434 ret = parse_arg_function(optctx, opt);
437 }
438 }
439 }
440
441 return 0;
442 }
443
445 {
447
449 g->group_def->name,
g->arg);
450
451 for (
i = 0;
i <
g->nb_opts;
i++) {
453
454 if (
g->group_def->flags &&
455 !(
g->group_def->flags & o->
opt->
flags)) {
457 "%s %s -- you are trying to apply an input option to an "
458 "output file or vice versa. Move this option before the "
459 "file it belongs to.\n", o->
key, o->
opt->
help,
460 g->group_def->name,
g->arg);
462 }
463
466
470 }
471
473
474 return 0;
475 }
476
478 const char *optname)
479 {
482
483 for (
i = 1;
i < argc;
i++) {
484 const char *cur_opt = argv[
i];
485
486 if (*cur_opt++ != '-')
487 continue;
488
490 if (!po->
name && cur_opt[0] ==
'n' && cur_opt[1] ==
'o')
492
493 if ((!po->
name && !strcmp(cur_opt, optname)) ||
494 (po->
name && !strcmp(optname, po->
name)))
496
499 }
500 return 0;
501 }
502
504 {
505 const unsigned char *p;
506
508 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
509 *p == '_' || (*p >= 'a' && *p <= 'z')))
510 break;
511 if (!*p) {
513 return;
514 }
516 for (p =
a; *p; p++) {
517 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
519 else if (*p < ' ' || *p > '~')
521 else
523 }
525 }
526
528 {
532
535
536 // OPT_FUNC_ARG can only be ser for OPT_TYPE_FUNC
538
539 po++;
540 }
541 }
542
544 {
546 char *env;
547
549
550 if (!idx)
552 if (idx && argv[idx + 1])
556 if (env || idx) {
562 for (
i = 0;
i < argc;
i++) {
565 }
567 }
568 }
571 if (idx)
573 }
574
576 int opt_flags, int search_flags)
577 {
581 return o;
582 }
583
584 #define FLAGS ((o->type == AV_OPT_TYPE_FLAGS && (arg[0]=='-' || arg[0]=='+')) ? AV_DICT_APPEND : 0)
586 {
588 int consumed = 0;
589 char opt_stripped[128];
590 const char *p;
592 #if CONFIG_SWSCALE
594 #endif
595 #if CONFIG_SWRESAMPLE
597 #endif
598
599 if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug"))
601
602 if (!(p = strchr(opt, ':')))
603 p = opt + strlen(opt);
604 av_strlcpy(opt_stripped, opt,
FFMIN(
sizeof(opt_stripped), p - opt + 1));
605
608 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
611 consumed = 1;
612 }
616 if (consumed)
618 consumed = 1;
619 }
620 #if CONFIG_SWSCALE
623 if (!strcmp(opt, "srcw") || !strcmp(opt, "srch") ||
624 !strcmp(opt, "dstw") || !strcmp(opt, "dsth") ||
625 !strcmp(opt, "src_format") || !strcmp(opt, "dst_format")) {
626 av_log(
NULL,
AV_LOG_ERROR,
"Directly using swscale dimensions/format options is not supported, please use the -s or -pix_fmt options\n");
628 }
630
631 consumed = 1;
632 }
633 #else
634 if (!consumed && !strcmp(opt, "sws_flags")) {
636 consumed = 1;
637 }
638 #endif
639 #if CONFIG_SWRESAMPLE
643 consumed = 1;
644 }
645 #endif
646
647 if (consumed)
648 return 0;
650 }
651
652 /*
653 * Check whether given option is a group separator.
654 *
655 * @return index of the group definition that matched or -1 if none
656 */
658 const char *opt)
659 {
661
662 for (
i = 0;
i < nb_groups;
i++) {
664 if (p->
sep && !strcmp(p->
sep, opt))
666 }
667
668 return -1;
669 }
670
671 /*
672 * Finish parsing an option group.
673 *
674 * @param group_idx which group definition should this group belong to
675 * @param arg argument of the group delimiting option
676 */
679 {
683
687
689
697
702
704
706 }
707
708 /*
709 * Add an option instance to currently parsed group.
710 */
712 const char *
key,
const char *
val)
713 {
717
721
722 g->opts[
g->nb_opts - 1].opt = opt;
723 g->opts[
g->nb_opts - 1].key =
key;
724 g->opts[
g->nb_opts - 1].val =
val;
725
726 return 0;
727 }
728
731 {
734
735 memset(octx, 0, sizeof(*octx));
736
741
744
747
748 return 0;
749 }
750
752 {
754
757
762
765 }
767 }
769
772
774 }
775
779 {
781 int optindex = 1;
782 int dashdash = -2;
783
784 /* perform system-dependent conversions for arguments list */
786
790
792
793 while (optindex < argc) {
794 const char *opt = argv[optindex++], *
arg;
797
799
800 if (opt[0] == '-' && opt[1] == '-' && !opt[2]) {
801 dashdash = optindex;
802 continue;
803 }
804 /* unnamed group separators, e.g. output filename */
805 if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
809
811 continue;
812 }
813 opt++;
814
815 #define GET_ARG(arg) \
816 do { \
817 arg = argv[optindex++]; \
818 if (!arg) { \
819 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
820 return AVERROR(EINVAL); \
821 } \
822 } while (0)
823
824 /* named group separators, e.g. -i */
826 if (group_idx >= 0) {
831
834 continue;
835 }
836
837 /* normal options */
841 /* optional argument, e.g. -h */
842 arg = argv[optindex++];
845 } else {
847 }
848
852
855 continue;
856 }
857
858 /* AVOptions */
859 if (argv[optindex]) {
863 "argument '%s'.\n", opt, argv[optindex]);
864 optindex++;
865 continue;
868 "with argument '%s'.\n", opt, argv[optindex]);
870 }
871 }
872
873 /* boolean -nofoo options */
874 if (opt[0] == 'n' && opt[1] == 'o' &&
880
882 "argument 0.\n", po->
name, po->
help);
883 continue;
884 }
885
888 }
889
892 "command: may be ignored.\n");
893
895
896 return 0;
897 }
898
900 {
903
904 while (
c !=
'\n' &&
c != EOF)
906
907 return yesno;
908 }
909
911 const char *preset_name, int is_path,
912 const char *codec_name)
913 {
916 #if HAVE_GETMODULEHANDLE && defined(_WIN32)
917 char *datadir =
NULL;
918 #endif
920 char *env_ffmpeg_datadir =
getenv_utf8(
"FFMPEG_DATADIR");
921 const char *
base[3] = { env_ffmpeg_datadir,
922 env_home, /* index=1(HOME) is special: search in a .ffmpeg subfolder */
923 FFMPEG_DATADIR, };
924
925 if (is_path) {
926 av_strlcpy(filename, preset_name, filename_size);
928 } else {
929 #if HAVE_GETMODULEHANDLE && defined(_WIN32)
930 wchar_t *datadir_w = get_module_filename(
NULL);
932
933 if (wchartoutf8(datadir_w, &datadir))
936
937 if (datadir)
938 {
939 char *ls;
940 for (ls = datadir; *ls; ls++)
941 if (*ls == '\\') *ls = '/';
942
943 if (ls = strrchr(datadir, '/'))
944 {
945 ptrdiff_t datadir_len = ls - datadir;
946 size_t desired_size = datadir_len + strlen("/ffpresets") + 1;
948 datadir, desired_size, sizeof *datadir);
949 if (new_datadir) {
950 datadir = new_datadir;
951 datadir[datadir_len] = 0;
952 strncat(datadir, "/ffpresets", desired_size - 1 - datadir_len);
954 }
955 }
956 }
957 #endif
958 for (
i = 0;
i < 3 && !
f;
i++) {
960 continue;
961 snprintf(filename, filename_size,
"%s%s/%s.ffpreset",
base[
i],
962 i != 1 ?
"" :
"/.ffmpeg", preset_name);
964 if (!
f && codec_name) {
966 "%s%s/%s-%s.ffpreset",
967 base[
i],
i != 1 ?
"" :
"/.ffmpeg", codec_name,
968 preset_name);
970 }
971 }
972 }
973
974 #if HAVE_GETMODULEHANDLE && defined(_WIN32)
976 #endif
980 }
981
983 {
988 }
989
993 {
998 char prefix = 0;
1000
1001 if (!codec)
1004
1007 prefix = 'v';
1009 break;
1011 prefix = 'a';
1013 break;
1015 prefix = 's';
1017 break;
1018 }
1019
1022 char *p = strchr(t->
key,
':');
1023
1024 /* check stream specification in opt name */
1025 if (p) {
1027 if (err < 0) {
1029 return err;
1030 } else if (!err)
1031 continue;
1032
1033 *p = 0;
1034 }
1035
1037 !codec ||
1042 else if (t->
key[0] == prefix &&
1046
1047 if (p)
1048 *p = ':';
1049 }
1050
1052 return 0;
1053 }
1054
1058 {
1061
1063
1065 return 0;
1066
1070
1071 for (
int i = 0;
i <
s->nb_streams;
i++) {
1076 }
1078 return 0;
1080 for (
int i = 0;
i <
s->nb_streams;
i++)
1084 }
1085
1087 {
1088 if (new_size >= INT_MAX / elem_size) {
1091 }
1092 if (*
size < new_size) {
1096 memset(
tmp + *
size*elem_size, 0, (new_size-*
size) * elem_size);
1099 return 0;
1100 }
1101 return 0;
1102 }
1103
1105 {
1106 void *new_elem;
1107
1111 return new_elem;
1112 }
1113
1115 {
1116 double theta = 0;
1117 if (displaymatrix)
1119
1120 theta -= 360*
floor(theta/360 + 0.9/360);
1121
1122 if (
fabs(theta - 90*
round(theta/90)) > 2)
1124 "If you want to help, upload a sample "
1125 "of this file to https://streams.videolan.org/upload/ "
1126 "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)");
1127
1128 return theta;
1129 }
1130
1131 /* read file contents into a string */
1133 {
1136 AVBPrint bprint;
1137 char *str;
1138
1142 }
1143
1150 }
1154 return str;
1155 }