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.

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