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

389 lines
13KB

  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 opt.c
  23. * AVOptions
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "avcodec.h"
  27. #include "opt.h"
  28. #include "eval.h"
  29. //FIXME order them and do a bin search
  30. const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags){
  31. AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
  32. const AVOption *o= c->option;
  33. for(;o && o->name; o++){
  34. if(!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags )
  35. return o;
  36. }
  37. return NULL;
  38. }
  39. const AVOption *av_next_option(void *obj, const AVOption *last){
  40. if(last && last[1].name) return ++last;
  41. else if(last) return NULL;
  42. else return (*(AVClass**)obj)->option;
  43. }
  44. static const AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum){
  45. const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
  46. void *dst;
  47. if(!o || o->offset<=0)
  48. return NULL;
  49. if(o->max*den < num*intnum || o->min*den > num*intnum) {
  50. av_log(NULL, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range.\n", num, name);
  51. return NULL;
  52. }
  53. dst= ((uint8_t*)obj) + o->offset;
  54. switch(o->type){
  55. case FF_OPT_TYPE_FLAGS:
  56. case FF_OPT_TYPE_INT: *(int *)dst= lrintf(num/den)*intnum; break;
  57. case FF_OPT_TYPE_INT64: *(int64_t *)dst= lrintf(num/den)*intnum; break;
  58. case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
  59. case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
  60. case FF_OPT_TYPE_RATIONAL:
  61. if((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
  62. else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
  63. default:
  64. return NULL;
  65. }
  66. return o;
  67. }
  68. static const AVOption *set_all_opt(void *v, const char *unit, double d){
  69. AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
  70. const AVOption *o= c->option;
  71. const AVOption *ret=NULL;
  72. for(;o && o->name; o++){
  73. if(o->type != FF_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)){
  74. double tmp= d;
  75. if(o->type == FF_OPT_TYPE_FLAGS)
  76. tmp= av_get_int(v, o->name, NULL) | (int64_t)d;
  77. av_set_number(v, o->name, tmp, 1, 1);
  78. ret= o;
  79. }
  80. }
  81. return ret;
  82. }
  83. static double const_values[]={
  84. M_PI,
  85. M_E,
  86. FF_QP2LAMBDA,
  87. 0
  88. };
  89. static const char *const_names[]={
  90. "PI",
  91. "E",
  92. "QP2LAMBDA",
  93. 0
  94. };
  95. const AVOption *av_set_string(void *obj, const char *name, const char *val){
  96. const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
  97. if(o && o->offset==0 && o->type == FF_OPT_TYPE_CONST && o->unit){
  98. return set_all_opt(obj, o->unit, o->default_val);
  99. }
  100. if(!o || !val || o->offset<=0)
  101. return NULL;
  102. if(o->type != FF_OPT_TYPE_STRING){
  103. for(;;){
  104. int i;
  105. char buf[256];
  106. int cmd=0;
  107. double d;
  108. char *error = NULL;
  109. if(*val == '+' || *val == '-')
  110. cmd= *(val++);
  111. for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
  112. buf[i]= val[i];
  113. buf[i]=0;
  114. val+= i;
  115. d = ff_eval2(buf, const_values, const_names, NULL, NULL, NULL, NULL, NULL, &error);
  116. if(isnan(d)) {
  117. const AVOption *o_named= av_find_opt(obj, buf, o->unit, 0, 0);
  118. if(o_named && o_named->type == FF_OPT_TYPE_CONST)
  119. d= o_named->default_val;
  120. else if(!strcmp(buf, "default")) d= o->default_val;
  121. else if(!strcmp(buf, "max" )) d= o->max;
  122. else if(!strcmp(buf, "min" )) d= o->min;
  123. else if(!strcmp(buf, "none" )) d= 0;
  124. else if(!strcmp(buf, "all" )) d= ~0;
  125. else {
  126. if (!error)
  127. av_log(NULL, AV_LOG_ERROR, "Unable to parse option value \"%s\": %s\n", val, error);
  128. return NULL;
  129. }
  130. }
  131. if(o->type == FF_OPT_TYPE_FLAGS){
  132. if (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
  133. else if(cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
  134. }else if(cmd=='-')
  135. d= -d;
  136. av_set_number(obj, name, d, 1, 1);
  137. if(!*val)
  138. return o;
  139. }
  140. return NULL;
  141. }
  142. memcpy(((uint8_t*)obj) + o->offset, val, sizeof(val));
  143. return o;
  144. }
  145. const AVOption *av_set_double(void *obj, const char *name, double n){
  146. return av_set_number(obj, name, n, 1, 1);
  147. }
  148. const AVOption *av_set_q(void *obj, const char *name, AVRational n){
  149. return av_set_number(obj, name, n.num, n.den, 1);
  150. }
  151. const AVOption *av_set_int(void *obj, const char *name, int64_t n){
  152. return av_set_number(obj, name, 1, 1, n);
  153. }
  154. /**
  155. *
  156. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  157. * @param buf_len allocated length in bytes of buf
  158. */
  159. const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len){
  160. const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
  161. void *dst;
  162. if(!o || o->offset<=0)
  163. return NULL;
  164. if(o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
  165. return NULL;
  166. dst= ((uint8_t*)obj) + o->offset;
  167. if(o_out) *o_out= o;
  168. if(o->type == FF_OPT_TYPE_STRING)
  169. return dst;
  170. switch(o->type){
  171. case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  172. case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  173. case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
  174. case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  175. case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  176. case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  177. default: return NULL;
  178. }
  179. return buf;
  180. }
  181. static int av_get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum){
  182. const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
  183. void *dst;
  184. if(!o || o->offset<=0)
  185. goto error;
  186. dst= ((uint8_t*)obj) + o->offset;
  187. if(o_out) *o_out= o;
  188. switch(o->type){
  189. case FF_OPT_TYPE_FLAGS:
  190. case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0;
  191. case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0;
  192. case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0;
  193. case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0;
  194. case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num;
  195. *den = ((AVRational*)dst)->den;
  196. return 0;
  197. }
  198. error:
  199. *den=*intnum=0;
  200. return -1;
  201. }
  202. double av_get_double(void *obj, const char *name, const AVOption **o_out){
  203. int64_t intnum=1;
  204. double num=1;
  205. int den=1;
  206. av_get_number(obj, name, o_out, &num, &den, &intnum);
  207. return num*intnum/den;
  208. }
  209. AVRational av_get_q(void *obj, const char *name, const AVOption **o_out){
  210. int64_t intnum=1;
  211. double num=1;
  212. int den=1;
  213. av_get_number(obj, name, o_out, &num, &den, &intnum);
  214. if(num == 1.0 && (int)intnum == intnum)
  215. return (AVRational){intnum, den};
  216. else
  217. return av_d2q(num*intnum/den, 1<<24);
  218. }
  219. int64_t av_get_int(void *obj, const char *name, const AVOption **o_out){
  220. int64_t intnum=1;
  221. double num=1;
  222. int den=1;
  223. av_get_number(obj, name, o_out, &num, &den, &intnum);
  224. return num*intnum/den;
  225. }
  226. static void opt_list(void *obj, void *av_log_obj, const char *unit)
  227. {
  228. const AVOption *opt=NULL;
  229. while((opt= av_next_option(obj, opt))){
  230. if(!(opt->flags & (AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM)))
  231. continue;
  232. /* Don't print CONST's on level one.
  233. * Don't print anything but CONST's on level two.
  234. * Only print items from the requested unit.
  235. */
  236. if (!unit && opt->type==FF_OPT_TYPE_CONST)
  237. continue;
  238. else if (unit && opt->type!=FF_OPT_TYPE_CONST)
  239. continue;
  240. else if (unit && opt->type==FF_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  241. continue;
  242. else if (unit && opt->type == FF_OPT_TYPE_CONST)
  243. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  244. else
  245. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  246. switch( opt->type )
  247. {
  248. case FF_OPT_TYPE_FLAGS:
  249. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>" );
  250. break;
  251. case FF_OPT_TYPE_INT:
  252. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<int>" );
  253. break;
  254. case FF_OPT_TYPE_INT64:
  255. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>" );
  256. break;
  257. case FF_OPT_TYPE_DOUBLE:
  258. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<double>" );
  259. break;
  260. case FF_OPT_TYPE_FLOAT:
  261. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<float>" );
  262. break;
  263. case FF_OPT_TYPE_STRING:
  264. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<string>" );
  265. break;
  266. case FF_OPT_TYPE_RATIONAL:
  267. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>" );
  268. break;
  269. case FF_OPT_TYPE_CONST:
  270. default:
  271. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "" );
  272. break;
  273. }
  274. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  275. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  276. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  277. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  278. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  279. if(opt->help)
  280. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  281. av_log(av_log_obj, AV_LOG_INFO, "\n");
  282. if (opt->unit && opt->type != FF_OPT_TYPE_CONST) {
  283. opt_list(obj, av_log_obj, opt->unit);
  284. }
  285. }
  286. }
  287. int av_opt_show(void *obj, void *av_log_obj){
  288. if(!obj)
  289. return -1;
  290. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  291. opt_list(obj, av_log_obj, NULL);
  292. return 0;
  293. }
  294. /** Set the values of the AVCodecContext or AVFormatContext structure.
  295. * They are set to the defaults specified in the according AVOption options
  296. * array default_val field.
  297. *
  298. * @param s AVCodecContext or AVFormatContext for which the defaults will be set
  299. */
  300. void av_opt_set_defaults2(void *s, int mask, int flags)
  301. {
  302. const AVOption *opt = NULL;
  303. while ((opt = av_next_option(s, opt)) != NULL) {
  304. if((opt->flags & mask) != flags)
  305. continue;
  306. switch(opt->type) {
  307. case FF_OPT_TYPE_CONST:
  308. /* Nothing to be done here */
  309. break;
  310. case FF_OPT_TYPE_FLAGS:
  311. case FF_OPT_TYPE_INT: {
  312. int val;
  313. val = opt->default_val;
  314. av_set_int(s, opt->name, val);
  315. }
  316. break;
  317. case FF_OPT_TYPE_FLOAT: {
  318. double val;
  319. val = opt->default_val;
  320. av_set_double(s, opt->name, val);
  321. }
  322. break;
  323. case FF_OPT_TYPE_RATIONAL: {
  324. AVRational val;
  325. val = av_d2q(opt->default_val, INT_MAX);
  326. av_set_q(s, opt->name, val);
  327. }
  328. break;
  329. case FF_OPT_TYPE_STRING:
  330. /* Cannot set default for string as default_val is of type * double */
  331. break;
  332. default:
  333. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  334. }
  335. }
  336. }
  337. void av_opt_set_defaults(void *s){
  338. av_opt_set_defaults2(s, 0, 0);
  339. }