opt.c 63 KB

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