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.

177 lines
4.1KB

  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. #ifdef HAVE_MMX
  13. extern const AVOption common_options[3 + 5];
  14. #else
  15. extern const AVOption common_options[3];
  16. #endif
  17. const AVOption common_options[] = {
  18. AVOPTION_CODEC_FLAG("bit_exact", "use only bit-exact stuff", flags, CODEC_FLAG_BITEXACT, 0),
  19. AVOPTION_CODEC_FLAG("mm_force", "force mm flags", dsp_mask, FF_MM_FORCE, 0),
  20. #ifdef HAVE_MMX
  21. AVOPTION_CODEC_FLAG("mm_mmx", "mask MMX feature", dsp_mask, FF_MM_MMX, 0),
  22. AVOPTION_CODEC_FLAG("mm_3dnow", "mask 3DNow feature", dsp_mask, FF_MM_3DNOW, 0),
  23. AVOPTION_CODEC_FLAG("mm_mmxext", "mask MMXEXT (MMX2) feature", dsp_mask, FF_MM_MMXEXT, 0),
  24. AVOPTION_CODEC_FLAG("mm_sse", "mask SSE feature", dsp_mask, FF_MM_SSE, 0),
  25. AVOPTION_CODEC_FLAG("mm_sse2", "mask SSE2 feature", dsp_mask, FF_MM_SSE2, 0),
  26. #endif
  27. AVOPTION_END()
  28. };
  29. static int parse_bool(const AVOption *c, char *s, int *var)
  30. {
  31. int b = 1; /* by default -on- when present */
  32. if (s) {
  33. if (!strcasecmp(s, "off") || !strcasecmp(s, "false")
  34. || !strcmp(s, "0"))
  35. b = 0;
  36. else if (!strcasecmp(s, "on") || !strcasecmp(s, "true")
  37. || !strcmp(s, "1"))
  38. b = 1;
  39. else
  40. return -1;
  41. }
  42. *var = b;
  43. return 0;
  44. }
  45. static int parse_double(const AVOption *c, char *s, double *var)
  46. {
  47. double d;
  48. if (!s)
  49. return -1;
  50. d = atof(s);
  51. if (c->min != c->max) {
  52. if (d < c->min || d > c->max) {
  53. fprintf(stderr, "Option: %s double value: %f out of range <%f, %f>\n",
  54. c->name, d, c->min, c->max);
  55. return -1;
  56. }
  57. }
  58. *var = d;
  59. return 0;
  60. }
  61. static int parse_int(const AVOption* c, char* s, int* var)
  62. {
  63. int i;
  64. if (!s)
  65. return -1;
  66. i = atoi(s);
  67. if (c->min != c->max) {
  68. if (i < (int)c->min || i > (int)c->max) {
  69. fprintf(stderr, "Option: %s integer value: %d out of range <%d, %d>\n",
  70. c->name, i, (int)c->min, (int)c->max);
  71. return -1;
  72. }
  73. }
  74. *var = i;
  75. return 0;
  76. }
  77. static int parse_string(const AVOption *c, char *s, void* strct, char **var)
  78. {
  79. if (!s)
  80. return -1;
  81. if (c->type == FF_OPT_TYPE_RCOVERRIDE) {
  82. int sf, ef, qs;
  83. float qf;
  84. if (sscanf(s, "%d,%d,%d,%f", &sf, &ef, &qs, &qf) == 4 && sf < ef) {
  85. AVCodecContext *avctx = (AVCodecContext *) strct;
  86. RcOverride *o;
  87. avctx->rc_override = av_realloc(avctx->rc_override,
  88. sizeof(RcOverride) * (avctx->rc_override_count + 1));
  89. o = avctx->rc_override + avctx->rc_override_count++;
  90. o->start_frame = sf;
  91. o->end_frame = ef;
  92. o->qscale = qs;
  93. o->quality_factor = qf;
  94. //printf("parsed Rc: %d,%d,%d,%f (%d)\n", sf,ef,qs,qf, avctx->rc_override_count);
  95. } else {
  96. printf("incorrect/unparsable Rc: \"%s\"\n", s);
  97. }
  98. } else
  99. *var = av_strdup(s);
  100. return 0;
  101. }
  102. int avoption_parse(void* strct, const AVOption* list, const char *opts)
  103. {
  104. int r = 0;
  105. char* dopts = av_strdup(opts);
  106. if (dopts) {
  107. char *str = dopts;
  108. while (str && *str && r == 0) {
  109. const AVOption *stack[FF_OPT_MAX_DEPTH];
  110. const AVOption *c = list;
  111. int depth = 0;
  112. char* e = strchr(str, ':');
  113. char* p;
  114. if (e)
  115. *e++ = 0;
  116. p = strchr(str, '=');
  117. if (p)
  118. *p++ = 0;
  119. // going through option structures
  120. for (;;) {
  121. if (!c->name) {
  122. if (c->help) {
  123. stack[depth++] = c;
  124. c = (const AVOption*) c->help;
  125. assert(depth > FF_OPT_MAX_DEPTH);
  126. } else {
  127. if (depth == 0)
  128. break; // finished
  129. c = stack[--depth];
  130. c++;
  131. }
  132. } else {
  133. if (!strcmp(c->name, str)) {
  134. void* ptr = (char*)strct + c->offset;
  135. switch (c->type & FF_OPT_TYPE_MASK) {
  136. case FF_OPT_TYPE_BOOL:
  137. r = parse_bool(c, p, (int*)ptr);
  138. break;
  139. case FF_OPT_TYPE_DOUBLE:
  140. r = parse_double(c, p, (double*)ptr);
  141. break;
  142. case FF_OPT_TYPE_INT:
  143. r = parse_int(c, p, (int*)ptr);
  144. break;
  145. case FF_OPT_TYPE_STRING:
  146. r = parse_string(c, p, strct, (char**)ptr);
  147. break;
  148. default:
  149. assert(0 == 1);
  150. }
  151. }
  152. c++;
  153. }
  154. }
  155. str = e;
  156. }
  157. av_free(dopts);
  158. }
  159. return r;
  160. }