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.

239 lines
7.5KB

  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_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. //FIXME use eval.c maybe?
  70. AVOption *av_set_string(void *obj, const char *name, const char *val){
  71. AVOption *o= find_opt(obj, name);
  72. if(!o || !val || o->offset<=0)
  73. return NULL;
  74. if(o->type != FF_OPT_TYPE_STRING){
  75. double d=0, tmp_d;
  76. for(;;){
  77. int i;
  78. char buf[256], *tail;
  79. for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+'; i++)
  80. buf[i]= val[i];
  81. buf[i]=0;
  82. val+= i;
  83. tmp_d= av_parse_num(buf, &tail);
  84. if(tail > buf)
  85. d+= tmp_d;
  86. else{
  87. AVOption *o_named= find_opt(obj, buf);
  88. if(o_named && o_named->type == FF_OPT_TYPE_CONST)
  89. d+= o_named->default_val;
  90. else if(!strcmp(buf, "default")) d+= o->default_val;
  91. else if(!strcmp(buf, "max" )) d+= o->max;
  92. else if(!strcmp(buf, "min" )) d+= o->min;
  93. else return NULL;
  94. }
  95. if(*val == '+') val++;
  96. if(!*val)
  97. return av_set_number(obj, name, d, 1, 1);
  98. }
  99. return NULL;
  100. }
  101. memcpy(((uint8_t*)obj) + o->offset, val, sizeof(val));
  102. return o;
  103. }
  104. AVOption *av_set_double(void *obj, const char *name, double n){
  105. return av_set_number(obj, name, n, 1, 1);
  106. }
  107. AVOption *av_set_q(void *obj, const char *name, AVRational n){
  108. return av_set_number(obj, name, n.num, n.den, 1);
  109. }
  110. AVOption *av_set_int(void *obj, const char *name, int64_t n){
  111. return av_set_number(obj, name, 1, 1, n);
  112. }
  113. /**
  114. *
  115. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  116. * @param buf_len allocated length in bytes of buf
  117. */
  118. const char *av_get_string(void *obj, const char *name, AVOption **o_out, char *buf, int buf_len){
  119. AVOption *o= find_opt(obj, name);
  120. void *dst;
  121. if(!o || o->offset<=0)
  122. return NULL;
  123. if(o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
  124. return NULL;
  125. dst= ((uint8_t*)obj) + o->offset;
  126. if(o_out) *o_out= o;
  127. if(o->type == FF_OPT_TYPE_STRING)
  128. return dst;
  129. switch(o->type){
  130. case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  131. case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%Ld", *(int64_t*)dst);break;
  132. case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  133. case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  134. case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  135. default: return NULL;
  136. }
  137. return buf;
  138. }
  139. static int av_get_number(void *obj, const char *name, AVOption **o_out, double *num, int *den, int64_t *intnum){
  140. AVOption *o= find_opt(obj, name);
  141. void *dst;
  142. if(!o || o->offset<=0)
  143. goto error;
  144. dst= ((uint8_t*)obj) + o->offset;
  145. if(o_out) *o_out= o;
  146. switch(o->type){
  147. case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0;
  148. case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0;
  149. case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0;
  150. case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0;
  151. case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num;
  152. *den = ((AVRational*)dst)->den;
  153. return 0;
  154. }
  155. error:
  156. *den=*intnum=0;
  157. return -1;
  158. }
  159. double av_get_double(void *obj, const char *name, AVOption **o_out){
  160. int64_t intnum=1;
  161. double num=1;
  162. int den=1;
  163. av_get_number(obj, name, o_out, &num, &den, &intnum);
  164. return num*intnum/den;
  165. }
  166. AVRational av_get_q(void *obj, const char *name, AVOption **o_out){
  167. int64_t intnum=1;
  168. double num=1;
  169. int den=1;
  170. av_get_number(obj, name, o_out, &num, &den, &intnum);
  171. if(num == 1.0 && (int)intnum == intnum)
  172. return (AVRational){intnum, den};
  173. else
  174. return av_d2q(num*intnum/den, 1<<24);
  175. }
  176. int64_t av_get_int(void *obj, const char *name, AVOption **o_out){
  177. int64_t intnum=1;
  178. double num=1;
  179. int den=1;
  180. av_get_number(obj, name, o_out, &num, &den, &intnum);
  181. return num*intnum/den;
  182. }
  183. int av_opt_show(void *obj, FILE *f){
  184. AVOption *opt=NULL;
  185. if(!obj)
  186. return -1;
  187. #undef fprintf
  188. fprintf(f, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  189. while((opt= av_next_option(obj, opt))){
  190. if(!(opt->flags & (AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM)))
  191. continue;
  192. fprintf(f, "-%-17s ", opt->name);
  193. fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  194. fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  195. fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  196. fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  197. fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  198. fprintf(f, " %s\n", opt->help);
  199. }
  200. return 0;
  201. }