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.

193 lines
5.3KB

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