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.

190 lines
5.1KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * misc parsing utilities for libavcore
  21. */
  22. #include "parseutils.h"
  23. #include "libavutil/avutil.h"
  24. #include "libavutil/eval.h"
  25. typedef struct {
  26. const char *abbr;
  27. int width, height;
  28. } VideoSizeAbbr;
  29. typedef struct {
  30. const char *abbr;
  31. AVRational rate;
  32. } VideoRateAbbr;
  33. static const VideoSizeAbbr video_size_abbrs[] = {
  34. { "ntsc", 720, 480 },
  35. { "pal", 720, 576 },
  36. { "qntsc", 352, 240 }, /* VCD compliant NTSC */
  37. { "qpal", 352, 288 }, /* VCD compliant PAL */
  38. { "sntsc", 640, 480 }, /* square pixel NTSC */
  39. { "spal", 768, 576 }, /* square pixel PAL */
  40. { "film", 352, 240 },
  41. { "ntsc-film", 352, 240 },
  42. { "sqcif", 128, 96 },
  43. { "qcif", 176, 144 },
  44. { "cif", 352, 288 },
  45. { "4cif", 704, 576 },
  46. { "16cif", 1408,1152 },
  47. { "qqvga", 160, 120 },
  48. { "qvga", 320, 240 },
  49. { "vga", 640, 480 },
  50. { "svga", 800, 600 },
  51. { "xga", 1024, 768 },
  52. { "uxga", 1600,1200 },
  53. { "qxga", 2048,1536 },
  54. { "sxga", 1280,1024 },
  55. { "qsxga", 2560,2048 },
  56. { "hsxga", 5120,4096 },
  57. { "wvga", 852, 480 },
  58. { "wxga", 1366, 768 },
  59. { "wsxga", 1600,1024 },
  60. { "wuxga", 1920,1200 },
  61. { "woxga", 2560,1600 },
  62. { "wqsxga", 3200,2048 },
  63. { "wquxga", 3840,2400 },
  64. { "whsxga", 6400,4096 },
  65. { "whuxga", 7680,4800 },
  66. { "cga", 320, 200 },
  67. { "ega", 640, 350 },
  68. { "hd480", 852, 480 },
  69. { "hd720", 1280, 720 },
  70. { "hd1080", 1920,1080 },
  71. };
  72. static const VideoRateAbbr video_rate_abbrs[]= {
  73. { "ntsc", { 30000, 1001 } },
  74. { "pal", { 25, 1 } },
  75. { "qntsc", { 30000, 1001 } }, /* VCD compliant NTSC */
  76. { "qpal", { 25, 1 } }, /* VCD compliant PAL */
  77. { "sntsc", { 30000, 1001 } }, /* square pixel NTSC */
  78. { "spal", { 25, 1 } }, /* square pixel PAL */
  79. { "film", { 24, 1 } },
  80. { "ntsc-film", { 24000, 1001 } },
  81. };
  82. int av_parse_video_size(int *width_ptr, int *height_ptr, const char *str)
  83. {
  84. int i;
  85. int n = FF_ARRAY_ELEMS(video_size_abbrs);
  86. char *p;
  87. int width = 0, height = 0;
  88. for (i = 0; i < n; i++) {
  89. if (!strcmp(video_size_abbrs[i].abbr, str)) {
  90. width = video_size_abbrs[i].width;
  91. height = video_size_abbrs[i].height;
  92. break;
  93. }
  94. }
  95. if (i == n) {
  96. p = str;
  97. width = strtol(p, &p, 10);
  98. if (*p)
  99. p++;
  100. height = strtol(p, &p, 10);
  101. }
  102. if (width <= 0 || height <= 0)
  103. return AVERROR(EINVAL);
  104. *width_ptr = width;
  105. *height_ptr = height;
  106. return 0;
  107. }
  108. int av_parse_video_rate(AVRational *rate, const char *arg)
  109. {
  110. int i, ret;
  111. int n = FF_ARRAY_ELEMS(video_rate_abbrs);
  112. double res;
  113. /* First, we check our abbreviation table */
  114. for (i = 0; i < n; ++i)
  115. if (!strcmp(video_rate_abbrs[i].abbr, arg)) {
  116. *rate = video_rate_abbrs[i].rate;
  117. return 0;
  118. }
  119. /* Then, we try to parse it as fraction */
  120. if ((ret = av_parse_and_eval_expr(&res, arg, NULL, NULL, NULL, NULL, NULL, NULL,
  121. NULL, 0, NULL)) < 0)
  122. return ret;
  123. *rate = av_d2q(res, 1001000);
  124. if (rate->num <= 0 || rate->den <= 0)
  125. return AVERROR(EINVAL);
  126. return 0;
  127. }
  128. #ifdef TEST
  129. #undef printf
  130. int main(void)
  131. {
  132. printf("Testing av_parse_video_rate()\n");
  133. {
  134. int i;
  135. const char *rates[] = {
  136. "-inf",
  137. "inf",
  138. "nan",
  139. "123/0",
  140. "-123 / 0",
  141. "",
  142. "/",
  143. " 123 / 321",
  144. "foo/foo",
  145. "foo/1",
  146. "1/foo",
  147. "0/0",
  148. "/0",
  149. "1/",
  150. "1",
  151. "0",
  152. "-123/123",
  153. "-foo",
  154. "123.23",
  155. ".23",
  156. "-.23",
  157. "-0.234",
  158. "-0.0000001",
  159. " 21332.2324 ",
  160. " -21332.2324 ",
  161. };
  162. for (i = 0; i < FF_ARRAY_ELEMS(rates); i++) {
  163. int ret;
  164. AVRational q = (AVRational){0, 0};
  165. ret = av_parse_video_rate(&q, rates[i]),
  166. printf("'%s' -> %d/%d ret:%d\n",
  167. rates[i], q.num, q.den, ret);
  168. }
  169. }
  170. return 0;
  171. }
  172. #endif /* TEST */