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.

249 lines
8.0KB

  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file opt.c
  22. * AVOptions
  23. * @author Michael Niedermayer <michaelni@gmx.at>
  24. */
  25. #include "avcodec.h"
  26. static double av_parse_num(const char *name, char **tail){
  27. double d;
  28. d= strtod(name, tail);
  29. if(*tail>name && (**tail=='/' || **tail==':'))
  30. d/=strtod((*tail)+1, tail);
  31. return d;
  32. }
  33. //FIXME order them and do a bin search
  34. static AVOption *find_opt(void *v, const char *name){
  35. AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
  36. AVOption *o= c->option;
  37. for(;o && o->name; o++){
  38. if(!strcmp(o->name, name))
  39. return o;
  40. }
  41. return NULL;
  42. }
  43. AVOption *av_next_option(void *obj, AVOption *last){
  44. if(last && last[1].name) return ++last;
  45. else if(last) return NULL;
  46. else return (*(AVClass**)obj)->option;
  47. }
  48. static AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum){
  49. AVOption *o= find_opt(obj, name);
  50. void *dst;
  51. if(!o || o->offset<=0)
  52. return NULL;
  53. if(o->max*den < num*intnum || o->min*den > num*intnum)
  54. return NULL;
  55. dst= ((uint8_t*)obj) + o->offset;
  56. switch(o->type){
  57. case FF_OPT_TYPE_FLAGS:
  58. case FF_OPT_TYPE_INT: *(int *)dst= lrintf(num/den)*intnum; break;
  59. case FF_OPT_TYPE_INT64: *(int64_t *)dst= lrintf(num/den)*intnum; break;
  60. case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
  61. case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
  62. case FF_OPT_TYPE_RATIONAL:
  63. if((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
  64. else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
  65. default:
  66. return NULL;
  67. }
  68. return o;
  69. }
  70. //FIXME use eval.c maybe?
  71. AVOption *av_set_string(void *obj, const char *name, const char *val){
  72. AVOption *o= find_opt(obj, name);
  73. if(!o || !val || o->offset<=0)
  74. return NULL;
  75. if(o->type != FF_OPT_TYPE_STRING){
  76. for(;;){
  77. int i;
  78. char buf[256], *tail;
  79. int cmd=0;
  80. double d;
  81. if(*val == '+' || *val == '-')
  82. cmd= *(val++);
  83. for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
  84. buf[i]= val[i];
  85. buf[i]=0;
  86. val+= i;
  87. d= av_parse_num(buf, &tail);
  88. if(tail <= buf){
  89. AVOption *o_named= find_opt(obj, buf);
  90. if(o_named && o_named->type == FF_OPT_TYPE_CONST && !strcmp(o_named->unit, o->unit))
  91. d= o_named->default_val;
  92. else if(!strcmp(buf, "default")) d= o->default_val;
  93. else if(!strcmp(buf, "max" )) d= o->max;
  94. else if(!strcmp(buf, "min" )) d= o->min;
  95. else return NULL;
  96. }
  97. if(o->type == FF_OPT_TYPE_FLAGS){
  98. if (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
  99. else if(cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
  100. }else if(cmd=='-')
  101. d= -d;
  102. av_set_number(obj, name, d, 1, 1);
  103. if(!*val)
  104. return o;
  105. }
  106. return NULL;
  107. }
  108. memcpy(((uint8_t*)obj) + o->offset, val, sizeof(val));
  109. return o;
  110. }
  111. AVOption *av_set_double(void *obj, const char *name, double n){
  112. return av_set_number(obj, name, n, 1, 1);
  113. }
  114. AVOption *av_set_q(void *obj, const char *name, AVRational n){
  115. return av_set_number(obj, name, n.num, n.den, 1);
  116. }
  117. AVOption *av_set_int(void *obj, const char *name, int64_t n){
  118. return av_set_number(obj, name, 1, 1, n);
  119. }
  120. /**
  121. *
  122. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  123. * @param buf_len allocated length in bytes of buf
  124. */
  125. const char *av_get_string(void *obj, const char *name, AVOption **o_out, char *buf, int buf_len){
  126. AVOption *o= find_opt(obj, name);
  127. void *dst;
  128. if(!o || o->offset<=0)
  129. return NULL;
  130. if(o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
  131. return NULL;
  132. dst= ((uint8_t*)obj) + o->offset;
  133. if(o_out) *o_out= o;
  134. if(o->type == FF_OPT_TYPE_STRING)
  135. return dst;
  136. switch(o->type){
  137. case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  138. case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  139. case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%Ld", *(int64_t*)dst);break;
  140. case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  141. case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  142. case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  143. default: return NULL;
  144. }
  145. return buf;
  146. }
  147. static int av_get_number(void *obj, const char *name, AVOption **o_out, double *num, int *den, int64_t *intnum){
  148. AVOption *o= find_opt(obj, name);
  149. void *dst;
  150. if(!o || o->offset<=0)
  151. goto error;
  152. dst= ((uint8_t*)obj) + o->offset;
  153. if(o_out) *o_out= o;
  154. switch(o->type){
  155. case FF_OPT_TYPE_FLAGS:
  156. case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0;
  157. case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0;
  158. case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0;
  159. case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0;
  160. case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num;
  161. *den = ((AVRational*)dst)->den;
  162. return 0;
  163. }
  164. error:
  165. *den=*intnum=0;
  166. return -1;
  167. }
  168. double av_get_double(void *obj, const char *name, AVOption **o_out){
  169. int64_t intnum=1;
  170. double num=1;
  171. int den=1;
  172. av_get_number(obj, name, o_out, &num, &den, &intnum);
  173. return num*intnum/den;
  174. }
  175. AVRational av_get_q(void *obj, const char *name, AVOption **o_out){
  176. int64_t intnum=1;
  177. double num=1;
  178. int den=1;
  179. av_get_number(obj, name, o_out, &num, &den, &intnum);
  180. if(num == 1.0 && (int)intnum == intnum)
  181. return (AVRational){intnum, den};
  182. else
  183. return av_d2q(num*intnum/den, 1<<24);
  184. }
  185. int64_t av_get_int(void *obj, const char *name, AVOption **o_out){
  186. int64_t intnum=1;
  187. double num=1;
  188. int den=1;
  189. av_get_number(obj, name, o_out, &num, &den, &intnum);
  190. return num*intnum/den;
  191. }
  192. int av_opt_show(void *obj, FILE *f){
  193. AVOption *opt=NULL;
  194. if(!obj)
  195. return -1;
  196. #undef fprintf
  197. fprintf(f, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  198. while((opt= av_next_option(obj, opt))){
  199. if(!(opt->flags & (AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM)))
  200. continue;
  201. fprintf(f, "-%-17s ", opt->name);
  202. fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  203. fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  204. fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  205. fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  206. fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  207. fprintf(f, " %s\n", opt->help);
  208. }
  209. return 0;
  210. }