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.

168 lines
3.5KB

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