opt.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853
  1. /*
  2. * AVOptions
  3. * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
  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. * @file
  23. * AVOptions
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "avutil.h"
  27. #include "avstring.h"
  28. #include "channel_layout.h"
  29. #include "common.h"
  30. #include "opt.h"
  31. #include "eval.h"
  32. #include "dict.h"
  33. #include "log.h"
  34. #include "parseutils.h"
  35. #include "pixdesc.h"
  36. #include "mathematics.h"
  37. #include "samplefmt.h"
  38. #include <float.h>
  39. #if FF_API_FIND_OPT
  40. //FIXME order them and do a bin search
  41. const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
  42. {
  43. const AVOption *o = NULL;
  44. while ((o = av_next_option(v, o))) {
  45. if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
  46. return o;
  47. }
  48. return NULL;
  49. }
  50. #endif
  51. #if FF_API_OLD_AVOPTIONS
  52. const AVOption *av_next_option(void *obj, const AVOption *last)
  53. {
  54. return av_opt_next(obj, last);
  55. }
  56. #endif
  57. const AVOption *av_opt_next(void *obj, const AVOption *last)
  58. {
  59. AVClass *class = *(AVClass**)obj;
  60. if (!last && class && class->option && class->option[0].name)
  61. return class->option;
  62. if (last && last[1].name)
  63. return ++last;
  64. return NULL;
  65. }
  66. static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
  67. {
  68. switch (o->type) {
  69. case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
  70. case AV_OPT_TYPE_PIXEL_FMT:
  71. case AV_OPT_TYPE_SAMPLE_FMT:
  72. case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
  73. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  74. case AV_OPT_TYPE_DURATION:
  75. case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
  76. case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
  77. case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
  78. case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
  79. *den = ((AVRational*)dst)->den;
  80. return 0;
  81. case AV_OPT_TYPE_CONST: *num = o->default_val.dbl; return 0;
  82. }
  83. return AVERROR(EINVAL);
  84. }
  85. static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
  86. {
  87. if (o->type != AV_OPT_TYPE_FLAGS &&
  88. (o->max * den < num * intnum || o->min * den > num * intnum)) {
  89. av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
  90. num*intnum/den, o->name, o->min, o->max);
  91. return AVERROR(ERANGE);
  92. }
  93. if (o->type == AV_OPT_TYPE_FLAGS) {
  94. double d = num*intnum/den;
  95. if (d < -1.5 || d > 0xFFFFFFFF+0.5 || (llrint(d*256) & 255)) {
  96. av_log(obj, AV_LOG_ERROR,
  97. "Value %f for parameter '%s' is not a valid set of 32bit integer flags\n",
  98. num*intnum/den, o->name);
  99. return AVERROR(ERANGE);
  100. }
  101. }
  102. switch (o->type) {
  103. case AV_OPT_TYPE_FLAGS:
  104. case AV_OPT_TYPE_PIXEL_FMT:
  105. case AV_OPT_TYPE_SAMPLE_FMT:
  106. case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
  107. case AV_OPT_TYPE_DURATION:
  108. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  109. case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
  110. case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
  111. case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
  112. case AV_OPT_TYPE_RATIONAL:
  113. if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
  114. else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
  115. break;
  116. default:
  117. return AVERROR(EINVAL);
  118. }
  119. return 0;
  120. }
  121. static const double const_values[] = {
  122. M_PI,
  123. M_E,
  124. FF_QP2LAMBDA,
  125. 0
  126. };
  127. static const char * const const_names[] = {
  128. "PI",
  129. "E",
  130. "QP2LAMBDA",
  131. 0
  132. };
  133. static int hexchar2int(char c) {
  134. if (c >= '0' && c <= '9') return c - '0';
  135. if (c >= 'a' && c <= 'f') return c - 'a' + 10;
  136. if (c >= 'A' && c <= 'F') return c - 'A' + 10;
  137. return -1;
  138. }
  139. static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
  140. {
  141. int *lendst = (int *)(dst + 1);
  142. uint8_t *bin, *ptr;
  143. int len = strlen(val);
  144. av_freep(dst);
  145. *lendst = 0;
  146. if (len & 1)
  147. return AVERROR(EINVAL);
  148. len /= 2;
  149. ptr = bin = av_malloc(len);
  150. while (*val) {
  151. int a = hexchar2int(*val++);
  152. int b = hexchar2int(*val++);
  153. if (a < 0 || b < 0) {
  154. av_free(bin);
  155. return AVERROR(EINVAL);
  156. }
  157. *ptr++ = (a << 4) | b;
  158. }
  159. *dst = bin;
  160. *lendst = len;
  161. return 0;
  162. }
  163. static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
  164. {
  165. av_freep(dst);
  166. *dst = av_strdup(val);
  167. return 0;
  168. }
  169. #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
  170. opt->type == AV_OPT_TYPE_CONST || \
  171. opt->type == AV_OPT_TYPE_FLAGS || \
  172. opt->type == AV_OPT_TYPE_INT) ? \
  173. opt->default_val.i64 : opt->default_val.dbl)
  174. static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
  175. {
  176. int ret = 0, notfirst = 0;
  177. int num, den;
  178. char c;
  179. if (sscanf(val, "%d%*1[:/]%d%c", &num, &den, &c) == 2) {
  180. if ((ret = write_number(obj, o, dst, 1, den, num)) >= 0)
  181. return ret;
  182. ret = 0;
  183. }
  184. for (;;) {
  185. int i, den = 1;
  186. char buf[256];
  187. int cmd = 0;
  188. double d, num = 1;
  189. int64_t intnum = 1;
  190. i = 0;
  191. if (*val == '+' || *val == '-') {
  192. if (o->type == AV_OPT_TYPE_FLAGS)
  193. cmd = *(val++);
  194. else if (!notfirst)
  195. buf[i++] = *val;
  196. }
  197. for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
  198. buf[i] = val[i];
  199. buf[i] = 0;
  200. {
  201. const AVOption *o_named = av_opt_find(target_obj, buf, o->unit, 0, 0);
  202. if (o_named && o_named->type == AV_OPT_TYPE_CONST)
  203. d = DEFAULT_NUMVAL(o_named);
  204. else if (!strcmp(buf, "default")) d = DEFAULT_NUMVAL(o);
  205. else if (!strcmp(buf, "max" )) d = o->max;
  206. else if (!strcmp(buf, "min" )) d = o->min;
  207. else if (!strcmp(buf, "none" )) d = 0;
  208. else if (!strcmp(buf, "all" )) d = ~0;
  209. else {
  210. int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
  211. if (res < 0) {
  212. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
  213. return res;
  214. }
  215. }
  216. }
  217. if (o->type == AV_OPT_TYPE_FLAGS) {
  218. read_number(o, dst, NULL, NULL, &intnum);
  219. if (cmd == '+') d = intnum | (int64_t)d;
  220. else if (cmd == '-') d = intnum &~(int64_t)d;
  221. } else {
  222. read_number(o, dst, &num, &den, &intnum);
  223. if (cmd == '+') d = notfirst*num*intnum/den + d;
  224. else if (cmd == '-') d = notfirst*num*intnum/den - d;
  225. }
  226. if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
  227. return ret;
  228. val += i;
  229. if (!*val)
  230. return 0;
  231. notfirst = 1;
  232. }
  233. return 0;
  234. }
  235. static int set_string_image_size(void *obj, const AVOption *o, const char *val, int *dst)
  236. {
  237. int ret;
  238. if (!val || !strcmp(val, "none")) {
  239. dst[0] =
  240. dst[1] = 0;
  241. return 0;
  242. }
  243. ret = av_parse_video_size(dst, dst + 1, val);
  244. if (ret < 0)
  245. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
  246. return ret;
  247. }
  248. static int set_string_video_rate(void *obj, const AVOption *o, const char *val, AVRational *dst)
  249. {
  250. int ret;
  251. if (!val) {
  252. ret = AVERROR(EINVAL);
  253. } else {
  254. ret = av_parse_video_rate(dst, val);
  255. }
  256. if (ret < 0)
  257. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as video rate\n", val);
  258. return ret;
  259. }
  260. static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst)
  261. {
  262. int ret;
  263. if (!val) {
  264. return 0;
  265. } else {
  266. ret = av_parse_color(dst, val, -1, obj);
  267. if (ret < 0)
  268. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as color\n", val);
  269. return ret;
  270. }
  271. return 0;
  272. }
  273. static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst,
  274. int fmt_nb, int ((*get_fmt)(const char *)), const char *desc)
  275. {
  276. int fmt, min, max;
  277. if (!val || !strcmp(val, "none")) {
  278. fmt = -1;
  279. } else {
  280. fmt = get_fmt(val);
  281. if (fmt == -1) {
  282. char *tail;
  283. fmt = strtol(val, &tail, 0);
  284. if (*tail || (unsigned)fmt >= fmt_nb) {
  285. av_log(obj, AV_LOG_ERROR,
  286. "Unable to parse option value \"%s\" as %s\n", val, desc);
  287. return AVERROR(EINVAL);
  288. }
  289. }
  290. }
  291. min = FFMAX(o->min, -1);
  292. max = FFMIN(o->max, fmt_nb-1);
  293. // hack for compatibility with old ffmpeg
  294. if(min == 0 && max == 0) {
  295. min = -1;
  296. max = fmt_nb-1;
  297. }
  298. if (fmt < min || fmt > max) {
  299. av_log(obj, AV_LOG_ERROR,
  300. "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
  301. fmt, o->name, desc, min, max);
  302. return AVERROR(ERANGE);
  303. }
  304. *(int *)dst = fmt;
  305. return 0;
  306. }
  307. static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
  308. {
  309. return set_string_fmt(obj, o, val, dst,
  310. AV_PIX_FMT_NB, av_get_pix_fmt, "pixel format");
  311. }
  312. static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
  313. {
  314. return set_string_fmt(obj, o, val, dst,
  315. AV_SAMPLE_FMT_NB, av_get_sample_fmt, "sample format");
  316. }
  317. #if FF_API_OLD_AVOPTIONS
  318. int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
  319. {
  320. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  321. if (o_out)
  322. *o_out = o;
  323. return av_opt_set(obj, name, val, 0);
  324. }
  325. #endif
  326. int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
  327. {
  328. int ret = 0;
  329. void *dst, *target_obj;
  330. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  331. if (!o || !target_obj)
  332. return AVERROR_OPTION_NOT_FOUND;
  333. if (!val && (o->type != AV_OPT_TYPE_STRING &&
  334. o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
  335. o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_VIDEO_RATE &&
  336. o->type != AV_OPT_TYPE_DURATION && o->type != AV_OPT_TYPE_COLOR &&
  337. o->type != AV_OPT_TYPE_CHANNEL_LAYOUT))
  338. return AVERROR(EINVAL);
  339. if (o->flags & AV_OPT_FLAG_READONLY)
  340. return AVERROR(EINVAL);
  341. dst = ((uint8_t*)target_obj) + o->offset;
  342. switch (o->type) {
  343. case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
  344. case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
  345. case AV_OPT_TYPE_FLAGS:
  346. case AV_OPT_TYPE_INT:
  347. case AV_OPT_TYPE_INT64:
  348. case AV_OPT_TYPE_FLOAT:
  349. case AV_OPT_TYPE_DOUBLE:
  350. case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, target_obj, o, val, dst);
  351. case AV_OPT_TYPE_IMAGE_SIZE: return set_string_image_size(obj, o, val, dst);
  352. case AV_OPT_TYPE_VIDEO_RATE: return set_string_video_rate(obj, o, val, dst);
  353. case AV_OPT_TYPE_PIXEL_FMT: return set_string_pixel_fmt(obj, o, val, dst);
  354. case AV_OPT_TYPE_SAMPLE_FMT: return set_string_sample_fmt(obj, o, val, dst);
  355. case AV_OPT_TYPE_DURATION:
  356. if (!val) {
  357. *(int64_t *)dst = 0;
  358. return 0;
  359. } else {
  360. if ((ret = av_parse_time(dst, val, 1)) < 0)
  361. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
  362. return ret;
  363. }
  364. break;
  365. case AV_OPT_TYPE_COLOR: return set_string_color(obj, o, val, dst);
  366. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  367. if (!val || !strcmp(val, "none")) {
  368. *(int64_t *)dst = 0;
  369. } else {
  370. #if FF_API_GET_CHANNEL_LAYOUT_COMPAT
  371. int64_t cl = ff_get_channel_layout(val, 0);
  372. #else
  373. int64_t cl = av_get_channel_layout(val);
  374. #endif
  375. if (!cl) {
  376. av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as channel layout\n", val);
  377. ret = AVERROR(EINVAL);
  378. }
  379. *(int64_t *)dst = cl;
  380. return ret;
  381. }
  382. break;
  383. }
  384. av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
  385. return AVERROR(EINVAL);
  386. }
  387. #define OPT_EVAL_NUMBER(name, opttype, vartype)\
  388. int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
  389. {\
  390. if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY)\
  391. return AVERROR(EINVAL);\
  392. return set_string_number(obj, obj, o, val, name ## _out);\
  393. }
  394. OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
  395. OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
  396. OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
  397. OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
  398. OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
  399. OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
  400. static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
  401. int search_flags)
  402. {
  403. void *dst, *target_obj;
  404. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  405. if (!o || !target_obj)
  406. return AVERROR_OPTION_NOT_FOUND;
  407. if (o->flags & AV_OPT_FLAG_READONLY)
  408. return AVERROR(EINVAL);
  409. dst = ((uint8_t*)target_obj) + o->offset;
  410. return write_number(obj, o, dst, num, den, intnum);
  411. }
  412. #if FF_API_OLD_AVOPTIONS
  413. const AVOption *av_set_double(void *obj, const char *name, double n)
  414. {
  415. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  416. if (set_number(obj, name, n, 1, 1, 0) < 0)
  417. return NULL;
  418. return o;
  419. }
  420. const AVOption *av_set_q(void *obj, const char *name, AVRational n)
  421. {
  422. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  423. if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
  424. return NULL;
  425. return o;
  426. }
  427. const AVOption *av_set_int(void *obj, const char *name, int64_t n)
  428. {
  429. const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
  430. if (set_number(obj, name, 1, 1, n, 0) < 0)
  431. return NULL;
  432. return o;
  433. }
  434. #endif
  435. int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
  436. {
  437. return set_number(obj, name, 1, 1, val, search_flags);
  438. }
  439. int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
  440. {
  441. return set_number(obj, name, val, 1, 1, search_flags);
  442. }
  443. int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
  444. {
  445. return set_number(obj, name, val.num, val.den, 1, search_flags);
  446. }
  447. int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
  448. {
  449. void *target_obj;
  450. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  451. uint8_t *ptr;
  452. uint8_t **dst;
  453. int *lendst;
  454. if (!o || !target_obj)
  455. return AVERROR_OPTION_NOT_FOUND;
  456. if (o->type != AV_OPT_TYPE_BINARY || o->flags & AV_OPT_FLAG_READONLY)
  457. return AVERROR(EINVAL);
  458. ptr = len ? av_malloc(len) : NULL;
  459. if (len && !ptr)
  460. return AVERROR(ENOMEM);
  461. dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
  462. lendst = (int *)(dst + 1);
  463. av_free(*dst);
  464. *dst = ptr;
  465. *lendst = len;
  466. if (len)
  467. memcpy(ptr, val, len);
  468. return 0;
  469. }
  470. int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
  471. {
  472. void *target_obj;
  473. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  474. if (!o || !target_obj)
  475. return AVERROR_OPTION_NOT_FOUND;
  476. if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
  477. av_log(obj, AV_LOG_ERROR,
  478. "The value set by option '%s' is not an image size.\n", o->name);
  479. return AVERROR(EINVAL);
  480. }
  481. if (w<0 || h<0) {
  482. av_log(obj, AV_LOG_ERROR,
  483. "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
  484. return AVERROR(EINVAL);
  485. }
  486. *(int *)(((uint8_t *)target_obj) + o->offset) = w;
  487. *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
  488. return 0;
  489. }
  490. int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
  491. {
  492. void *target_obj;
  493. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  494. if (!o || !target_obj)
  495. return AVERROR_OPTION_NOT_FOUND;
  496. if (o->type != AV_OPT_TYPE_VIDEO_RATE) {
  497. av_log(obj, AV_LOG_ERROR,
  498. "The value set by option '%s' is not a video rate.\n", o->name);
  499. return AVERROR(EINVAL);
  500. }
  501. if (val.num <= 0 || val.den <= 0)
  502. return AVERROR(EINVAL);
  503. return set_number(obj, name, val.num, val.den, 1, search_flags);
  504. }
  505. static int set_format(void *obj, const char *name, int fmt, int search_flags,
  506. enum AVOptionType type, const char *desc, int nb_fmts)
  507. {
  508. void *target_obj;
  509. const AVOption *o = av_opt_find2(obj, name, NULL, 0,
  510. search_flags, &target_obj);
  511. int min, max;
  512. const AVClass *class = *(AVClass **)obj;
  513. if (!o || !target_obj)
  514. return AVERROR_OPTION_NOT_FOUND;
  515. if (o->type != type) {
  516. av_log(obj, AV_LOG_ERROR,
  517. "The value set by option '%s' is not a %s format", name, desc);
  518. return AVERROR(EINVAL);
  519. }
  520. #if LIBAVUTIL_VERSION_MAJOR < 54
  521. if (class->version && class->version < AV_VERSION_INT(52, 11, 100)) {
  522. min = -1;
  523. max = nb_fmts-1;
  524. } else
  525. #endif
  526. {
  527. min = FFMAX(o->min, -1);
  528. max = FFMIN(o->max, nb_fmts-1);
  529. }
  530. if (fmt < min || fmt > max) {
  531. av_log(obj, AV_LOG_ERROR,
  532. "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
  533. fmt, name, desc, min, max);
  534. return AVERROR(ERANGE);
  535. }
  536. *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
  537. return 0;
  538. }
  539. int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
  540. {
  541. return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
  542. }
  543. int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
  544. {
  545. return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
  546. }
  547. int av_opt_set_channel_layout(void *obj, const char *name, int64_t cl, int search_flags)
  548. {
  549. void *target_obj;
  550. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  551. if (!o || !target_obj)
  552. return AVERROR_OPTION_NOT_FOUND;
  553. if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
  554. av_log(obj, AV_LOG_ERROR,
  555. "The value set by option '%s' is not a channel layout.\n", o->name);
  556. return AVERROR(EINVAL);
  557. }
  558. *(int64_t *)(((uint8_t *)target_obj) + o->offset) = cl;
  559. return 0;
  560. }
  561. #if FF_API_OLD_AVOPTIONS
  562. /**
  563. *
  564. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  565. * @param buf_len allocated length in bytes of buf
  566. */
  567. const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
  568. {
  569. const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
  570. void *dst;
  571. uint8_t *bin;
  572. int len, i;
  573. if (!o)
  574. return NULL;
  575. if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
  576. return NULL;
  577. dst= ((uint8_t*)obj) + o->offset;
  578. if (o_out) *o_out= o;
  579. switch (o->type) {
  580. case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  581. case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  582. case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
  583. case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  584. case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  585. case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  586. case AV_OPT_TYPE_CONST: snprintf(buf, buf_len, "%f" , o->default_val.dbl);break;
  587. case AV_OPT_TYPE_STRING: return *(void**)dst;
  588. case AV_OPT_TYPE_BINARY:
  589. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  590. if (len >= (buf_len + 1)/2) return NULL;
  591. bin = *(uint8_t**)dst;
  592. for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
  593. break;
  594. default: return NULL;
  595. }
  596. return buf;
  597. }
  598. #endif
  599. int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
  600. {
  601. void *dst, *target_obj;
  602. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  603. uint8_t *bin, buf[128];
  604. int len, i, ret;
  605. int64_t i64;
  606. if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
  607. return AVERROR_OPTION_NOT_FOUND;
  608. dst = (uint8_t*)target_obj + o->offset;
  609. buf[0] = 0;
  610. switch (o->type) {
  611. case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
  612. case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
  613. case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
  614. case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
  615. case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
  616. case AV_OPT_TYPE_VIDEO_RATE:
  617. case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  618. case AV_OPT_TYPE_CONST: ret = snprintf(buf, sizeof(buf), "%f" , o->default_val.dbl);break;
  619. case AV_OPT_TYPE_STRING:
  620. if (*(uint8_t**)dst)
  621. *out_val = av_strdup(*(uint8_t**)dst);
  622. else
  623. *out_val = av_strdup("");
  624. return 0;
  625. case AV_OPT_TYPE_BINARY:
  626. len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
  627. if ((uint64_t)len*2 + 1 > INT_MAX)
  628. return AVERROR(EINVAL);
  629. if (!(*out_val = av_malloc(len*2 + 1)))
  630. return AVERROR(ENOMEM);
  631. if (!len) {
  632. *out_val[0] = '\0';
  633. return 0;
  634. }
  635. bin = *(uint8_t**)dst;
  636. for (i = 0; i < len; i++)
  637. snprintf(*out_val + i*2, 3, "%02X", bin[i]);
  638. return 0;
  639. case AV_OPT_TYPE_IMAGE_SIZE:
  640. ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
  641. break;
  642. case AV_OPT_TYPE_PIXEL_FMT:
  643. ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
  644. break;
  645. case AV_OPT_TYPE_SAMPLE_FMT:
  646. ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
  647. break;
  648. case AV_OPT_TYPE_DURATION:
  649. i64 = *(int64_t *)dst;
  650. ret = snprintf(buf, sizeof(buf), "%"PRIi64":%02d:%02d.%06d",
  651. i64 / 3600000000, (int)((i64 / 60000000) % 60),
  652. (int)((i64 / 1000000) % 60), (int)(i64 % 1000000));
  653. break;
  654. case AV_OPT_TYPE_COLOR:
  655. ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x",
  656. (int)((uint8_t *)dst)[0], (int)((uint8_t *)dst)[1],
  657. (int)((uint8_t *)dst)[2], (int)((uint8_t *)dst)[3]);
  658. break;
  659. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  660. i64 = *(int64_t *)dst;
  661. ret = snprintf(buf, sizeof(buf), "0x%"PRIx64, i64);
  662. break;
  663. default:
  664. return AVERROR(EINVAL);
  665. }
  666. if (ret >= sizeof(buf))
  667. return AVERROR(EINVAL);
  668. *out_val = av_strdup(buf);
  669. return 0;
  670. }
  671. static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
  672. int search_flags)
  673. {
  674. void *dst, *target_obj;
  675. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  676. if (!o || !target_obj)
  677. goto error;
  678. dst = ((uint8_t*)target_obj) + o->offset;
  679. if (o_out) *o_out= o;
  680. return read_number(o, dst, num, den, intnum);
  681. error:
  682. *den=*intnum=0;
  683. return -1;
  684. }
  685. #if FF_API_OLD_AVOPTIONS
  686. double av_get_double(void *obj, const char *name, const AVOption **o_out)
  687. {
  688. int64_t intnum=1;
  689. double num=1;
  690. int den=1;
  691. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  692. return NAN;
  693. return num*intnum/den;
  694. }
  695. AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
  696. {
  697. int64_t intnum=1;
  698. double num=1;
  699. int den=1;
  700. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  701. return (AVRational){0, 0};
  702. if (num == 1.0 && (int)intnum == intnum)
  703. return (AVRational){intnum, den};
  704. else
  705. return av_d2q(num*intnum/den, 1<<24);
  706. }
  707. int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
  708. {
  709. int64_t intnum=1;
  710. double num=1;
  711. int den=1;
  712. if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
  713. return -1;
  714. return num*intnum/den;
  715. }
  716. #endif
  717. int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
  718. {
  719. int64_t intnum = 1;
  720. double num = 1;
  721. int ret, den = 1;
  722. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  723. return ret;
  724. *out_val = num*intnum/den;
  725. return 0;
  726. }
  727. int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
  728. {
  729. int64_t intnum = 1;
  730. double num = 1;
  731. int ret, den = 1;
  732. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  733. return ret;
  734. *out_val = num*intnum/den;
  735. return 0;
  736. }
  737. int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
  738. {
  739. int64_t intnum = 1;
  740. double num = 1;
  741. int ret, den = 1;
  742. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  743. return ret;
  744. if (num == 1.0 && (int)intnum == intnum)
  745. *out_val = (AVRational){intnum, den};
  746. else
  747. *out_val = av_d2q(num*intnum/den, 1<<24);
  748. return 0;
  749. }
  750. int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
  751. {
  752. void *dst, *target_obj;
  753. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  754. if (!o || !target_obj)
  755. return AVERROR_OPTION_NOT_FOUND;
  756. if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
  757. av_log(obj, AV_LOG_ERROR,
  758. "The value for option '%s' is not an image size.\n", name);
  759. return AVERROR(EINVAL);
  760. }
  761. dst = ((uint8_t*)target_obj) + o->offset;
  762. if (w_out) *w_out = *(int *)dst;
  763. if (h_out) *h_out = *((int *)dst+1);
  764. return 0;
  765. }
  766. int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
  767. {
  768. int64_t intnum = 1;
  769. double num = 1;
  770. int ret, den = 1;
  771. if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
  772. return ret;
  773. if (num == 1.0 && (int)intnum == intnum)
  774. *out_val = (AVRational){intnum, den};
  775. else
  776. *out_val = av_d2q(num*intnum/den, 1<<24);
  777. return 0;
  778. }
  779. static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
  780. enum AVOptionType type, const char *desc)
  781. {
  782. void *dst, *target_obj;
  783. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  784. if (!o || !target_obj)
  785. return AVERROR_OPTION_NOT_FOUND;
  786. if (o->type != type) {
  787. av_log(obj, AV_LOG_ERROR,
  788. "The value for option '%s' is not a %s format.\n", desc, name);
  789. return AVERROR(EINVAL);
  790. }
  791. dst = ((uint8_t*)target_obj) + o->offset;
  792. *out_fmt = *(int *)dst;
  793. return 0;
  794. }
  795. int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
  796. {
  797. return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
  798. }
  799. int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
  800. {
  801. return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
  802. }
  803. int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *cl)
  804. {
  805. void *dst, *target_obj;
  806. const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
  807. if (!o || !target_obj)
  808. return AVERROR_OPTION_NOT_FOUND;
  809. if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
  810. av_log(obj, AV_LOG_ERROR,
  811. "The value for option '%s' is not a channel layout.\n", name);
  812. return AVERROR(EINVAL);
  813. }
  814. dst = ((uint8_t*)target_obj) + o->offset;
  815. *cl = *(int64_t *)dst;
  816. return 0;
  817. }
  818. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
  819. {
  820. const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
  821. const AVOption *flag = av_opt_find(obj, flag_name,
  822. field ? field->unit : NULL, 0, 0);
  823. int64_t res;
  824. if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
  825. av_opt_get_int(obj, field_name, 0, &res) < 0)
  826. return 0;
  827. return res & flag->default_val.i64;
  828. }
  829. static void log_value(void *av_log_obj, int level, double d)
  830. {
  831. if (d == INT_MAX) {
  832. av_log(av_log_obj, level, "INT_MAX");
  833. } else if (d == INT_MIN) {
  834. av_log(av_log_obj, level, "INT_MIN");
  835. } else if (d == UINT32_MAX) {
  836. av_log(av_log_obj, level, "UINT32_MAX");
  837. } else if (d == (double)INT64_MAX) {
  838. av_log(av_log_obj, level, "I64_MAX");
  839. } else if (d == INT64_MIN) {
  840. av_log(av_log_obj, level, "I64_MIN");
  841. } else if (d == FLT_MAX) {
  842. av_log(av_log_obj, level, "FLT_MAX");
  843. } else if (d == FLT_MIN) {
  844. av_log(av_log_obj, level, "FLT_MIN");
  845. } else if (d == -FLT_MAX) {
  846. av_log(av_log_obj, level, "-FLT_MAX");
  847. } else if (d == -FLT_MIN) {
  848. av_log(av_log_obj, level, "-FLT_MIN");
  849. } else if (d == DBL_MAX) {
  850. av_log(av_log_obj, level, "DBL_MAX");
  851. } else if (d == DBL_MIN) {
  852. av_log(av_log_obj, level, "DBL_MIN");
  853. } else if (d == -DBL_MAX) {
  854. av_log(av_log_obj, level, "-DBL_MAX");
  855. } else if (d == -DBL_MIN) {
  856. av_log(av_log_obj, level, "-DBL_MIN");
  857. } else {
  858. av_log(av_log_obj, level, "%g", d);
  859. }
  860. }
  861. static void opt_list(void *obj, void *av_log_obj, const char *unit,
  862. int req_flags, int rej_flags)
  863. {
  864. const AVOption *opt=NULL;
  865. AVOptionRanges *r;
  866. int i;
  867. while ((opt = av_opt_next(obj, opt))) {
  868. if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
  869. continue;
  870. /* Don't print CONST's on level one.
  871. * Don't print anything but CONST's on level two.
  872. * Only print items from the requested unit.
  873. */
  874. if (!unit && opt->type==AV_OPT_TYPE_CONST)
  875. continue;
  876. else if (unit && opt->type!=AV_OPT_TYPE_CONST)
  877. continue;
  878. else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  879. continue;
  880. else if (unit && opt->type == AV_OPT_TYPE_CONST)
  881. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  882. else
  883. av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
  884. (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
  885. opt->name);
  886. switch (opt->type) {
  887. case AV_OPT_TYPE_FLAGS:
  888. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
  889. break;
  890. case AV_OPT_TYPE_INT:
  891. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
  892. break;
  893. case AV_OPT_TYPE_INT64:
  894. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
  895. break;
  896. case AV_OPT_TYPE_DOUBLE:
  897. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
  898. break;
  899. case AV_OPT_TYPE_FLOAT:
  900. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
  901. break;
  902. case AV_OPT_TYPE_STRING:
  903. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
  904. break;
  905. case AV_OPT_TYPE_RATIONAL:
  906. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
  907. break;
  908. case AV_OPT_TYPE_BINARY:
  909. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
  910. break;
  911. case AV_OPT_TYPE_IMAGE_SIZE:
  912. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
  913. break;
  914. case AV_OPT_TYPE_VIDEO_RATE:
  915. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
  916. break;
  917. case AV_OPT_TYPE_PIXEL_FMT:
  918. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
  919. break;
  920. case AV_OPT_TYPE_SAMPLE_FMT:
  921. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
  922. break;
  923. case AV_OPT_TYPE_DURATION:
  924. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
  925. break;
  926. case AV_OPT_TYPE_COLOR:
  927. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
  928. break;
  929. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  930. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
  931. break;
  932. case AV_OPT_TYPE_CONST:
  933. default:
  934. av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
  935. break;
  936. }
  937. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  938. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  939. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
  940. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  941. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  942. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  943. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.');
  944. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.');
  945. if (opt->help)
  946. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  947. if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
  948. switch (opt->type) {
  949. case AV_OPT_TYPE_INT:
  950. case AV_OPT_TYPE_INT64:
  951. case AV_OPT_TYPE_DOUBLE:
  952. case AV_OPT_TYPE_FLOAT:
  953. case AV_OPT_TYPE_RATIONAL:
  954. for (i = 0; i < r->nb_ranges; i++) {
  955. av_log(av_log_obj, AV_LOG_INFO, " (from ");
  956. log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
  957. av_log(av_log_obj, AV_LOG_INFO, " to ");
  958. log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
  959. av_log(av_log_obj, AV_LOG_INFO, ")");
  960. }
  961. break;
  962. }
  963. av_opt_freep_ranges(&r);
  964. }
  965. if (opt->type != AV_OPT_TYPE_CONST &&
  966. opt->type != AV_OPT_TYPE_BINARY &&
  967. !((opt->type == AV_OPT_TYPE_COLOR ||
  968. opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
  969. opt->type == AV_OPT_TYPE_STRING ||
  970. opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
  971. !opt->default_val.str)) {
  972. av_log(av_log_obj, AV_LOG_INFO, " (default ");
  973. switch (opt->type) {
  974. case AV_OPT_TYPE_FLAGS:
  975. av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
  976. break;
  977. case AV_OPT_TYPE_DURATION:
  978. case AV_OPT_TYPE_INT:
  979. case AV_OPT_TYPE_INT64:
  980. log_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
  981. break;
  982. case AV_OPT_TYPE_DOUBLE:
  983. case AV_OPT_TYPE_FLOAT:
  984. log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
  985. break;
  986. case AV_OPT_TYPE_RATIONAL: {
  987. AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
  988. av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
  989. break;
  990. case AV_OPT_TYPE_PIXEL_FMT:
  991. av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
  992. break;
  993. case AV_OPT_TYPE_SAMPLE_FMT:
  994. av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
  995. break;
  996. case AV_OPT_TYPE_COLOR:
  997. case AV_OPT_TYPE_IMAGE_SIZE:
  998. case AV_OPT_TYPE_STRING:
  999. case AV_OPT_TYPE_VIDEO_RATE:
  1000. av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
  1001. break;
  1002. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  1003. av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
  1004. break;
  1005. }
  1006. av_log(av_log_obj, AV_LOG_INFO, ")");
  1007. }
  1008. av_log(av_log_obj, AV_LOG_INFO, "\n");
  1009. if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
  1010. opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
  1011. }
  1012. }
  1013. }
  1014. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
  1015. {
  1016. if (!obj)
  1017. return -1;
  1018. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  1019. opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
  1020. return 0;
  1021. }
  1022. void av_opt_set_defaults(void *s)
  1023. {
  1024. #if FF_API_OLD_AVOPTIONS
  1025. av_opt_set_defaults2(s, 0, 0);
  1026. }
  1027. void av_opt_set_defaults2(void *s, int mask, int flags)
  1028. {
  1029. #endif
  1030. const AVClass *class = *(AVClass **)s;
  1031. const AVOption *opt = NULL;
  1032. while ((opt = av_opt_next(s, opt)) != NULL) {
  1033. void *dst = ((uint8_t*)s) + opt->offset;
  1034. #if FF_API_OLD_AVOPTIONS
  1035. if ((opt->flags & mask) != flags)
  1036. continue;
  1037. #endif
  1038. if (opt->flags & AV_OPT_FLAG_READONLY)
  1039. continue;
  1040. switch (opt->type) {
  1041. case AV_OPT_TYPE_CONST:
  1042. /* Nothing to be done here */
  1043. break;
  1044. case AV_OPT_TYPE_FLAGS:
  1045. case AV_OPT_TYPE_INT:
  1046. case AV_OPT_TYPE_INT64:
  1047. case AV_OPT_TYPE_DURATION:
  1048. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  1049. write_number(s, opt, dst, 1, 1, opt->default_val.i64);
  1050. break;
  1051. case AV_OPT_TYPE_DOUBLE:
  1052. case AV_OPT_TYPE_FLOAT: {
  1053. double val;
  1054. val = opt->default_val.dbl;
  1055. write_number(s, opt, dst, val, 1, 1);
  1056. }
  1057. break;
  1058. case AV_OPT_TYPE_RATIONAL: {
  1059. AVRational val;
  1060. val = av_d2q(opt->default_val.dbl, INT_MAX);
  1061. write_number(s, opt, dst, 1, val.den, val.num);
  1062. }
  1063. break;
  1064. case AV_OPT_TYPE_COLOR:
  1065. set_string_color(s, opt, opt->default_val.str, dst);
  1066. break;
  1067. case AV_OPT_TYPE_STRING:
  1068. set_string(s, opt, opt->default_val.str, dst);
  1069. break;
  1070. case AV_OPT_TYPE_IMAGE_SIZE:
  1071. set_string_image_size(s, opt, opt->default_val.str, dst);
  1072. break;
  1073. case AV_OPT_TYPE_VIDEO_RATE:
  1074. set_string_video_rate(s, opt, opt->default_val.str, dst);
  1075. break;
  1076. case AV_OPT_TYPE_PIXEL_FMT:
  1077. #if LIBAVUTIL_VERSION_MAJOR < 54
  1078. if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
  1079. av_opt_set(s, opt->name, opt->default_val.str, 0);
  1080. else
  1081. #endif
  1082. write_number(s, opt, dst, 1, 1, opt->default_val.i64);
  1083. break;
  1084. case AV_OPT_TYPE_SAMPLE_FMT:
  1085. #if LIBAVUTIL_VERSION_MAJOR < 54
  1086. if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
  1087. av_opt_set(s, opt->name, opt->default_val.str, 0);
  1088. else
  1089. #endif
  1090. write_number(s, opt, dst, 1, 1, opt->default_val.i64);
  1091. break;
  1092. case AV_OPT_TYPE_BINARY:
  1093. /* Cannot set default for binary */
  1094. break;
  1095. default:
  1096. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  1097. }
  1098. }
  1099. }
  1100. /**
  1101. * Store the value in the field in ctx that is named like key.
  1102. * ctx must be an AVClass context, storing is done using AVOptions.
  1103. *
  1104. * @param buf the string to parse, buf will be updated to point at the
  1105. * separator just after the parsed key/value pair
  1106. * @param key_val_sep a 0-terminated list of characters used to
  1107. * separate key from value
  1108. * @param pairs_sep a 0-terminated list of characters used to separate
  1109. * two pairs from each other
  1110. * @return 0 if the key/value pair has been successfully parsed and
  1111. * set, or a negative value corresponding to an AVERROR code in case
  1112. * of error:
  1113. * AVERROR(EINVAL) if the key/value pair cannot be parsed,
  1114. * the error code issued by av_opt_set() if the key/value pair
  1115. * cannot be set
  1116. */
  1117. static int parse_key_value_pair(void *ctx, const char **buf,
  1118. const char *key_val_sep, const char *pairs_sep)
  1119. {
  1120. char *key = av_get_token(buf, key_val_sep);
  1121. char *val;
  1122. int ret;
  1123. if (!key)
  1124. return AVERROR(ENOMEM);
  1125. if (*key && strspn(*buf, key_val_sep)) {
  1126. (*buf)++;
  1127. val = av_get_token(buf, pairs_sep);
  1128. if (!val) {
  1129. av_freep(&key);
  1130. return AVERROR(ENOMEM);
  1131. }
  1132. } else {
  1133. av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
  1134. av_free(key);
  1135. return AVERROR(EINVAL);
  1136. }
  1137. av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
  1138. ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
  1139. if (ret == AVERROR_OPTION_NOT_FOUND)
  1140. av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
  1141. av_free(key);
  1142. av_free(val);
  1143. return ret;
  1144. }
  1145. int av_set_options_string(void *ctx, const char *opts,
  1146. const char *key_val_sep, const char *pairs_sep)
  1147. {
  1148. int ret, count = 0;
  1149. if (!opts)
  1150. return 0;
  1151. while (*opts) {
  1152. if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
  1153. return ret;
  1154. count++;
  1155. if (*opts)
  1156. opts++;
  1157. }
  1158. return count;
  1159. }
  1160. #define WHITESPACES " \n\t"
  1161. static int is_key_char(char c)
  1162. {
  1163. return (unsigned)((c | 32) - 'a') < 26 ||
  1164. (unsigned)(c - '0') < 10 ||
  1165. c == '-' || c == '_' || c == '/' || c == '.';
  1166. }
  1167. /**
  1168. * Read a key from a string.
  1169. *
  1170. * The key consists of is_key_char characters and must be terminated by a
  1171. * character from the delim string; spaces are ignored.
  1172. *
  1173. * @return 0 for success (even with ellipsis), <0 for failure
  1174. */
  1175. static int get_key(const char **ropts, const char *delim, char **rkey)
  1176. {
  1177. const char *opts = *ropts;
  1178. const char *key_start, *key_end;
  1179. key_start = opts += strspn(opts, WHITESPACES);
  1180. while (is_key_char(*opts))
  1181. opts++;
  1182. key_end = opts;
  1183. opts += strspn(opts, WHITESPACES);
  1184. if (!*opts || !strchr(delim, *opts))
  1185. return AVERROR(EINVAL);
  1186. opts++;
  1187. if (!(*rkey = av_malloc(key_end - key_start + 1)))
  1188. return AVERROR(ENOMEM);
  1189. memcpy(*rkey, key_start, key_end - key_start);
  1190. (*rkey)[key_end - key_start] = 0;
  1191. *ropts = opts;
  1192. return 0;
  1193. }
  1194. int av_opt_get_key_value(const char **ropts,
  1195. const char *key_val_sep, const char *pairs_sep,
  1196. unsigned flags,
  1197. char **rkey, char **rval)
  1198. {
  1199. int ret;
  1200. char *key = NULL, *val;
  1201. const char *opts = *ropts;
  1202. if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
  1203. !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
  1204. return AVERROR(EINVAL);
  1205. if (!(val = av_get_token(&opts, pairs_sep))) {
  1206. av_free(key);
  1207. return AVERROR(ENOMEM);
  1208. }
  1209. *ropts = opts;
  1210. *rkey = key;
  1211. *rval = val;
  1212. return 0;
  1213. }
  1214. int av_opt_set_from_string(void *ctx, const char *opts,
  1215. const char *const *shorthand,
  1216. const char *key_val_sep, const char *pairs_sep)
  1217. {
  1218. int ret, count = 0;
  1219. const char *dummy_shorthand = NULL;
  1220. char *av_uninit(parsed_key), *av_uninit(value);
  1221. const char *key;
  1222. if (!opts)
  1223. return 0;
  1224. if (!shorthand)
  1225. shorthand = &dummy_shorthand;
  1226. while (*opts) {
  1227. ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
  1228. *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
  1229. &parsed_key, &value);
  1230. if (ret < 0) {
  1231. if (ret == AVERROR(EINVAL))
  1232. av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
  1233. else
  1234. av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
  1235. av_err2str(ret));
  1236. return ret;
  1237. }
  1238. if (*opts)
  1239. opts++;
  1240. if (parsed_key) {
  1241. key = parsed_key;
  1242. while (*shorthand) /* discard all remaining shorthand */
  1243. shorthand++;
  1244. } else {
  1245. key = *(shorthand++);
  1246. }
  1247. av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
  1248. if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
  1249. if (ret == AVERROR_OPTION_NOT_FOUND)
  1250. av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
  1251. av_free(value);
  1252. av_free(parsed_key);
  1253. return ret;
  1254. }
  1255. av_free(value);
  1256. av_free(parsed_key);
  1257. count++;
  1258. }
  1259. return count;
  1260. }
  1261. void av_opt_free(void *obj)
  1262. {
  1263. const AVOption *o = NULL;
  1264. while ((o = av_opt_next(obj, o)))
  1265. if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
  1266. av_freep((uint8_t *)obj + o->offset);
  1267. }
  1268. int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
  1269. {
  1270. AVDictionaryEntry *t = NULL;
  1271. AVDictionary *tmp = NULL;
  1272. int ret = 0;
  1273. if (!options)
  1274. return 0;
  1275. while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
  1276. ret = av_opt_set(obj, t->key, t->value, search_flags);
  1277. if (ret == AVERROR_OPTION_NOT_FOUND)
  1278. av_dict_set(&tmp, t->key, t->value, 0);
  1279. else if (ret < 0) {
  1280. av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
  1281. break;
  1282. }
  1283. ret = 0;
  1284. }
  1285. av_dict_free(options);
  1286. *options = tmp;
  1287. return ret;
  1288. }
  1289. int av_opt_set_dict(void *obj, AVDictionary **options)
  1290. {
  1291. return av_opt_set_dict2(obj, options, 0);
  1292. }
  1293. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  1294. int opt_flags, int search_flags)
  1295. {
  1296. return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
  1297. }
  1298. const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
  1299. int opt_flags, int search_flags, void **target_obj)
  1300. {
  1301. const AVClass *c;
  1302. const AVOption *o = NULL;
  1303. if(!obj)
  1304. return NULL;
  1305. c= *(AVClass**)obj;
  1306. if (!c)
  1307. return NULL;
  1308. if (search_flags & AV_OPT_SEARCH_CHILDREN) {
  1309. if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
  1310. const AVClass *child = NULL;
  1311. while (child = av_opt_child_class_next(c, child))
  1312. if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
  1313. return o;
  1314. } else {
  1315. void *child = NULL;
  1316. while (child = av_opt_child_next(obj, child))
  1317. if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
  1318. return o;
  1319. }
  1320. }
  1321. while (o = av_opt_next(obj, o)) {
  1322. if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
  1323. ((!unit && o->type != AV_OPT_TYPE_CONST) ||
  1324. (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
  1325. if (target_obj) {
  1326. if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
  1327. *target_obj = obj;
  1328. else
  1329. *target_obj = NULL;
  1330. }
  1331. return o;
  1332. }
  1333. }
  1334. return NULL;
  1335. }
  1336. void *av_opt_child_next(void *obj, void *prev)
  1337. {
  1338. const AVClass *c = *(AVClass**)obj;
  1339. if (c->child_next)
  1340. return c->child_next(obj, prev);
  1341. return NULL;
  1342. }
  1343. const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
  1344. {
  1345. if (parent->child_class_next)
  1346. return parent->child_class_next(prev);
  1347. return NULL;
  1348. }
  1349. void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
  1350. {
  1351. const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
  1352. if(!opt)
  1353. return NULL;
  1354. return (uint8_t*)obj + opt->offset;
  1355. }
  1356. static int opt_size(enum AVOptionType type)
  1357. {
  1358. switch(type) {
  1359. case AV_OPT_TYPE_INT:
  1360. case AV_OPT_TYPE_FLAGS: return sizeof(int);
  1361. case AV_OPT_TYPE_DURATION:
  1362. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  1363. case AV_OPT_TYPE_INT64: return sizeof(int64_t);
  1364. case AV_OPT_TYPE_DOUBLE: return sizeof(double);
  1365. case AV_OPT_TYPE_FLOAT: return sizeof(float);
  1366. case AV_OPT_TYPE_STRING: return sizeof(uint8_t*);
  1367. case AV_OPT_TYPE_VIDEO_RATE:
  1368. case AV_OPT_TYPE_RATIONAL: return sizeof(AVRational);
  1369. case AV_OPT_TYPE_BINARY: return sizeof(uint8_t*) + sizeof(int);
  1370. case AV_OPT_TYPE_IMAGE_SIZE:return sizeof(int[2]);
  1371. case AV_OPT_TYPE_PIXEL_FMT: return sizeof(enum AVPixelFormat);
  1372. case AV_OPT_TYPE_SAMPLE_FMT:return sizeof(enum AVSampleFormat);
  1373. case AV_OPT_TYPE_COLOR: return 4;
  1374. }
  1375. return 0;
  1376. }
  1377. int av_opt_copy(void *dst, void *src)
  1378. {
  1379. const AVOption *o = NULL;
  1380. const AVClass *c;
  1381. int ret = 0;
  1382. if (!src)
  1383. return 0;
  1384. c = *(AVClass**)src;
  1385. if (*(AVClass**)dst && c != *(AVClass**)dst)
  1386. return AVERROR(EINVAL);
  1387. while ((o = av_opt_next(src, o))) {
  1388. void *field_dst = ((uint8_t*)dst) + o->offset;
  1389. void *field_src = ((uint8_t*)src) + o->offset;
  1390. uint8_t **field_dst8 = (uint8_t**)field_dst;
  1391. uint8_t **field_src8 = (uint8_t**)field_src;
  1392. if (o->type == AV_OPT_TYPE_STRING) {
  1393. set_string(dst, o, *field_src8, field_dst8);
  1394. if (*field_src8 && !*field_dst8)
  1395. ret = AVERROR(ENOMEM);
  1396. } else if (o->type == AV_OPT_TYPE_BINARY) {
  1397. int len = *(int*)(field_src8 + 1);
  1398. if (*field_dst8 != *field_src8)
  1399. av_freep(field_dst8);
  1400. *field_dst8 = av_memdup(*field_src8, len);
  1401. if (len && !*field_dst8) {
  1402. ret = AVERROR(ENOMEM);
  1403. len = 0;
  1404. }
  1405. *(int*)(field_dst8 + 1) = len;
  1406. } else if (o->type == AV_OPT_TYPE_CONST) {
  1407. // do nothing
  1408. } else {
  1409. memcpy(field_dst, field_src, opt_size(o->type));
  1410. }
  1411. }
  1412. return ret;
  1413. }
  1414. int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
  1415. {
  1416. int ret;
  1417. const AVClass *c = *(AVClass**)obj;
  1418. int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
  1419. if (c->version > (52 << 16 | 11 << 8))
  1420. callback = c->query_ranges;
  1421. if (!callback)
  1422. callback = av_opt_query_ranges_default;
  1423. ret = callback(ranges_arg, obj, key, flags);
  1424. if (ret >= 0) {
  1425. if (!(flags & AV_OPT_MULTI_COMPONENT_RANGE))
  1426. ret = 1;
  1427. (*ranges_arg)->nb_components = ret;
  1428. }
  1429. return ret;
  1430. }
  1431. int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
  1432. {
  1433. AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
  1434. AVOptionRange **range_array = av_mallocz(sizeof(void*));
  1435. AVOptionRange *range = av_mallocz(sizeof(*range));
  1436. const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
  1437. int ret;
  1438. *ranges_arg = NULL;
  1439. if (!ranges || !range || !range_array || !field) {
  1440. ret = AVERROR(ENOMEM);
  1441. goto fail;
  1442. }
  1443. ranges->range = range_array;
  1444. ranges->range[0] = range;
  1445. ranges->nb_ranges = 1;
  1446. ranges->nb_components = 1;
  1447. range->is_range = 1;
  1448. range->value_min = field->min;
  1449. range->value_max = field->max;
  1450. switch (field->type) {
  1451. case AV_OPT_TYPE_INT:
  1452. case AV_OPT_TYPE_INT64:
  1453. case AV_OPT_TYPE_PIXEL_FMT:
  1454. case AV_OPT_TYPE_SAMPLE_FMT:
  1455. case AV_OPT_TYPE_FLOAT:
  1456. case AV_OPT_TYPE_DOUBLE:
  1457. case AV_OPT_TYPE_DURATION:
  1458. case AV_OPT_TYPE_COLOR:
  1459. case AV_OPT_TYPE_CHANNEL_LAYOUT:
  1460. break;
  1461. case AV_OPT_TYPE_STRING:
  1462. range->component_min = 0;
  1463. range->component_max = 0x10FFFF; // max unicode value
  1464. range->value_min = -1;
  1465. range->value_max = INT_MAX;
  1466. break;
  1467. case AV_OPT_TYPE_RATIONAL:
  1468. range->component_min = INT_MIN;
  1469. range->component_max = INT_MAX;
  1470. break;
  1471. case AV_OPT_TYPE_IMAGE_SIZE:
  1472. range->component_min = 0;
  1473. range->component_max = INT_MAX/128/8;
  1474. range->value_min = 0;
  1475. range->value_max = INT_MAX/8;
  1476. break;
  1477. case AV_OPT_TYPE_VIDEO_RATE:
  1478. range->component_min = 1;
  1479. range->component_max = INT_MAX;
  1480. range->value_min = 1;
  1481. range->value_max = INT_MAX;
  1482. break;
  1483. default:
  1484. ret = AVERROR(ENOSYS);
  1485. goto fail;
  1486. }
  1487. *ranges_arg = ranges;
  1488. return 1;
  1489. fail:
  1490. av_free(ranges);
  1491. av_free(range);
  1492. av_free(range_array);
  1493. return ret;
  1494. }
  1495. void av_opt_freep_ranges(AVOptionRanges **rangesp)
  1496. {
  1497. int i;
  1498. AVOptionRanges *ranges = *rangesp;
  1499. if (!ranges)
  1500. return;
  1501. for (i = 0; i < ranges->nb_ranges * ranges->nb_components; i++) {
  1502. AVOptionRange *range = ranges->range[i];
  1503. if (range) {
  1504. av_freep(&range->str);
  1505. av_freep(&ranges->range[i]);
  1506. }
  1507. }
  1508. av_freep(&ranges->range);
  1509. av_freep(rangesp);
  1510. }
  1511. #ifdef TEST
  1512. typedef struct TestContext
  1513. {
  1514. const AVClass *class;
  1515. int num;
  1516. int toggle;
  1517. char *string;
  1518. int flags;
  1519. AVRational rational;
  1520. AVRational video_rate;
  1521. int w, h;
  1522. enum AVPixelFormat pix_fmt;
  1523. enum AVSampleFormat sample_fmt;
  1524. int64_t duration;
  1525. uint8_t color[4];
  1526. int64_t channel_layout;
  1527. } TestContext;
  1528. #define OFFSET(x) offsetof(TestContext, x)
  1529. #define TEST_FLAG_COOL 01
  1530. #define TEST_FLAG_LAME 02
  1531. #define TEST_FLAG_MU 04
  1532. static const AVOption test_options[]= {
  1533. {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
  1534. {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
  1535. {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
  1536. {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {.str = "default"}, CHAR_MIN, CHAR_MAX },
  1537. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
  1538. {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
  1539. {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
  1540. {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
  1541. {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{0}, 0, 0 },
  1542. {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, INT_MAX},
  1543. {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_NONE}, -1, INT_MAX},
  1544. {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0 },
  1545. {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX},
  1546. {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, {.str = "pink"}, 0, 0},
  1547. {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = AV_CH_LAYOUT_HEXAGONAL}, 0, INT64_MAX},
  1548. {NULL},
  1549. };
  1550. static const char *test_get_name(void *ctx)
  1551. {
  1552. return "test";
  1553. }
  1554. static const AVClass test_class = {
  1555. "TestContext",
  1556. test_get_name,
  1557. test_options
  1558. };
  1559. int main(void)
  1560. {
  1561. int i;
  1562. printf("\nTesting av_set_options_string()\n");
  1563. {
  1564. TestContext test_ctx = { 0 };
  1565. static const char * const options[] = {
  1566. "",
  1567. ":",
  1568. "=",
  1569. "foo=:",
  1570. ":=foo",
  1571. "=foo",
  1572. "foo=",
  1573. "foo",
  1574. "foo=val",
  1575. "foo==val",
  1576. "toggle=:",
  1577. "string=:",
  1578. "toggle=1 : foo",
  1579. "toggle=100",
  1580. "toggle==1",
  1581. "flags=+mu-lame : num=42: toggle=0",
  1582. "num=42 : string=blahblah",
  1583. "rational=0 : rational=1/2 : rational=1/-1",
  1584. "rational=-1/0",
  1585. "size=1024x768",
  1586. "size=pal",
  1587. "size=bogus",
  1588. "pix_fmt=yuv420p",
  1589. "pix_fmt=2",
  1590. "pix_fmt=bogus",
  1591. "sample_fmt=s16",
  1592. "sample_fmt=2",
  1593. "sample_fmt=bogus",
  1594. "video_rate=pal",
  1595. "video_rate=25",
  1596. "video_rate=30000/1001",
  1597. "video_rate=30/1.001",
  1598. "video_rate=bogus",
  1599. "duration=bogus",
  1600. "duration=123.45",
  1601. "duration=1\\:23\\:45.67",
  1602. "color=blue",
  1603. "color=0x223300",
  1604. "color=0x42FF07AA",
  1605. "cl=stereo+downmix",
  1606. "cl=foo",
  1607. };
  1608. test_ctx.class = &test_class;
  1609. av_opt_set_defaults(&test_ctx);
  1610. av_log_set_level(AV_LOG_DEBUG);
  1611. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  1612. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  1613. if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
  1614. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  1615. printf("\n");
  1616. }
  1617. av_opt_free(&test_ctx);
  1618. }
  1619. printf("\nTesting av_opt_set_from_string()\n");
  1620. {
  1621. TestContext test_ctx = { 0 };
  1622. static const char * const options[] = {
  1623. "",
  1624. "5",
  1625. "5:hello",
  1626. "5:hello:size=pal",
  1627. "5:size=pal:hello",
  1628. ":",
  1629. "=",
  1630. " 5 : hello : size = pal ",
  1631. "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
  1632. };
  1633. static const char * const shorthand[] = { "num", "string", NULL };
  1634. test_ctx.class = &test_class;
  1635. av_opt_set_defaults(&test_ctx);
  1636. av_log_set_level(AV_LOG_DEBUG);
  1637. for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
  1638. av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
  1639. if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
  1640. av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
  1641. printf("\n");
  1642. }
  1643. av_opt_free(&test_ctx);
  1644. }
  1645. return 0;
  1646. }
  1647. #endif