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.

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