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.

209 lines
5.6KB

  1. /*
  2. * LGPL
  3. */
  4. /**
  5. * @file opts.c
  6. * options parser.
  7. * typical parsed command line:
  8. * msmpeg4:bitrate=720000:qmax=16
  9. *
  10. */
  11. #include "avcodec.h"
  12. const AVOption avoptions_common[] = {
  13. AVOPTION_CODEC_FLAG("bit_exact", "use only bit-exact stuff", flags, CODEC_FLAG_BITEXACT, 0),
  14. AVOPTION_CODEC_FLAG("mm_force", "force mm flags", dsp_mask, FF_MM_FORCE, 0),
  15. #ifdef HAVE_MMX
  16. AVOPTION_CODEC_FLAG("mm_mmx", "mask MMX feature", dsp_mask, FF_MM_MMX, 0),
  17. AVOPTION_CODEC_FLAG("mm_3dnow", "mask 3DNow feature", dsp_mask, FF_MM_3DNOW, 0),
  18. AVOPTION_CODEC_FLAG("mm_mmxext", "mask MMXEXT (MMX2) feature", dsp_mask, FF_MM_MMXEXT, 0),
  19. AVOPTION_CODEC_FLAG("mm_sse", "mask SSE feature", dsp_mask, FF_MM_SSE, 0),
  20. AVOPTION_CODEC_FLAG("mm_sse2", "mask SSE2 feature", dsp_mask, FF_MM_SSE2, 0),
  21. #endif
  22. AVOPTION_END()
  23. };
  24. const AVOption avoptions_workaround_bug[] = {
  25. AVOPTION_CODEC_FLAG("bug_autodetect", "workaround bug autodetection", workaround_bugs, FF_BUG_AUTODETECT, 1),
  26. AVOPTION_CODEC_FLAG("bug_old_msmpeg4", "workaround old msmpeg4 bug", workaround_bugs, FF_BUG_OLD_MSMPEG4, 0),
  27. AVOPTION_CODEC_FLAG("bug_xvid_ilace", "workaround XviD interlace bug", workaround_bugs, FF_BUG_XVID_ILACE, 0),
  28. AVOPTION_CODEC_FLAG("bug_ump4", "workaround ump4 bug", workaround_bugs, FF_BUG_UMP4, 0),
  29. AVOPTION_CODEC_FLAG("bug_no_padding", "workaround padding bug", workaround_bugs, FF_BUG_NO_PADDING, 0),
  30. AVOPTION_CODEC_FLAG("bug_ac_vlc", "workaround ac VLC bug", workaround_bugs, FF_BUG_AC_VLC, 0),
  31. AVOPTION_CODEC_FLAG("bug_qpel_chroma", "workaround qpel chroma bug", workaround_bugs, FF_BUG_QPEL_CHROMA, 0),
  32. AVOPTION_CODEC_FLAG("bug_std_qpel", "workaround std qpel bug", workaround_bugs, FF_BUG_STD_QPEL, 0),
  33. AVOPTION_CODEC_FLAG("bug_qpel_chroma2", "workaround qpel chroma2 bug", workaround_bugs, FF_BUG_QPEL_CHROMA2, 0),
  34. AVOPTION_CODEC_FLAG("bug_direct_blocksize", "workaround direct blocksize bug", workaround_bugs, FF_BUG_DIRECT_BLOCKSIZE, 0),
  35. AVOPTION_END()
  36. };
  37. /* avoid compatibility problems by redefining it */
  38. static int av_strcasecmp(const char *s1, const char *s2)
  39. {
  40. signed char val;
  41. for(;;) {
  42. val = toupper(*s1) - toupper(*s2);
  43. if (val != 0)
  44. break;
  45. if (*s1 != '\0')
  46. break;
  47. s1++;
  48. s2++;
  49. }
  50. return val;
  51. }
  52. static int parse_bool(const AVOption *c, char *s, int *var)
  53. {
  54. int b = 1; /* by default -on- when present */
  55. if (s) {
  56. if (!av_strcasecmp(s, "off") || !av_strcasecmp(s, "false")
  57. || !strcmp(s, "0"))
  58. b = 0;
  59. else if (!av_strcasecmp(s, "on") || !av_strcasecmp(s, "true")
  60. || !strcmp(s, "1"))
  61. b = 1;
  62. else
  63. return -1;
  64. }
  65. if (c->type == FF_OPT_TYPE_FLAG) {
  66. if (b)
  67. *var |= (int)c->min;
  68. else
  69. *var &= ~(int)c->min;
  70. } else
  71. *var = b;
  72. return 0;
  73. }
  74. static int parse_double(const AVOption *c, char *s, double *var)
  75. {
  76. double d;
  77. if (!s)
  78. return -1;
  79. d = atof(s);
  80. if (c->min != c->max) {
  81. if (d < c->min || d > c->max) {
  82. av_log(NULL, AV_LOG_ERROR, "Option: %s double value: %f out of range <%f, %f>\n",
  83. c->name, d, c->min, c->max);
  84. return -1;
  85. }
  86. }
  87. *var = d;
  88. return 0;
  89. }
  90. static int parse_int(const AVOption* c, char* s, int* var)
  91. {
  92. int i;
  93. if (!s)
  94. return -1;
  95. i = atoi(s);
  96. if (c->min != c->max) {
  97. if (i < (int)c->min || i > (int)c->max) {
  98. av_log(NULL, AV_LOG_ERROR, "Option: %s integer value: %d out of range <%d, %d>\n",
  99. c->name, i, (int)c->min, (int)c->max);
  100. return -1;
  101. }
  102. }
  103. *var = i;
  104. return 0;
  105. }
  106. static int parse_string(const AVOption *c, char *s, void* strct, char **var)
  107. {
  108. if (!s)
  109. return -1;
  110. if (c->type == FF_OPT_TYPE_RCOVERRIDE) {
  111. int sf, ef, qs;
  112. float qf;
  113. if (sscanf(s, "%d,%d,%d,%f", &sf, &ef, &qs, &qf) == 4 && sf < ef) {
  114. AVCodecContext *avctx = (AVCodecContext *) strct;
  115. RcOverride *o;
  116. avctx->rc_override = av_realloc(avctx->rc_override,
  117. sizeof(RcOverride) * (avctx->rc_override_count + 1));
  118. o = avctx->rc_override + avctx->rc_override_count++;
  119. o->start_frame = sf;
  120. o->end_frame = ef;
  121. o->qscale = qs;
  122. o->quality_factor = qf;
  123. //printf("parsed Rc: %d,%d,%d,%f (%d)\n", sf,ef,qs,qf, avctx->rc_override_count);
  124. } else {
  125. av_log(NULL, AV_LOG_ERROR, "incorrect/unparsable Rc: \"%s\"\n", s);
  126. }
  127. } else
  128. *var = av_strdup(s);
  129. return 0;
  130. }
  131. int avoption_parse(void* strct, const AVOption* list, const char *opts)
  132. {
  133. int r = 0;
  134. char* dopts = av_strdup(opts);
  135. if (dopts) {
  136. char *str = dopts;
  137. while (str && *str && r == 0) {
  138. const AVOption *stack[FF_OPT_MAX_DEPTH];
  139. const AVOption *c = list;
  140. int depth = 0;
  141. char* e = strchr(str, ':');
  142. char* p;
  143. if (e)
  144. *e++ = 0;
  145. p = strchr(str, '=');
  146. if (p)
  147. *p++ = 0;
  148. // going through option structures
  149. for (;;) {
  150. if (!c->name) {
  151. if (c->help) {
  152. stack[depth++] = c;
  153. c = (const AVOption*) c->help;
  154. assert(depth > FF_OPT_MAX_DEPTH);
  155. } else {
  156. if (depth == 0)
  157. break; // finished
  158. c = stack[--depth];
  159. c++;
  160. }
  161. } else {
  162. if (!strcmp(c->name, str)) {
  163. void* ptr = (char*)strct + c->offset;
  164. switch (c->type & FF_OPT_TYPE_MASK) {
  165. case FF_OPT_TYPE_BOOL:
  166. r = parse_bool(c, p, (int*)ptr);
  167. break;
  168. case FF_OPT_TYPE_DOUBLE:
  169. r = parse_double(c, p, (double*)ptr);
  170. break;
  171. case FF_OPT_TYPE_INT:
  172. r = parse_int(c, p, (int*)ptr);
  173. break;
  174. case FF_OPT_TYPE_STRING:
  175. r = parse_string(c, p, strct, (char**)ptr);
  176. break;
  177. default:
  178. assert(0 == 1);
  179. }
  180. }
  181. c++;
  182. }
  183. }
  184. str = e;
  185. }
  186. av_free(dopts);
  187. }
  188. return r;
  189. }