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.

382 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. /**
  23. * @file opt.c
  24. * AVOptions
  25. * @author Michael Niedermayer <michaelni@gmx.at>
  26. */
  27. #include "avcodec.h"
  28. #include "opt.h"
  29. #include "eval.h"
  30. //FIXME order them and do a bin search
  31. static const AVOption *find_opt(void *v, const char *name, const char *unit){
  32. AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
  33. const AVOption *o= c->option;
  34. for(;o && o->name; o++){
  35. if(!strcmp(o->name, name) && (!unit || !strcmp(o->unit, unit)) )
  36. return o;
  37. }
  38. return NULL;
  39. }
  40. const AVOption *av_next_option(void *obj, const AVOption *last){
  41. if(last && last[1].name) return ++last;
  42. else if(last) return NULL;
  43. else return (*(AVClass**)obj)->option;
  44. }
  45. static const AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum){
  46. const AVOption *o= find_opt(obj, name, NULL);
  47. void *dst;
  48. if(!o || o->offset<=0)
  49. return NULL;
  50. if(o->max*den < num*intnum || o->min*den > num*intnum) {
  51. av_log(NULL, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range.\n", num, name);
  52. return NULL;
  53. }
  54. dst= ((uint8_t*)obj) + o->offset;
  55. switch(o->type){
  56. case FF_OPT_TYPE_FLAGS:
  57. case FF_OPT_TYPE_INT: *(int *)dst= lrintf(num/den)*intnum; break;
  58. case FF_OPT_TYPE_INT64: *(int64_t *)dst= lrintf(num/den)*intnum; break;
  59. case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
  60. case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
  61. case FF_OPT_TYPE_RATIONAL:
  62. if((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
  63. else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
  64. default:
  65. return NULL;
  66. }
  67. return o;
  68. }
  69. static const AVOption *set_all_opt(void *v, const char *unit, double d){
  70. AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
  71. const AVOption *o= c->option;
  72. const AVOption *ret=NULL;
  73. for(;o && o->name; o++){
  74. if(o->type != FF_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)){
  75. double tmp= d;
  76. if(o->type == FF_OPT_TYPE_FLAGS)
  77. tmp= av_get_int(v, o->name, NULL) | (int64_t)d;
  78. av_set_number(v, o->name, tmp, 1, 1);
  79. ret= o;
  80. }
  81. }
  82. return ret;
  83. }
  84. static double const_values[]={
  85. M_PI,
  86. M_E,
  87. FF_QP2LAMBDA,
  88. 0
  89. };
  90. static const char *const_names[]={
  91. "PI",
  92. "E",
  93. "QP2LAMBDA",
  94. 0
  95. };
  96. const AVOption *av_set_string(void *obj, const char *name, const char *val){
  97. const AVOption *o= find_opt(obj, name, NULL);
  98. if(o && o->offset==0 && o->type == FF_OPT_TYPE_CONST && o->unit){
  99. return set_all_opt(obj, o->unit, o->default_val);
  100. }
  101. if(!o || !val || o->offset<=0)
  102. return NULL;
  103. if(o->type != FF_OPT_TYPE_STRING){
  104. for(;;){
  105. int i;
  106. char buf[256];
  107. int cmd=0;
  108. double d;
  109. char *error = NULL;
  110. if(*val == '+' || *val == '-')
  111. cmd= *(val++);
  112. for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
  113. buf[i]= val[i];
  114. buf[i]=0;
  115. val+= i;
  116. d = ff_eval2(buf, const_values, const_names, NULL, NULL, NULL, NULL, NULL, &error);
  117. if(isnan(d)) {
  118. const AVOption *o_named= find_opt(obj, buf, o->unit);
  119. if(o_named && o_named->type == FF_OPT_TYPE_CONST)
  120. d= o_named->default_val;
  121. else if(!strcmp(buf, "default")) d= o->default_val;
  122. else if(!strcmp(buf, "max" )) d= o->max;
  123. else if(!strcmp(buf, "min" )) d= o->min;
  124. else {
  125. if (!error)
  126. av_log(NULL, AV_LOG_ERROR, "Unable to parse option value \"%s\": %s\n", val, error);
  127. return NULL;
  128. }
  129. }
  130. if(o->type == FF_OPT_TYPE_FLAGS){
  131. if (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
  132. else if(cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
  133. }else if(cmd=='-')
  134. d= -d;
  135. av_set_number(obj, name, d, 1, 1);
  136. if(!*val)
  137. return o;
  138. }
  139. return NULL;
  140. }
  141. memcpy(((uint8_t*)obj) + o->offset, val, sizeof(val));
  142. return o;
  143. }
  144. const AVOption *av_set_double(void *obj, const char *name, double n){
  145. return av_set_number(obj, name, n, 1, 1);
  146. }
  147. const AVOption *av_set_q(void *obj, const char *name, AVRational n){
  148. return av_set_number(obj, name, n.num, n.den, 1);
  149. }
  150. const AVOption *av_set_int(void *obj, const char *name, int64_t n){
  151. return av_set_number(obj, name, 1, 1, n);
  152. }
  153. /**
  154. *
  155. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  156. * @param buf_len allocated length in bytes of buf
  157. */
  158. const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len){
  159. const AVOption *o= find_opt(obj, name, NULL);
  160. void *dst;
  161. if(!o || o->offset<=0)
  162. return NULL;
  163. if(o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
  164. return NULL;
  165. dst= ((uint8_t*)obj) + o->offset;
  166. if(o_out) *o_out= o;
  167. if(o->type == FF_OPT_TYPE_STRING)
  168. return dst;
  169. switch(o->type){
  170. case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  171. case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  172. case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
  173. case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  174. case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  175. case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  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= find_opt(obj, name, NULL);
  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_defaults(void *s)
  300. {
  301. const AVOption *opt = NULL;
  302. while ((opt = av_next_option(s, opt)) != NULL) {
  303. switch(opt->type) {
  304. case FF_OPT_TYPE_CONST:
  305. /* Nothing to be done here */
  306. break;
  307. case FF_OPT_TYPE_FLAGS:
  308. case FF_OPT_TYPE_INT: {
  309. int val;
  310. val = opt->default_val;
  311. av_set_int(s, opt->name, val);
  312. }
  313. break;
  314. case FF_OPT_TYPE_FLOAT: {
  315. double val;
  316. val = opt->default_val;
  317. av_set_double(s, opt->name, val);
  318. }
  319. break;
  320. case FF_OPT_TYPE_RATIONAL: {
  321. AVRational val;
  322. val = av_d2q(opt->default_val, INT_MAX);
  323. av_set_q(s, opt->name, val);
  324. }
  325. break;
  326. case FF_OPT_TYPE_STRING:
  327. /* Cannot set default for string as default_val is of type * double */
  328. break;
  329. default:
  330. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  331. }
  332. }
  333. }