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.

407 lines
13KB

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