opt.c 65 KB

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