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.

405 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. return NULL;
  112. dst= ((uint8_t*)obj) + o->offset;
  113. switch(o->type){
  114. case FF_OPT_TYPE_FLAGS:
  115. case FF_OPT_TYPE_INT: *(int *)dst= lrintf(num/den)*intnum; break;
  116. case FF_OPT_TYPE_INT64: *(int64_t *)dst= lrintf(num/den)*intnum; break;
  117. case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
  118. case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
  119. case FF_OPT_TYPE_RATIONAL:
  120. if((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
  121. else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
  122. default:
  123. return NULL;
  124. }
  125. return o;
  126. }
  127. static AVOption *set_all_opt(void *v, const char *unit, double d){
  128. AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
  129. AVOption *o= c->option;
  130. AVOption *ret=NULL;
  131. for(;o && o->name; o++){
  132. if(o->type != FF_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)){
  133. double tmp= d;
  134. if(o->type == FF_OPT_TYPE_FLAGS)
  135. tmp= av_get_int(v, o->name, NULL) | (int64_t)d;
  136. av_set_number(v, o->name, tmp, 1, 1);
  137. ret= o;
  138. }
  139. }
  140. return ret;
  141. }
  142. //FIXME use eval.c maybe?
  143. AVOption *av_set_string(void *obj, const char *name, const char *val){
  144. AVOption *o= find_opt(obj, name, NULL);
  145. if(o && o->offset==0 && o->type == FF_OPT_TYPE_CONST && o->unit){
  146. return set_all_opt(obj, o->unit, o->default_val);
  147. }
  148. if(!o || !val || o->offset<=0)
  149. return NULL;
  150. if(o->type != FF_OPT_TYPE_STRING){
  151. for(;;){
  152. int i;
  153. char buf[256], *tail;
  154. int cmd=0;
  155. double d;
  156. if(*val == '+' || *val == '-')
  157. cmd= *(val++);
  158. for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
  159. buf[i]= val[i];
  160. buf[i]=0;
  161. val+= i;
  162. d= av_parse_num(buf, &tail);
  163. if(tail <= buf){
  164. AVOption *o_named= find_opt(obj, buf, o->unit);
  165. if(o_named && o_named->type == FF_OPT_TYPE_CONST)
  166. d= o_named->default_val;
  167. else if(!strcmp(buf, "default")) d= o->default_val;
  168. else if(!strcmp(buf, "max" )) d= o->max;
  169. else if(!strcmp(buf, "min" )) d= o->min;
  170. else return NULL;
  171. }
  172. if(o->type == FF_OPT_TYPE_FLAGS){
  173. if (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
  174. else if(cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
  175. }else if(cmd=='-')
  176. d= -d;
  177. av_set_number(obj, name, d, 1, 1);
  178. if(!*val)
  179. return o;
  180. }
  181. return NULL;
  182. }
  183. memcpy(((uint8_t*)obj) + o->offset, val, sizeof(val));
  184. return o;
  185. }
  186. AVOption *av_set_double(void *obj, const char *name, double n){
  187. return av_set_number(obj, name, n, 1, 1);
  188. }
  189. AVOption *av_set_q(void *obj, const char *name, AVRational n){
  190. return av_set_number(obj, name, n.num, n.den, 1);
  191. }
  192. AVOption *av_set_int(void *obj, const char *name, int64_t n){
  193. return av_set_number(obj, name, 1, 1, n);
  194. }
  195. /**
  196. *
  197. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  198. * @param buf_len allocated length in bytes of buf
  199. */
  200. const char *av_get_string(void *obj, const char *name, AVOption **o_out, char *buf, int buf_len){
  201. AVOption *o= find_opt(obj, name, NULL);
  202. void *dst;
  203. if(!o || o->offset<=0)
  204. return NULL;
  205. if(o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
  206. return NULL;
  207. dst= ((uint8_t*)obj) + o->offset;
  208. if(o_out) *o_out= o;
  209. if(o->type == FF_OPT_TYPE_STRING)
  210. return dst;
  211. switch(o->type){
  212. case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  213. case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  214. case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
  215. case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  216. case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  217. case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  218. default: return NULL;
  219. }
  220. return buf;
  221. }
  222. static int av_get_number(void *obj, const char *name, AVOption **o_out, double *num, int *den, int64_t *intnum){
  223. AVOption *o= find_opt(obj, name, NULL);
  224. void *dst;
  225. if(!o || o->offset<=0)
  226. goto error;
  227. dst= ((uint8_t*)obj) + o->offset;
  228. if(o_out) *o_out= o;
  229. switch(o->type){
  230. case FF_OPT_TYPE_FLAGS:
  231. case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0;
  232. case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0;
  233. case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0;
  234. case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0;
  235. case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num;
  236. *den = ((AVRational*)dst)->den;
  237. return 0;
  238. }
  239. error:
  240. *den=*intnum=0;
  241. return -1;
  242. }
  243. double av_get_double(void *obj, const char *name, AVOption **o_out){
  244. int64_t intnum=1;
  245. double num=1;
  246. int den=1;
  247. av_get_number(obj, name, o_out, &num, &den, &intnum);
  248. return num*intnum/den;
  249. }
  250. AVRational av_get_q(void *obj, const char *name, AVOption **o_out){
  251. int64_t intnum=1;
  252. double num=1;
  253. int den=1;
  254. av_get_number(obj, name, o_out, &num, &den, &intnum);
  255. if(num == 1.0 && (int)intnum == intnum)
  256. return (AVRational){intnum, den};
  257. else
  258. return av_d2q(num*intnum/den, 1<<24);
  259. }
  260. int64_t av_get_int(void *obj, const char *name, AVOption **o_out){
  261. int64_t intnum=1;
  262. double num=1;
  263. int den=1;
  264. av_get_number(obj, name, o_out, &num, &den, &intnum);
  265. return num*intnum/den;
  266. }
  267. int av_opt_show(void *obj, void *av_log_obj){
  268. AVOption *opt=NULL;
  269. if(!obj)
  270. return -1;
  271. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  272. while((opt= av_next_option(obj, opt))){
  273. if(!(opt->flags & (AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM)))
  274. continue;
  275. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  276. switch( opt->type )
  277. {
  278. case FF_OPT_TYPE_FLAGS:
  279. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>" );
  280. break;
  281. case FF_OPT_TYPE_INT:
  282. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<int>" );
  283. break;
  284. case FF_OPT_TYPE_INT64:
  285. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>" );
  286. break;
  287. case FF_OPT_TYPE_DOUBLE:
  288. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<double>" );
  289. break;
  290. case FF_OPT_TYPE_FLOAT:
  291. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<float>" );
  292. break;
  293. case FF_OPT_TYPE_STRING:
  294. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<string>" );
  295. break;
  296. case FF_OPT_TYPE_RATIONAL:
  297. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>" );
  298. break;
  299. case FF_OPT_TYPE_CONST:
  300. default:
  301. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "" );
  302. break;
  303. }
  304. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  305. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  306. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  307. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  308. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  309. if(opt->help)
  310. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  311. av_log(av_log_obj, AV_LOG_INFO, "\n");
  312. }
  313. return 0;
  314. }
  315. /** Set the values of the AVCodecContext or AVFormatContext structure.
  316. * They are set to the defaults specified in the according AVOption options
  317. * array default_val field.
  318. *
  319. * @param s AVCodecContext or AVFormatContext for which the defaults will be set
  320. */
  321. void av_opt_set_defaults(void *s)
  322. {
  323. AVOption *opt = NULL;
  324. while ((opt = av_next_option(s, opt)) != NULL) {
  325. switch(opt->type) {
  326. case FF_OPT_TYPE_CONST:
  327. /* Nothing to be done here */
  328. break;
  329. case FF_OPT_TYPE_FLAGS:
  330. case FF_OPT_TYPE_INT: {
  331. int val;
  332. val = opt->default_val;
  333. av_set_int(s, opt->name, val);
  334. }
  335. break;
  336. case FF_OPT_TYPE_FLOAT: {
  337. double val;
  338. val = opt->default_val;
  339. av_set_double(s, opt->name, val);
  340. }
  341. break;
  342. case FF_OPT_TYPE_RATIONAL: {
  343. AVRational val;
  344. val = av_d2q(opt->default_val, INT_MAX);
  345. av_set_q(s, opt->name, val);
  346. }
  347. break;
  348. case FF_OPT_TYPE_STRING:
  349. /* Cannot set default for string as default_val is of type * double */
  350. break;
  351. default:
  352. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  353. }
  354. }
  355. }