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.

380 lines
12KB

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