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.

192 lines
5.2KB

  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. static int parse_bool(const AVOption *c, char *s, int *var)
  38. {
  39. int b = 1; /* by default -on- when present */
  40. if (s) {
  41. if (!strcasecmp(s, "off") || !strcasecmp(s, "false")
  42. || !strcmp(s, "0"))
  43. b = 0;
  44. else if (!strcasecmp(s, "on") || !strcasecmp(s, "true")
  45. || !strcmp(s, "1"))
  46. b = 1;
  47. else
  48. return -1;
  49. }
  50. if (c->type == FF_OPT_TYPE_FLAG) {
  51. if (b)
  52. *var |= (int)c->min;
  53. else
  54. *var &= ~(int)c->min;
  55. } else
  56. *var = b;
  57. return 0;
  58. }
  59. static int parse_double(const AVOption *c, char *s, double *var)
  60. {
  61. double d;
  62. if (!s)
  63. return -1;
  64. d = atof(s);
  65. if (c->min != c->max) {
  66. if (d < c->min || d > c->max) {
  67. fprintf(stderr, "Option: %s double value: %f out of range <%f, %f>\n",
  68. c->name, d, c->min, c->max);
  69. return -1;
  70. }
  71. }
  72. *var = d;
  73. return 0;
  74. }
  75. static int parse_int(const AVOption* c, char* s, int* var)
  76. {
  77. int i;
  78. if (!s)
  79. return -1;
  80. i = atoi(s);
  81. if (c->min != c->max) {
  82. if (i < (int)c->min || i > (int)c->max) {
  83. fprintf(stderr, "Option: %s integer value: %d out of range <%d, %d>\n",
  84. c->name, i, (int)c->min, (int)c->max);
  85. return -1;
  86. }
  87. }
  88. *var = i;
  89. return 0;
  90. }
  91. static int parse_string(const AVOption *c, char *s, void* strct, char **var)
  92. {
  93. if (!s)
  94. return -1;
  95. if (c->type == FF_OPT_TYPE_RCOVERRIDE) {
  96. int sf, ef, qs;
  97. float qf;
  98. if (sscanf(s, "%d,%d,%d,%f", &sf, &ef, &qs, &qf) == 4 && sf < ef) {
  99. AVCodecContext *avctx = (AVCodecContext *) strct;
  100. RcOverride *o;
  101. avctx->rc_override = av_realloc(avctx->rc_override,
  102. sizeof(RcOverride) * (avctx->rc_override_count + 1));
  103. o = avctx->rc_override + avctx->rc_override_count++;
  104. o->start_frame = sf;
  105. o->end_frame = ef;
  106. o->qscale = qs;
  107. o->quality_factor = qf;
  108. //printf("parsed Rc: %d,%d,%d,%f (%d)\n", sf,ef,qs,qf, avctx->rc_override_count);
  109. } else {
  110. printf("incorrect/unparsable Rc: \"%s\"\n", s);
  111. }
  112. } else
  113. *var = av_strdup(s);
  114. return 0;
  115. }
  116. int avoption_parse(void* strct, const AVOption* list, const char *opts)
  117. {
  118. int r = 0;
  119. char* dopts = av_strdup(opts);
  120. if (dopts) {
  121. char *str = dopts;
  122. while (str && *str && r == 0) {
  123. const AVOption *stack[FF_OPT_MAX_DEPTH];
  124. const AVOption *c = list;
  125. int depth = 0;
  126. char* e = strchr(str, ':');
  127. char* p;
  128. if (e)
  129. *e++ = 0;
  130. p = strchr(str, '=');
  131. if (p)
  132. *p++ = 0;
  133. // going through option structures
  134. for (;;) {
  135. if (!c->name) {
  136. if (c->help) {
  137. stack[depth++] = c;
  138. c = (const AVOption*) c->help;
  139. assert(depth > FF_OPT_MAX_DEPTH);
  140. } else {
  141. if (depth == 0)
  142. break; // finished
  143. c = stack[--depth];
  144. c++;
  145. }
  146. } else {
  147. if (!strcmp(c->name, str)) {
  148. void* ptr = (char*)strct + c->offset;
  149. switch (c->type & FF_OPT_TYPE_MASK) {
  150. case FF_OPT_TYPE_BOOL:
  151. r = parse_bool(c, p, (int*)ptr);
  152. break;
  153. case FF_OPT_TYPE_DOUBLE:
  154. r = parse_double(c, p, (double*)ptr);
  155. break;
  156. case FF_OPT_TYPE_INT:
  157. r = parse_int(c, p, (int*)ptr);
  158. break;
  159. case FF_OPT_TYPE_STRING:
  160. r = parse_string(c, p, strct, (char**)ptr);
  161. break;
  162. default:
  163. assert(0 == 1);
  164. }
  165. }
  166. c++;
  167. }
  168. }
  169. str = e;
  170. }
  171. av_free(dopts);
  172. }
  173. return r;
  174. }