opt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <limits.h>
  19. #include <stdio.h>
  20. #include "libavutil/common.h"
  21. #include "libavutil/channel_layout.h"
  22. #include "libavutil/error.h"
  23. #include "libavutil/log.h"
  24. #include "libavutil/mem.h"
  25. #include "libavutil/rational.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/pixdesc.h"
  28. typedef struct TestContext {
  29. const AVClass *class;
  30. int num;
  31. int toggle;
  32. char *string;
  33. int flags;
  34. AVRational rational;
  35. AVRational video_rate;
  36. int w, h;
  37. enum AVPixelFormat pix_fmt;
  38. enum AVSampleFormat sample_fmt;
  39. int64_t duration;
  40. uint8_t color[4];
  41. int64_t channel_layout;
  42. void *binary;
  43. int binary_size;
  44. void *binary1;
  45. int binary_size1;
  46. void *binary2;
  47. int binary_size2;
  48. int64_t num64;
  49. float flt;
  50. double dbl;
  51. char *escape;
  52. int bool1;
  53. int bool2;
  54. int bool3;
  55. } TestContext;
  56. #define OFFSET(x) offsetof(TestContext, x)
  57. #define TEST_FLAG_COOL 01
  58. #define TEST_FLAG_LAME 02
  59. #define TEST_FLAG_MU 04
  60. static const AVOption test_options[]= {
  61. {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, 1 },
  62. {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, 1 },
  63. {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, 10, 1 },
  64. {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, { .str = "default" }, CHAR_MIN, CHAR_MAX, 1 },
  65. {"escape", "set escape str", OFFSET(escape), AV_OPT_TYPE_STRING, { .str = "\\=," }, CHAR_MIN, CHAR_MAX, 1 },
  66. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, { .i64 = 1 }, 0, INT_MAX, 1, "flags" },
  67. {"cool", "set cool flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_COOL }, INT_MIN, INT_MAX, 1, "flags" },
  68. {"lame", "set lame flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_LAME }, INT_MIN, INT_MAX, 1, "flags" },
  69. {"mu", "set mu flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_MU }, INT_MIN, INT_MAX, 1, "flags" },
  70. {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, { .str="200x300" }, 0, 0, 1 },
  71. {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, { .i64 = AV_PIX_FMT_0BGR }, -1, INT_MAX, 1 },
  72. {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, { .i64 = AV_SAMPLE_FMT_S16 }, -1, INT_MAX, 1 },
  73. {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, INT_MAX, 1 },
  74. {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, { .i64 = 1000 }, 0, INT64_MAX, 1 },
  75. {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, { .str = "pink" }, 0, 0, 1 },
  76. {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, { .i64 = AV_CH_LAYOUT_HEXAGONAL }, 0, INT64_MAX, 1 },
  77. {"bin", "set binary value", OFFSET(binary), AV_OPT_TYPE_BINARY, { .str="62696e00" }, 0, 0, 1 },
  78. {"bin1", "set binary value", OFFSET(binary1), AV_OPT_TYPE_BINARY, { .str=NULL }, 0, 0, 1 },
  79. {"bin2", "set binary value", OFFSET(binary2), AV_OPT_TYPE_BINARY, { .str="" }, 0, 0, 1 },
  80. {"num64", "set num 64bit", OFFSET(num64), AV_OPT_TYPE_INT64, { .i64 = 1 }, 0, 100, 1 },
  81. {"flt", "set float", OFFSET(flt), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 / 3 }, 0, 100, 1 },
  82. {"dbl", "set double", OFFSET(dbl), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 / 3 }, 0, 100, 1 },
  83. {"bool1", "set boolean value", OFFSET(bool1), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, 1 },
  84. {"bool2", "set boolean value", OFFSET(bool2), AV_OPT_TYPE_BOOL, { .i64 = 1 }, -1, 1, 1 },
  85. {"bool3", "set boolean value", OFFSET(bool3), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, 1 },
  86. { NULL },
  87. };
  88. static const char *test_get_name(void *ctx)
  89. {
  90. return "test";
  91. }
  92. static const AVClass test_class = {
  93. .class_name = "TestContext",
  94. .item_name = test_get_name,
  95. .option = test_options,
  96. };
  97. static void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
  98. {
  99. vfprintf(stdout, fmt, vl);
  100. }
  101. int main(void)
  102. {
  103. int i;
  104. av_log_set_level(AV_LOG_DEBUG);
  105. av_log_set_callback(log_callback_help);
  106. printf("Testing default values\n");
  107. {
  108. TestContext test_ctx = { 0 };
  109. test_ctx.class = &test_class;
  110. av_opt_set_defaults(&test_ctx);
  111. printf("num=%d\n", test_ctx.num);
  112. printf("toggle=%d\n", test_ctx.toggle);
  113. printf("string=%s\n", test_ctx.string);
  114. printf("escape=%s\n", test_ctx.escape);
  115. printf("flags=%d\n", test_ctx.flags);
  116. printf("rational=%d/%d\n", test_ctx.rational.num, test_ctx.rational.den);
  117. printf("video_rate=%d/%d\n", test_ctx.video_rate.num, test_ctx.video_rate.den);
  118. printf("width=%d height=%d\n", test_ctx.w, test_ctx.h);
  119. printf("pix_fmt=%s\n", av_get_pix_fmt_name(test_ctx.pix_fmt));
  120. printf("sample_fmt=%s\n", av_get_sample_fmt_name(test_ctx.sample_fmt));
  121. printf("duration=%"PRId64"\n", test_ctx.duration);
  122. printf("color=%d %d %d %d\n", test_ctx.color[0], test_ctx.color[1], test_ctx.color[2], test_ctx.color[3]);
  123. printf("channel_layout=%"PRId64"=%"PRId64"\n", test_ctx.channel_layout, (int64_t)AV_CH_LAYOUT_HEXAGONAL);
  124. if (test_ctx.binary)
  125. printf("binary=%x %x %x %x\n", ((uint8_t*)test_ctx.binary)[0], ((uint8_t*)test_ctx.binary)[1], ((uint8_t*)test_ctx.binary)[2], ((uint8_t*)test_ctx.binary)[3]);
  126. printf("binary_size=%d\n", test_ctx.binary_size);
  127. printf("num64=%"PRId64"\n", test_ctx.num64);
  128. printf("flt=%.6f\n", test_ctx.flt);
  129. printf("dbl=%.6f\n", test_ctx.dbl);
  130. av_opt_show2(&test_ctx, NULL, -1, 0);
  131. av_opt_free(&test_ctx);
  132. }
  133. printf("\nTesting av_opt_is_set_to_default()\n");
  134. {
  135. int ret;
  136. TestContext test_ctx = { 0 };
  137. const AVOption *o = NULL;
  138. test_ctx.class = &test_class;
  139. av_log_set_level(AV_LOG_QUIET);
  140. while (o = av_opt_next(&test_ctx, o)) {
  141. ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
  142. printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
  143. }
  144. av_opt_set_defaults(&test_ctx);
  145. while (o = av_opt_next(&test_ctx, o)) {
  146. ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
  147. printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
  148. }
  149. av_opt_free(&test_ctx);
  150. }
  151. printf("\nTest av_opt_serialize()\n");
  152. {
  153. TestContext test_ctx = { 0 };
  154. char *buf;
  155. test_ctx.class = &test_class;
  156. av_log_set_level(AV_LOG_QUIET);
  157. av_opt_set_defaults(&test_ctx);
  158. if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
  159. printf("%s\n", buf);
  160. av_opt_free(&test_ctx);
  161. memset(&test_ctx, 0, sizeof(test_ctx));
  162. test_ctx.class = &test_class;
  163. av_set_options_string(&test_ctx, buf, "=", ",");
  164. av_free(buf);
  165. if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
  166. printf("%s\n", buf);
  167. av_free(buf);
  168. }
  169. }
  170. av_opt_free(&test_ctx);
  171. }
  172. printf("\nTesting av_set_options_string()\n");
  173. {
  174. TestContext test_ctx = { 0 };
  175. static const char * const options[] = {
  176. "",
  177. ":",
  178. "=",
  179. "foo=:",
  180. ":=foo",
  181. "=foo",
  182. "foo=",
  183. "foo",
  184. "foo=val",
  185. "foo==val",
  186. "toggle=:",
  187. "string=:",
  188. "toggle=1 : foo",
  189. "toggle=100",
  190. "toggle==1",
  191. "flags=+mu-lame : num=42: toggle=0",
  192. "num=42 : string=blahblah",
  193. "rational=0 : rational=1/2 : rational=1/-1",
  194. "rational=-1/0",
  195. "size=1024x768",
  196. "size=pal",
  197. "size=bogus",
  198. "pix_fmt=yuv420p",
  199. "pix_fmt=2",
  200. "pix_fmt=bogus",
  201. "sample_fmt=s16",
  202. "sample_fmt=2",
  203. "sample_fmt=bogus",
  204. "video_rate=pal",
  205. "video_rate=25",
  206. "video_rate=30000/1001",
  207. "video_rate=30/1.001",
  208. "video_rate=bogus",
  209. "duration=bogus",
  210. "duration=123.45",
  211. "duration=1\\:23\\:45.67",
  212. "color=blue",
  213. "color=0x223300",
  214. "color=0x42FF07AA",
  215. "cl=stereo+downmix",
  216. "cl=foo",
  217. "bin=boguss",
  218. "bin=111",
  219. "bin=ffff",
  220. "num64=bogus",
  221. "num64=44",
  222. "num64=44.4",
  223. "num64=-1",
  224. "num64=101",
  225. "flt=bogus",
  226. "flt=2",
  227. "flt=2.2",
  228. "flt=-1",
  229. "flt=101",
  230. "dbl=bogus",
  231. "dbl=2",
  232. "dbl=2.2",
  233. "dbl=-1",
  234. "dbl=101",
  235. "bool1=true",
  236. "bool2=auto",
  237. };
  238. test_ctx.class = &test_class;
  239. av_opt_set_defaults(&test_ctx);
  240. av_log_set_level(AV_LOG_QUIET);
  241. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  242. int silence_log = !strcmp(options[i], "rational=-1/0"); // inf formating differs between platforms
  243. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  244. if (silence_log)
  245. av_log_set_callback(NULL);
  246. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  247. printf("Error '%s'\n", options[i]);
  248. else
  249. printf("OK '%s'\n", options[i]);
  250. av_log_set_callback(log_callback_help);
  251. }
  252. av_opt_free(&test_ctx);
  253. }
  254. printf("\nTesting av_opt_set_from_string()\n");
  255. {
  256. TestContext test_ctx = { 0 };
  257. static const char * const options[] = {
  258. "",
  259. "5",
  260. "5:hello",
  261. "5:hello:size=pal",
  262. "5:size=pal:hello",
  263. ":",
  264. "=",
  265. " 5 : hello : size = pal ",
  266. "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
  267. };
  268. static const char * const shorthand[] = { "num", "string", NULL };
  269. test_ctx.class = &test_class;
  270. av_opt_set_defaults(&test_ctx);
  271. av_log_set_level(AV_LOG_QUIET);
  272. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  273. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  274. if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
  275. printf("Error '%s'\n", options[i]);
  276. else
  277. printf("OK '%s'\n", options[i]);
  278. }
  279. av_opt_free(&test_ctx);
  280. }
  281. return 0;
  282. }