You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

359 lines
14KB

  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. AVDictionary *dict1;
  56. AVDictionary *dict2;
  57. } TestContext;
  58. #define OFFSET(x) offsetof(TestContext, x)
  59. #define TEST_FLAG_COOL 01
  60. #define TEST_FLAG_LAME 02
  61. #define TEST_FLAG_MU 04
  62. static const AVOption test_options[]= {
  63. {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, 1 },
  64. {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, 1 },
  65. {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, 10, 1 },
  66. {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, { .str = "default" }, CHAR_MIN, CHAR_MAX, 1 },
  67. {"escape", "set escape str", OFFSET(escape), AV_OPT_TYPE_STRING, { .str = "\\=," }, CHAR_MIN, CHAR_MAX, 1 },
  68. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, { .i64 = 1 }, 0, INT_MAX, 1, "flags" },
  69. {"cool", "set cool flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_COOL }, INT_MIN, INT_MAX, 1, "flags" },
  70. {"lame", "set lame flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_LAME }, INT_MIN, INT_MAX, 1, "flags" },
  71. {"mu", "set mu flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_MU }, INT_MIN, INT_MAX, 1, "flags" },
  72. {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, { .str="200x300" }, 0, 0, 1 },
  73. {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, { .i64 = AV_PIX_FMT_0BGR }, -1, INT_MAX, 1 },
  74. {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, { .i64 = AV_SAMPLE_FMT_S16 }, -1, INT_MAX, 1 },
  75. {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, INT_MAX, 1 },
  76. {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, { .i64 = 1000 }, 0, INT64_MAX, 1 },
  77. {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, { .str = "pink" }, 0, 0, 1 },
  78. {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, { .i64 = AV_CH_LAYOUT_HEXAGONAL }, 0, INT64_MAX, 1 },
  79. {"bin", "set binary value", OFFSET(binary), AV_OPT_TYPE_BINARY, { .str="62696e00" }, 0, 0, 1 },
  80. {"bin1", "set binary value", OFFSET(binary1), AV_OPT_TYPE_BINARY, { .str=NULL }, 0, 0, 1 },
  81. {"bin2", "set binary value", OFFSET(binary2), AV_OPT_TYPE_BINARY, { .str="" }, 0, 0, 1 },
  82. {"num64", "set num 64bit", OFFSET(num64), AV_OPT_TYPE_INT64, { .i64 = 1 }, 0, 100, 1 },
  83. {"flt", "set float", OFFSET(flt), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 / 3 }, 0, 100, 1 },
  84. {"dbl", "set double", OFFSET(dbl), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 / 3 }, 0, 100, 1 },
  85. {"bool1", "set boolean value", OFFSET(bool1), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, 1 },
  86. {"bool2", "set boolean value", OFFSET(bool2), AV_OPT_TYPE_BOOL, { .i64 = 1 }, -1, 1, 1 },
  87. {"bool3", "set boolean value", OFFSET(bool3), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, 1 },
  88. {"dict1", "set dictionary value", OFFSET(dict1), AV_OPT_TYPE_DICT, { .str = NULL}, 0, 0, 1 },
  89. {"dict2", "set dictionary value", OFFSET(dict2), AV_OPT_TYPE_DICT, { .str = "happy=':-)'"}, 0, 0, 1 },
  90. { NULL },
  91. };
  92. static const char *test_get_name(void *ctx)
  93. {
  94. return "test";
  95. }
  96. static const AVClass test_class = {
  97. .class_name = "TestContext",
  98. .item_name = test_get_name,
  99. .option = test_options,
  100. };
  101. static void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
  102. {
  103. vfprintf(stdout, fmt, vl);
  104. }
  105. int main(void)
  106. {
  107. int i;
  108. av_log_set_level(AV_LOG_DEBUG);
  109. av_log_set_callback(log_callback_help);
  110. printf("Testing default values\n");
  111. {
  112. TestContext test_ctx = { 0 };
  113. test_ctx.class = &test_class;
  114. av_opt_set_defaults(&test_ctx);
  115. printf("num=%d\n", test_ctx.num);
  116. printf("toggle=%d\n", test_ctx.toggle);
  117. printf("string=%s\n", test_ctx.string);
  118. printf("escape=%s\n", test_ctx.escape);
  119. printf("flags=%d\n", test_ctx.flags);
  120. printf("rational=%d/%d\n", test_ctx.rational.num, test_ctx.rational.den);
  121. printf("video_rate=%d/%d\n", test_ctx.video_rate.num, test_ctx.video_rate.den);
  122. printf("width=%d height=%d\n", test_ctx.w, test_ctx.h);
  123. printf("pix_fmt=%s\n", av_get_pix_fmt_name(test_ctx.pix_fmt));
  124. printf("sample_fmt=%s\n", av_get_sample_fmt_name(test_ctx.sample_fmt));
  125. printf("duration=%"PRId64"\n", test_ctx.duration);
  126. printf("color=%d %d %d %d\n", test_ctx.color[0], test_ctx.color[1], test_ctx.color[2], test_ctx.color[3]);
  127. printf("channel_layout=%"PRId64"=%"PRId64"\n", test_ctx.channel_layout, (int64_t)AV_CH_LAYOUT_HEXAGONAL);
  128. if (test_ctx.binary)
  129. 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]);
  130. printf("binary_size=%d\n", test_ctx.binary_size);
  131. printf("num64=%"PRId64"\n", test_ctx.num64);
  132. printf("flt=%.6f\n", test_ctx.flt);
  133. printf("dbl=%.6f\n", test_ctx.dbl);
  134. av_opt_show2(&test_ctx, NULL, -1, 0);
  135. av_opt_free(&test_ctx);
  136. }
  137. printf("\nTesting av_opt_is_set_to_default()\n");
  138. {
  139. int ret;
  140. TestContext test_ctx = { 0 };
  141. const AVOption *o = NULL;
  142. test_ctx.class = &test_class;
  143. av_log_set_level(AV_LOG_QUIET);
  144. while (o = av_opt_next(&test_ctx, o)) {
  145. ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
  146. printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
  147. }
  148. av_opt_set_defaults(&test_ctx);
  149. while (o = av_opt_next(&test_ctx, o)) {
  150. ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
  151. printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
  152. }
  153. av_opt_free(&test_ctx);
  154. }
  155. printf("\nTesting av_opt_get/av_opt_set()\n");
  156. {
  157. TestContext test_ctx = { 0 };
  158. TestContext test2_ctx = { 0 };
  159. const AVOption *o = NULL;
  160. test_ctx.class = &test_class;
  161. test2_ctx.class = &test_class;
  162. av_log_set_level(AV_LOG_QUIET);
  163. av_opt_set_defaults(&test_ctx);
  164. while (o = av_opt_next(&test_ctx, o)) {
  165. char *value1 = NULL;
  166. char *value2 = NULL;
  167. int ret1 = AVERROR_BUG;
  168. int ret2 = AVERROR_BUG;
  169. int ret3 = AVERROR_BUG;
  170. if (o->type == AV_OPT_TYPE_CONST)
  171. continue;
  172. ret1 = av_opt_get(&test_ctx, o->name, 0, (uint8_t **)&value1);
  173. if (ret1 >= 0) {
  174. ret2 = av_opt_set(&test2_ctx, o->name, value1, 0);
  175. if (ret2 >= 0)
  176. ret3 = av_opt_get(&test2_ctx, o->name, 0, (uint8_t **)&value2);
  177. }
  178. printf("name: %-11s get: %-16s set: %-16s get: %-16s %s\n", o->name,
  179. ret1 >= 0 ? value1 : av_err2str(ret1),
  180. ret2 >= 0 ? "OK" : av_err2str(ret2),
  181. ret3 >= 0 ? value2 : av_err2str(ret3),
  182. ret1 >= 0 && ret2 >= 0 && ret3 >= 0 && !strcmp(value1, value2) ? "OK" : "Mismatch");
  183. av_free(value1);
  184. av_free(value2);
  185. }
  186. av_opt_free(&test_ctx);
  187. av_opt_free(&test2_ctx);
  188. }
  189. printf("\nTest av_opt_serialize()\n");
  190. {
  191. TestContext test_ctx = { 0 };
  192. char *buf;
  193. test_ctx.class = &test_class;
  194. av_log_set_level(AV_LOG_QUIET);
  195. av_opt_set_defaults(&test_ctx);
  196. if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
  197. printf("%s\n", buf);
  198. av_opt_free(&test_ctx);
  199. memset(&test_ctx, 0, sizeof(test_ctx));
  200. test_ctx.class = &test_class;
  201. av_set_options_string(&test_ctx, buf, "=", ",");
  202. av_free(buf);
  203. if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
  204. printf("%s\n", buf);
  205. av_free(buf);
  206. }
  207. }
  208. av_opt_free(&test_ctx);
  209. }
  210. printf("\nTesting av_set_options_string()\n");
  211. {
  212. TestContext test_ctx = { 0 };
  213. static const char * const options[] = {
  214. "",
  215. ":",
  216. "=",
  217. "foo=:",
  218. ":=foo",
  219. "=foo",
  220. "foo=",
  221. "foo",
  222. "foo=val",
  223. "foo==val",
  224. "toggle=:",
  225. "string=:",
  226. "toggle=1 : foo",
  227. "toggle=100",
  228. "toggle==1",
  229. "flags=+mu-lame : num=42: toggle=0",
  230. "num=42 : string=blahblah",
  231. "rational=0 : rational=1/2 : rational=1/-1",
  232. "rational=-1/0",
  233. "size=1024x768",
  234. "size=pal",
  235. "size=bogus",
  236. "pix_fmt=yuv420p",
  237. "pix_fmt=2",
  238. "pix_fmt=bogus",
  239. "sample_fmt=s16",
  240. "sample_fmt=2",
  241. "sample_fmt=bogus",
  242. "video_rate=pal",
  243. "video_rate=25",
  244. "video_rate=30000/1001",
  245. "video_rate=30/1.001",
  246. "video_rate=bogus",
  247. "duration=bogus",
  248. "duration=123.45",
  249. "duration=1\\:23\\:45.67",
  250. "color=blue",
  251. "color=0x223300",
  252. "color=0x42FF07AA",
  253. "cl=stereo+downmix",
  254. "cl=foo",
  255. "bin=boguss",
  256. "bin=111",
  257. "bin=ffff",
  258. "num64=bogus",
  259. "num64=44",
  260. "num64=44.4",
  261. "num64=-1",
  262. "num64=101",
  263. "flt=bogus",
  264. "flt=2",
  265. "flt=2.2",
  266. "flt=-1",
  267. "flt=101",
  268. "dbl=bogus",
  269. "dbl=2",
  270. "dbl=2.2",
  271. "dbl=-1",
  272. "dbl=101",
  273. "bool1=true",
  274. "bool2=auto",
  275. "dict1='happy=\\:-):sad=\\:-('",
  276. };
  277. test_ctx.class = &test_class;
  278. av_opt_set_defaults(&test_ctx);
  279. av_log_set_level(AV_LOG_QUIET);
  280. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  281. int silence_log = !strcmp(options[i], "rational=-1/0"); // inf formating differs between platforms
  282. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  283. if (silence_log)
  284. av_log_set_callback(NULL);
  285. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  286. printf("Error '%s'\n", options[i]);
  287. else
  288. printf("OK '%s'\n", options[i]);
  289. av_log_set_callback(log_callback_help);
  290. }
  291. av_opt_free(&test_ctx);
  292. }
  293. printf("\nTesting av_opt_set_from_string()\n");
  294. {
  295. TestContext test_ctx = { 0 };
  296. static const char * const options[] = {
  297. "",
  298. "5",
  299. "5:hello",
  300. "5:hello:size=pal",
  301. "5:size=pal:hello",
  302. ":",
  303. "=",
  304. " 5 : hello : size = pal ",
  305. "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
  306. };
  307. static const char * const shorthand[] = { "num", "string", NULL };
  308. test_ctx.class = &test_class;
  309. av_opt_set_defaults(&test_ctx);
  310. av_log_set_level(AV_LOG_QUIET);
  311. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  312. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  313. if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
  314. printf("Error '%s'\n", options[i]);
  315. else
  316. printf("OK '%s'\n", options[i]);
  317. }
  318. av_opt_free(&test_ctx);
  319. }
  320. return 0;
  321. }