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.

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