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.

148 lines
4.2KB

  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. typedef struct {
  25. const char *abbr;
  26. int width, height;
  27. } VideoSizeAbbr;
  28. typedef struct {
  29. const char *abbr;
  30. AVRational rate;
  31. } VideoRateAbbr;
  32. static const VideoSizeAbbr video_size_abbrs[] = {
  33. { "ntsc", 720, 480 },
  34. { "pal", 720, 576 },
  35. { "qntsc", 352, 240 }, /* VCD compliant NTSC */
  36. { "qpal", 352, 288 }, /* VCD compliant PAL */
  37. { "sntsc", 640, 480 }, /* square pixel NTSC */
  38. { "spal", 768, 576 }, /* square pixel PAL */
  39. { "film", 352, 240 },
  40. { "ntsc-film", 352, 240 },
  41. { "sqcif", 128, 96 },
  42. { "qcif", 176, 144 },
  43. { "cif", 352, 288 },
  44. { "4cif", 704, 576 },
  45. { "16cif", 1408,1152 },
  46. { "qqvga", 160, 120 },
  47. { "qvga", 320, 240 },
  48. { "vga", 640, 480 },
  49. { "svga", 800, 600 },
  50. { "xga", 1024, 768 },
  51. { "uxga", 1600,1200 },
  52. { "qxga", 2048,1536 },
  53. { "sxga", 1280,1024 },
  54. { "qsxga", 2560,2048 },
  55. { "hsxga", 5120,4096 },
  56. { "wvga", 852, 480 },
  57. { "wxga", 1366, 768 },
  58. { "wsxga", 1600,1024 },
  59. { "wuxga", 1920,1200 },
  60. { "woxga", 2560,1600 },
  61. { "wqsxga", 3200,2048 },
  62. { "wquxga", 3840,2400 },
  63. { "whsxga", 6400,4096 },
  64. { "whuxga", 7680,4800 },
  65. { "cga", 320, 200 },
  66. { "ega", 640, 350 },
  67. { "hd480", 852, 480 },
  68. { "hd720", 1280, 720 },
  69. { "hd1080", 1920,1080 },
  70. };
  71. static const VideoRateAbbr video_rate_abbrs[]= {
  72. { "ntsc", { 30000, 1001 } },
  73. { "pal", { 25, 1 } },
  74. { "qntsc", { 30000, 1001 } }, /* VCD compliant NTSC */
  75. { "qpal", { 25, 1 } }, /* VCD compliant PAL */
  76. { "sntsc", { 30000, 1001 } }, /* square pixel NTSC */
  77. { "spal", { 25, 1 } }, /* square pixel PAL */
  78. { "film", { 24, 1 } },
  79. { "ntsc-film", { 24000, 1001 } },
  80. };
  81. int av_parse_video_size(int *width_ptr, int *height_ptr, const char *str)
  82. {
  83. int i;
  84. int n = FF_ARRAY_ELEMS(video_size_abbrs);
  85. char *p;
  86. int width = 0, height = 0;
  87. for (i = 0; i < n; i++) {
  88. if (!strcmp(video_size_abbrs[i].abbr, str)) {
  89. width = video_size_abbrs[i].width;
  90. height = video_size_abbrs[i].height;
  91. break;
  92. }
  93. }
  94. if (i == n) {
  95. p = str;
  96. width = strtol(p, &p, 10);
  97. if (*p)
  98. p++;
  99. height = strtol(p, &p, 10);
  100. }
  101. if (width <= 0 || height <= 0)
  102. return AVERROR(EINVAL);
  103. *width_ptr = width;
  104. *height_ptr = height;
  105. return 0;
  106. }
  107. int av_parse_video_rate(AVRational *rate, const char *arg)
  108. {
  109. int i;
  110. int n = FF_ARRAY_ELEMS(video_rate_abbrs);
  111. char *cp;
  112. /* First, we check our abbreviation table */
  113. for (i = 0; i < n; ++i)
  114. if (!strcmp(video_rate_abbrs[i].abbr, arg)) {
  115. *rate = video_rate_abbrs[i].rate;
  116. return 0;
  117. }
  118. /* Then, we try to parse it as fraction */
  119. cp = strchr(arg, '/');
  120. if (!cp)
  121. cp = strchr(arg, ':');
  122. if (cp) {
  123. char *cpp;
  124. rate->num = strtol(arg, &cpp, 10);
  125. if (cpp != arg || cpp == cp)
  126. rate->den = strtol(cp+1, &cpp, 10);
  127. else
  128. rate->num = 0;
  129. } else {
  130. /* Finally we give up and parse it as double */
  131. *rate = av_d2q(strtod(arg, 0), 1001000);
  132. }
  133. if (rate->num <= 0 || rate->den <= 0)
  134. return AVERROR(EINVAL);
  135. return 0;
  136. }