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.

387 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. switch(o->type){
  169. case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  170. case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  171. case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
  172. case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  173. case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  174. case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  175. case FF_OPT_TYPE_STRING: return *(void**)dst;
  176. default: return NULL;
  177. }
  178. return buf;
  179. }
  180. static int av_get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum){
  181. const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
  182. void *dst;
  183. if(!o || o->offset<=0)
  184. goto error;
  185. dst= ((uint8_t*)obj) + o->offset;
  186. if(o_out) *o_out= o;
  187. switch(o->type){
  188. case FF_OPT_TYPE_FLAGS:
  189. case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0;
  190. case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0;
  191. case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0;
  192. case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0;
  193. case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num;
  194. *den = ((AVRational*)dst)->den;
  195. return 0;
  196. }
  197. error:
  198. *den=*intnum=0;
  199. return -1;
  200. }
  201. double av_get_double(void *obj, const char *name, const AVOption **o_out){
  202. int64_t intnum=1;
  203. double num=1;
  204. int den=1;
  205. av_get_number(obj, name, o_out, &num, &den, &intnum);
  206. return num*intnum/den;
  207. }
  208. AVRational av_get_q(void *obj, const char *name, const AVOption **o_out){
  209. int64_t intnum=1;
  210. double num=1;
  211. int den=1;
  212. av_get_number(obj, name, o_out, &num, &den, &intnum);
  213. if(num == 1.0 && (int)intnum == intnum)
  214. return (AVRational){intnum, den};
  215. else
  216. return av_d2q(num*intnum/den, 1<<24);
  217. }
  218. int64_t av_get_int(void *obj, const char *name, const AVOption **o_out){
  219. int64_t intnum=1;
  220. double num=1;
  221. int den=1;
  222. av_get_number(obj, name, o_out, &num, &den, &intnum);
  223. return num*intnum/den;
  224. }
  225. static void opt_list(void *obj, void *av_log_obj, const char *unit)
  226. {
  227. const AVOption *opt=NULL;
  228. while((opt= av_next_option(obj, opt))){
  229. if(!(opt->flags & (AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM)))
  230. continue;
  231. /* Don't print CONST's on level one.
  232. * Don't print anything but CONST's on level two.
  233. * Only print items from the requested unit.
  234. */
  235. if (!unit && opt->type==FF_OPT_TYPE_CONST)
  236. continue;
  237. else if (unit && opt->type!=FF_OPT_TYPE_CONST)
  238. continue;
  239. else if (unit && opt->type==FF_OPT_TYPE_CONST && strcmp(unit, opt->unit))
  240. continue;
  241. else if (unit && opt->type == FF_OPT_TYPE_CONST)
  242. av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
  243. else
  244. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  245. switch( opt->type )
  246. {
  247. case FF_OPT_TYPE_FLAGS:
  248. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>" );
  249. break;
  250. case FF_OPT_TYPE_INT:
  251. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<int>" );
  252. break;
  253. case FF_OPT_TYPE_INT64:
  254. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>" );
  255. break;
  256. case FF_OPT_TYPE_DOUBLE:
  257. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<double>" );
  258. break;
  259. case FF_OPT_TYPE_FLOAT:
  260. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<float>" );
  261. break;
  262. case FF_OPT_TYPE_STRING:
  263. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<string>" );
  264. break;
  265. case FF_OPT_TYPE_RATIONAL:
  266. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>" );
  267. break;
  268. case FF_OPT_TYPE_CONST:
  269. default:
  270. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "" );
  271. break;
  272. }
  273. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  274. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  275. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  276. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  277. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  278. if(opt->help)
  279. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  280. av_log(av_log_obj, AV_LOG_INFO, "\n");
  281. if (opt->unit && opt->type != FF_OPT_TYPE_CONST) {
  282. opt_list(obj, av_log_obj, opt->unit);
  283. }
  284. }
  285. }
  286. int av_opt_show(void *obj, void *av_log_obj){
  287. if(!obj)
  288. return -1;
  289. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  290. opt_list(obj, av_log_obj, NULL);
  291. return 0;
  292. }
  293. /** Set the values of the AVCodecContext or AVFormatContext structure.
  294. * They are set to the defaults specified in the according AVOption options
  295. * array default_val field.
  296. *
  297. * @param s AVCodecContext or AVFormatContext for which the defaults will be set
  298. */
  299. void av_opt_set_defaults2(void *s, int mask, int flags)
  300. {
  301. const AVOption *opt = NULL;
  302. while ((opt = av_next_option(s, opt)) != NULL) {
  303. if((opt->flags & mask) != flags)
  304. continue;
  305. switch(opt->type) {
  306. case FF_OPT_TYPE_CONST:
  307. /* Nothing to be done here */
  308. break;
  309. case FF_OPT_TYPE_FLAGS:
  310. case FF_OPT_TYPE_INT: {
  311. int val;
  312. val = opt->default_val;
  313. av_set_int(s, opt->name, val);
  314. }
  315. break;
  316. case FF_OPT_TYPE_FLOAT: {
  317. double val;
  318. val = opt->default_val;
  319. av_set_double(s, opt->name, val);
  320. }
  321. break;
  322. case FF_OPT_TYPE_RATIONAL: {
  323. AVRational val;
  324. val = av_d2q(opt->default_val, INT_MAX);
  325. av_set_q(s, opt->name, val);
  326. }
  327. break;
  328. case FF_OPT_TYPE_STRING:
  329. /* Cannot set default for string as default_val is of type * double */
  330. break;
  331. default:
  332. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  333. }
  334. }
  335. }
  336. void av_opt_set_defaults(void *s){
  337. av_opt_set_defaults2(s, 0, 0);
  338. }