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.

151 lines
4.6KB

  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. } VideoFrameSizeAbbr;
  28. typedef struct {
  29. const char *abbr;
  30. int rate_num, rate_den;
  31. } VideoFrameRateAbbr;
  32. static const VideoFrameSizeAbbr video_frame_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 VideoFrameRateAbbr video_frame_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_frame_size_abbrs);
  85. char *p;
  86. int frame_width = 0, frame_height = 0;
  87. for (i = 0; i < n; i++) {
  88. if (!strcmp(video_frame_size_abbrs[i].abbr, str)) {
  89. frame_width = video_frame_size_abbrs[i].width;
  90. frame_height = video_frame_size_abbrs[i].height;
  91. break;
  92. }
  93. }
  94. if (i == n) {
  95. p = str;
  96. frame_width = strtol(p, &p, 10);
  97. if (*p)
  98. p++;
  99. frame_height = strtol(p, &p, 10);
  100. }
  101. if (frame_width <= 0 || frame_height <= 0)
  102. return AVERROR(EINVAL);
  103. *width_ptr = frame_width;
  104. *height_ptr = frame_height;
  105. return 0;
  106. }
  107. int av_parse_video_rate(AVRational *frame_rate, const char *arg)
  108. {
  109. int i;
  110. int n = FF_ARRAY_ELEMS(video_frame_rate_abbrs);
  111. char *cp;
  112. /* First, we check our abbreviation table */
  113. for (i = 0; i < n; ++i)
  114. if (!strcmp(video_frame_rate_abbrs[i].abbr, arg)) {
  115. frame_rate->num = video_frame_rate_abbrs[i].rate_num;
  116. frame_rate->den = video_frame_rate_abbrs[i].rate_den;
  117. return 0;
  118. }
  119. /* Then, we try to parse it as fraction */
  120. cp = strchr(arg, '/');
  121. if (!cp)
  122. cp = strchr(arg, ':');
  123. if (cp) {
  124. char *cpp;
  125. frame_rate->num = strtol(arg, &cpp, 10);
  126. if (cpp != arg || cpp == cp)
  127. frame_rate->den = strtol(cp+1, &cpp, 10);
  128. else
  129. frame_rate->num = 0;
  130. } else {
  131. /* Finally we give up and parse it as double */
  132. AVRational time_base = av_d2q(strtod(arg, 0), 1001000);
  133. frame_rate->den = time_base.den;
  134. frame_rate->num = time_base.num;
  135. }
  136. if (frame_rate->num <= 0 || frame_rate->den <= 0)
  137. return AVERROR(EINVAL);
  138. return 0;
  139. }