yading@11: /* yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: /** yading@11: * @file yading@11: * misc parsing utilities yading@11: */ yading@11: yading@11: #include yading@11: yading@11: #include "avstring.h" yading@11: #include "avutil.h" yading@11: #include "common.h" yading@11: #include "eval.h" yading@11: #include "log.h" yading@11: #include "random_seed.h" yading@11: #include "parseutils.h" yading@11: yading@11: #ifdef TEST yading@11: yading@11: #define av_get_random_seed av_get_random_seed_deterministic yading@11: static uint32_t av_get_random_seed_deterministic(void); yading@11: yading@11: #define time(t) 1331972053 yading@11: yading@11: #endif yading@11: yading@11: int av_parse_ratio(AVRational *q, const char *str, int max, yading@11: int log_offset, void *log_ctx) yading@11: { yading@11: char c; yading@11: int ret; yading@11: yading@11: if (sscanf(str, "%d:%d%c", &q->num, &q->den, &c) != 2) { yading@11: double d; yading@11: ret = av_expr_parse_and_eval(&d, str, NULL, NULL, yading@11: NULL, NULL, NULL, NULL, yading@11: NULL, log_offset, log_ctx); yading@11: if (ret < 0) yading@11: return ret; yading@11: *q = av_d2q(d, max); yading@11: } else { yading@11: av_reduce(&q->num, &q->den, q->num, q->den, max); yading@11: } yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: typedef struct { yading@11: const char *abbr; yading@11: int width, height; yading@11: } VideoSizeAbbr; yading@11: yading@11: typedef struct { yading@11: const char *abbr; yading@11: AVRational rate; yading@11: } VideoRateAbbr; yading@11: yading@11: static const VideoSizeAbbr video_size_abbrs[] = { yading@11: { "ntsc", 720, 480 }, yading@11: { "pal", 720, 576 }, yading@11: { "qntsc", 352, 240 }, /* VCD compliant NTSC */ yading@11: { "qpal", 352, 288 }, /* VCD compliant PAL */ yading@11: { "sntsc", 640, 480 }, /* square pixel NTSC */ yading@11: { "spal", 768, 576 }, /* square pixel PAL */ yading@11: { "film", 352, 240 }, yading@11: { "ntsc-film", 352, 240 }, yading@11: { "sqcif", 128, 96 }, yading@11: { "qcif", 176, 144 }, yading@11: { "cif", 352, 288 }, yading@11: { "4cif", 704, 576 }, yading@11: { "16cif", 1408,1152 }, yading@11: { "qqvga", 160, 120 }, yading@11: { "qvga", 320, 240 }, yading@11: { "vga", 640, 480 }, yading@11: { "svga", 800, 600 }, yading@11: { "xga", 1024, 768 }, yading@11: { "uxga", 1600,1200 }, yading@11: { "qxga", 2048,1536 }, yading@11: { "sxga", 1280,1024 }, yading@11: { "qsxga", 2560,2048 }, yading@11: { "hsxga", 5120,4096 }, yading@11: { "wvga", 852, 480 }, yading@11: { "wxga", 1366, 768 }, yading@11: { "wsxga", 1600,1024 }, yading@11: { "wuxga", 1920,1200 }, yading@11: { "woxga", 2560,1600 }, yading@11: { "wqsxga", 3200,2048 }, yading@11: { "wquxga", 3840,2400 }, yading@11: { "whsxga", 6400,4096 }, yading@11: { "whuxga", 7680,4800 }, yading@11: { "cga", 320, 200 }, yading@11: { "ega", 640, 350 }, yading@11: { "hd480", 852, 480 }, yading@11: { "hd720", 1280, 720 }, yading@11: { "hd1080", 1920,1080 }, yading@11: { "2k", 2048,1080 }, /* Digital Cinema System Specification */ yading@11: { "2kflat", 1998,1080 }, yading@11: { "2kscope", 2048, 858 }, yading@11: { "4k", 4096,2160 }, /* Digital Cinema System Specification */ yading@11: { "4kflat", 3996,2160 }, yading@11: { "4kscope", 4096,1716 }, yading@11: }; yading@11: yading@11: static const VideoRateAbbr video_rate_abbrs[]= { yading@11: { "ntsc", { 30000, 1001 } }, yading@11: { "pal", { 25, 1 } }, yading@11: { "qntsc", { 30000, 1001 } }, /* VCD compliant NTSC */ yading@11: { "qpal", { 25, 1 } }, /* VCD compliant PAL */ yading@11: { "sntsc", { 30000, 1001 } }, /* square pixel NTSC */ yading@11: { "spal", { 25, 1 } }, /* square pixel PAL */ yading@11: { "film", { 24, 1 } }, yading@11: { "ntsc-film", { 24000, 1001 } }, yading@11: }; yading@11: yading@11: int av_parse_video_size(int *width_ptr, int *height_ptr, const char *str) yading@11: { yading@11: int i; yading@11: int n = FF_ARRAY_ELEMS(video_size_abbrs); yading@11: const char *p; yading@11: int width = 0, height = 0; yading@11: yading@11: for (i = 0; i < n; i++) { yading@11: if (!strcmp(video_size_abbrs[i].abbr, str)) { yading@11: width = video_size_abbrs[i].width; yading@11: height = video_size_abbrs[i].height; yading@11: break; yading@11: } yading@11: } yading@11: if (i == n) { yading@11: width = strtol(str, (void*)&p, 10); yading@11: if (*p) yading@11: p++; yading@11: height = strtol(p, (void*)&p, 10); yading@11: yading@11: /* trailing extraneous data detected, like in 123x345foobar */ yading@11: if (*p) yading@11: return AVERROR(EINVAL); yading@11: } yading@11: if (width <= 0 || height <= 0) yading@11: return AVERROR(EINVAL); yading@11: *width_ptr = width; yading@11: *height_ptr = height; yading@11: return 0; yading@11: } yading@11: yading@11: int av_parse_video_rate(AVRational *rate, const char *arg) yading@11: { yading@11: int i, ret; yading@11: int n = FF_ARRAY_ELEMS(video_rate_abbrs); yading@11: yading@11: /* First, we check our abbreviation table */ yading@11: for (i = 0; i < n; ++i) yading@11: if (!strcmp(video_rate_abbrs[i].abbr, arg)) { yading@11: *rate = video_rate_abbrs[i].rate; yading@11: return 0; yading@11: } yading@11: yading@11: /* Then, we try to parse it as fraction */ yading@11: if ((ret = av_parse_ratio_quiet(rate, arg, 1001000)) < 0) yading@11: return ret; yading@11: if (rate->num <= 0 || rate->den <= 0) yading@11: return AVERROR(EINVAL); yading@11: return 0; yading@11: } yading@11: yading@11: typedef struct { yading@11: const char *name; ///< a string representing the name of the color yading@11: uint8_t rgb_color[3]; ///< RGB values for the color yading@11: } ColorEntry; yading@11: yading@11: static const ColorEntry color_table[] = { yading@11: { "AliceBlue", { 0xF0, 0xF8, 0xFF } }, yading@11: { "AntiqueWhite", { 0xFA, 0xEB, 0xD7 } }, yading@11: { "Aqua", { 0x00, 0xFF, 0xFF } }, yading@11: { "Aquamarine", { 0x7F, 0xFF, 0xD4 } }, yading@11: { "Azure", { 0xF0, 0xFF, 0xFF } }, yading@11: { "Beige", { 0xF5, 0xF5, 0xDC } }, yading@11: { "Bisque", { 0xFF, 0xE4, 0xC4 } }, yading@11: { "Black", { 0x00, 0x00, 0x00 } }, yading@11: { "BlanchedAlmond", { 0xFF, 0xEB, 0xCD } }, yading@11: { "Blue", { 0x00, 0x00, 0xFF } }, yading@11: { "BlueViolet", { 0x8A, 0x2B, 0xE2 } }, yading@11: { "Brown", { 0xA5, 0x2A, 0x2A } }, yading@11: { "BurlyWood", { 0xDE, 0xB8, 0x87 } }, yading@11: { "CadetBlue", { 0x5F, 0x9E, 0xA0 } }, yading@11: { "Chartreuse", { 0x7F, 0xFF, 0x00 } }, yading@11: { "Chocolate", { 0xD2, 0x69, 0x1E } }, yading@11: { "Coral", { 0xFF, 0x7F, 0x50 } }, yading@11: { "CornflowerBlue", { 0x64, 0x95, 0xED } }, yading@11: { "Cornsilk", { 0xFF, 0xF8, 0xDC } }, yading@11: { "Crimson", { 0xDC, 0x14, 0x3C } }, yading@11: { "Cyan", { 0x00, 0xFF, 0xFF } }, yading@11: { "DarkBlue", { 0x00, 0x00, 0x8B } }, yading@11: { "DarkCyan", { 0x00, 0x8B, 0x8B } }, yading@11: { "DarkGoldenRod", { 0xB8, 0x86, 0x0B } }, yading@11: { "DarkGray", { 0xA9, 0xA9, 0xA9 } }, yading@11: { "DarkGreen", { 0x00, 0x64, 0x00 } }, yading@11: { "DarkKhaki", { 0xBD, 0xB7, 0x6B } }, yading@11: { "DarkMagenta", { 0x8B, 0x00, 0x8B } }, yading@11: { "DarkOliveGreen", { 0x55, 0x6B, 0x2F } }, yading@11: { "Darkorange", { 0xFF, 0x8C, 0x00 } }, yading@11: { "DarkOrchid", { 0x99, 0x32, 0xCC } }, yading@11: { "DarkRed", { 0x8B, 0x00, 0x00 } }, yading@11: { "DarkSalmon", { 0xE9, 0x96, 0x7A } }, yading@11: { "DarkSeaGreen", { 0x8F, 0xBC, 0x8F } }, yading@11: { "DarkSlateBlue", { 0x48, 0x3D, 0x8B } }, yading@11: { "DarkSlateGray", { 0x2F, 0x4F, 0x4F } }, yading@11: { "DarkTurquoise", { 0x00, 0xCE, 0xD1 } }, yading@11: { "DarkViolet", { 0x94, 0x00, 0xD3 } }, yading@11: { "DeepPink", { 0xFF, 0x14, 0x93 } }, yading@11: { "DeepSkyBlue", { 0x00, 0xBF, 0xFF } }, yading@11: { "DimGray", { 0x69, 0x69, 0x69 } }, yading@11: { "DodgerBlue", { 0x1E, 0x90, 0xFF } }, yading@11: { "FireBrick", { 0xB2, 0x22, 0x22 } }, yading@11: { "FloralWhite", { 0xFF, 0xFA, 0xF0 } }, yading@11: { "ForestGreen", { 0x22, 0x8B, 0x22 } }, yading@11: { "Fuchsia", { 0xFF, 0x00, 0xFF } }, yading@11: { "Gainsboro", { 0xDC, 0xDC, 0xDC } }, yading@11: { "GhostWhite", { 0xF8, 0xF8, 0xFF } }, yading@11: { "Gold", { 0xFF, 0xD7, 0x00 } }, yading@11: { "GoldenRod", { 0xDA, 0xA5, 0x20 } }, yading@11: { "Gray", { 0x80, 0x80, 0x80 } }, yading@11: { "Green", { 0x00, 0x80, 0x00 } }, yading@11: { "GreenYellow", { 0xAD, 0xFF, 0x2F } }, yading@11: { "HoneyDew", { 0xF0, 0xFF, 0xF0 } }, yading@11: { "HotPink", { 0xFF, 0x69, 0xB4 } }, yading@11: { "IndianRed", { 0xCD, 0x5C, 0x5C } }, yading@11: { "Indigo", { 0x4B, 0x00, 0x82 } }, yading@11: { "Ivory", { 0xFF, 0xFF, 0xF0 } }, yading@11: { "Khaki", { 0xF0, 0xE6, 0x8C } }, yading@11: { "Lavender", { 0xE6, 0xE6, 0xFA } }, yading@11: { "LavenderBlush", { 0xFF, 0xF0, 0xF5 } }, yading@11: { "LawnGreen", { 0x7C, 0xFC, 0x00 } }, yading@11: { "LemonChiffon", { 0xFF, 0xFA, 0xCD } }, yading@11: { "LightBlue", { 0xAD, 0xD8, 0xE6 } }, yading@11: { "LightCoral", { 0xF0, 0x80, 0x80 } }, yading@11: { "LightCyan", { 0xE0, 0xFF, 0xFF } }, yading@11: { "LightGoldenRodYellow", { 0xFA, 0xFA, 0xD2 } }, yading@11: { "LightGreen", { 0x90, 0xEE, 0x90 } }, yading@11: { "LightGrey", { 0xD3, 0xD3, 0xD3 } }, yading@11: { "LightPink", { 0xFF, 0xB6, 0xC1 } }, yading@11: { "LightSalmon", { 0xFF, 0xA0, 0x7A } }, yading@11: { "LightSeaGreen", { 0x20, 0xB2, 0xAA } }, yading@11: { "LightSkyBlue", { 0x87, 0xCE, 0xFA } }, yading@11: { "LightSlateGray", { 0x77, 0x88, 0x99 } }, yading@11: { "LightSteelBlue", { 0xB0, 0xC4, 0xDE } }, yading@11: { "LightYellow", { 0xFF, 0xFF, 0xE0 } }, yading@11: { "Lime", { 0x00, 0xFF, 0x00 } }, yading@11: { "LimeGreen", { 0x32, 0xCD, 0x32 } }, yading@11: { "Linen", { 0xFA, 0xF0, 0xE6 } }, yading@11: { "Magenta", { 0xFF, 0x00, 0xFF } }, yading@11: { "Maroon", { 0x80, 0x00, 0x00 } }, yading@11: { "MediumAquaMarine", { 0x66, 0xCD, 0xAA } }, yading@11: { "MediumBlue", { 0x00, 0x00, 0xCD } }, yading@11: { "MediumOrchid", { 0xBA, 0x55, 0xD3 } }, yading@11: { "MediumPurple", { 0x93, 0x70, 0xD8 } }, yading@11: { "MediumSeaGreen", { 0x3C, 0xB3, 0x71 } }, yading@11: { "MediumSlateBlue", { 0x7B, 0x68, 0xEE } }, yading@11: { "MediumSpringGreen", { 0x00, 0xFA, 0x9A } }, yading@11: { "MediumTurquoise", { 0x48, 0xD1, 0xCC } }, yading@11: { "MediumVioletRed", { 0xC7, 0x15, 0x85 } }, yading@11: { "MidnightBlue", { 0x19, 0x19, 0x70 } }, yading@11: { "MintCream", { 0xF5, 0xFF, 0xFA } }, yading@11: { "MistyRose", { 0xFF, 0xE4, 0xE1 } }, yading@11: { "Moccasin", { 0xFF, 0xE4, 0xB5 } }, yading@11: { "NavajoWhite", { 0xFF, 0xDE, 0xAD } }, yading@11: { "Navy", { 0x00, 0x00, 0x80 } }, yading@11: { "OldLace", { 0xFD, 0xF5, 0xE6 } }, yading@11: { "Olive", { 0x80, 0x80, 0x00 } }, yading@11: { "OliveDrab", { 0x6B, 0x8E, 0x23 } }, yading@11: { "Orange", { 0xFF, 0xA5, 0x00 } }, yading@11: { "OrangeRed", { 0xFF, 0x45, 0x00 } }, yading@11: { "Orchid", { 0xDA, 0x70, 0xD6 } }, yading@11: { "PaleGoldenRod", { 0xEE, 0xE8, 0xAA } }, yading@11: { "PaleGreen", { 0x98, 0xFB, 0x98 } }, yading@11: { "PaleTurquoise", { 0xAF, 0xEE, 0xEE } }, yading@11: { "PaleVioletRed", { 0xD8, 0x70, 0x93 } }, yading@11: { "PapayaWhip", { 0xFF, 0xEF, 0xD5 } }, yading@11: { "PeachPuff", { 0xFF, 0xDA, 0xB9 } }, yading@11: { "Peru", { 0xCD, 0x85, 0x3F } }, yading@11: { "Pink", { 0xFF, 0xC0, 0xCB } }, yading@11: { "Plum", { 0xDD, 0xA0, 0xDD } }, yading@11: { "PowderBlue", { 0xB0, 0xE0, 0xE6 } }, yading@11: { "Purple", { 0x80, 0x00, 0x80 } }, yading@11: { "Red", { 0xFF, 0x00, 0x00 } }, yading@11: { "RosyBrown", { 0xBC, 0x8F, 0x8F } }, yading@11: { "RoyalBlue", { 0x41, 0x69, 0xE1 } }, yading@11: { "SaddleBrown", { 0x8B, 0x45, 0x13 } }, yading@11: { "Salmon", { 0xFA, 0x80, 0x72 } }, yading@11: { "SandyBrown", { 0xF4, 0xA4, 0x60 } }, yading@11: { "SeaGreen", { 0x2E, 0x8B, 0x57 } }, yading@11: { "SeaShell", { 0xFF, 0xF5, 0xEE } }, yading@11: { "Sienna", { 0xA0, 0x52, 0x2D } }, yading@11: { "Silver", { 0xC0, 0xC0, 0xC0 } }, yading@11: { "SkyBlue", { 0x87, 0xCE, 0xEB } }, yading@11: { "SlateBlue", { 0x6A, 0x5A, 0xCD } }, yading@11: { "SlateGray", { 0x70, 0x80, 0x90 } }, yading@11: { "Snow", { 0xFF, 0xFA, 0xFA } }, yading@11: { "SpringGreen", { 0x00, 0xFF, 0x7F } }, yading@11: { "SteelBlue", { 0x46, 0x82, 0xB4 } }, yading@11: { "Tan", { 0xD2, 0xB4, 0x8C } }, yading@11: { "Teal", { 0x00, 0x80, 0x80 } }, yading@11: { "Thistle", { 0xD8, 0xBF, 0xD8 } }, yading@11: { "Tomato", { 0xFF, 0x63, 0x47 } }, yading@11: { "Turquoise", { 0x40, 0xE0, 0xD0 } }, yading@11: { "Violet", { 0xEE, 0x82, 0xEE } }, yading@11: { "Wheat", { 0xF5, 0xDE, 0xB3 } }, yading@11: { "White", { 0xFF, 0xFF, 0xFF } }, yading@11: { "WhiteSmoke", { 0xF5, 0xF5, 0xF5 } }, yading@11: { "Yellow", { 0xFF, 0xFF, 0x00 } }, yading@11: { "YellowGreen", { 0x9A, 0xCD, 0x32 } }, yading@11: }; yading@11: yading@11: static int color_table_compare(const void *lhs, const void *rhs) yading@11: { yading@11: return av_strcasecmp(lhs, ((const ColorEntry *)rhs)->name); yading@11: } yading@11: yading@11: #define ALPHA_SEP '@' yading@11: yading@11: int av_parse_color(uint8_t *rgba_color, const char *color_string, int slen, yading@11: void *log_ctx) yading@11: { yading@11: char *tail, color_string2[128]; yading@11: const ColorEntry *entry; yading@11: int len, hex_offset = 0; yading@11: yading@11: if (color_string[0] == '#') { yading@11: hex_offset = 1; yading@11: } else if (!strncmp(color_string, "0x", 2)) yading@11: hex_offset = 2; yading@11: yading@11: if (slen < 0) yading@11: slen = strlen(color_string); yading@11: av_strlcpy(color_string2, color_string + hex_offset, yading@11: FFMIN(slen-hex_offset+1, sizeof(color_string2))); yading@11: if ((tail = strchr(color_string2, ALPHA_SEP))) yading@11: *tail++ = 0; yading@11: len = strlen(color_string2); yading@11: rgba_color[3] = 255; yading@11: yading@11: if (!av_strcasecmp(color_string2, "random") || !av_strcasecmp(color_string2, "bikeshed")) { yading@11: int rgba = av_get_random_seed(); yading@11: rgba_color[0] = rgba >> 24; yading@11: rgba_color[1] = rgba >> 16; yading@11: rgba_color[2] = rgba >> 8; yading@11: rgba_color[3] = rgba; yading@11: } else if (hex_offset || yading@11: strspn(color_string2, "0123456789ABCDEFabcdef") == len) { yading@11: char *tail; yading@11: unsigned int rgba = strtoul(color_string2, &tail, 16); yading@11: yading@11: if (*tail || (len != 6 && len != 8)) { yading@11: av_log(log_ctx, AV_LOG_ERROR, "Invalid 0xRRGGBB[AA] color string: '%s'\n", color_string2); yading@11: return AVERROR(EINVAL); yading@11: } yading@11: if (len == 8) { yading@11: rgba_color[3] = rgba; yading@11: rgba >>= 8; yading@11: } yading@11: rgba_color[0] = rgba >> 16; yading@11: rgba_color[1] = rgba >> 8; yading@11: rgba_color[2] = rgba; yading@11: } else { yading@11: entry = bsearch(color_string2, yading@11: color_table, yading@11: FF_ARRAY_ELEMS(color_table), yading@11: sizeof(ColorEntry), yading@11: color_table_compare); yading@11: if (!entry) { yading@11: av_log(log_ctx, AV_LOG_ERROR, "Cannot find color '%s'\n", color_string2); yading@11: return AVERROR(EINVAL); yading@11: } yading@11: memcpy(rgba_color, entry->rgb_color, 3); yading@11: } yading@11: yading@11: if (tail) { yading@11: double alpha; yading@11: const char *alpha_string = tail; yading@11: if (!strncmp(alpha_string, "0x", 2)) { yading@11: alpha = strtoul(alpha_string, &tail, 16); yading@11: } else { yading@11: double norm_alpha = strtod(alpha_string, &tail); yading@11: if (norm_alpha < 0.0 || norm_alpha > 1.0) yading@11: alpha = 256; yading@11: else yading@11: alpha = 255 * norm_alpha; yading@11: } yading@11: yading@11: if (tail == alpha_string || *tail || alpha > 255 || alpha < 0) { yading@11: av_log(log_ctx, AV_LOG_ERROR, "Invalid alpha value specifier '%s' in '%s'\n", yading@11: alpha_string, color_string); yading@11: return AVERROR(EINVAL); yading@11: } yading@11: rgba_color[3] = alpha; yading@11: } yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: /* get a positive number between n_min and n_max, for a maximum length yading@11: of len_max. Return -1 if error. */ yading@11: static int date_get_num(const char **pp, yading@11: int n_min, int n_max, int len_max) yading@11: { yading@11: int i, val, c; yading@11: const char *p; yading@11: yading@11: p = *pp; yading@11: val = 0; yading@11: for(i = 0; i < len_max; i++) { yading@11: c = *p; yading@11: if (!av_isdigit(c)) yading@11: break; yading@11: val = (val * 10) + c - '0'; yading@11: p++; yading@11: } yading@11: /* no number read ? */ yading@11: if (p == *pp) yading@11: return -1; yading@11: if (val < n_min || val > n_max) yading@11: return -1; yading@11: *pp = p; yading@11: return val; yading@11: } yading@11: yading@11: char *av_small_strptime(const char *p, const char *fmt, struct tm *dt) yading@11: { yading@11: int c, val; yading@11: yading@11: for(;;) { yading@11: /* consume time string until a non whitespace char is found */ yading@11: while (av_isspace(*fmt)) { yading@11: while (av_isspace(*p)) yading@11: p++; yading@11: fmt++; yading@11: } yading@11: c = *fmt++; yading@11: if (c == '\0') { yading@11: return (char *)p; yading@11: } else if (c == '%') { yading@11: c = *fmt++; yading@11: switch(c) { yading@11: case 'H': yading@11: case 'J': yading@11: val = date_get_num(&p, 0, c == 'H' ? 23 : INT_MAX, 2); yading@11: if (val == -1) yading@11: return NULL; yading@11: dt->tm_hour = val; yading@11: break; yading@11: case 'M': yading@11: val = date_get_num(&p, 0, 59, 2); yading@11: if (val == -1) yading@11: return NULL; yading@11: dt->tm_min = val; yading@11: break; yading@11: case 'S': yading@11: val = date_get_num(&p, 0, 59, 2); yading@11: if (val == -1) yading@11: return NULL; yading@11: dt->tm_sec = val; yading@11: break; yading@11: case 'Y': yading@11: val = date_get_num(&p, 0, 9999, 4); yading@11: if (val == -1) yading@11: return NULL; yading@11: dt->tm_year = val - 1900; yading@11: break; yading@11: case 'm': yading@11: val = date_get_num(&p, 1, 12, 2); yading@11: if (val == -1) yading@11: return NULL; yading@11: dt->tm_mon = val - 1; yading@11: break; yading@11: case 'd': yading@11: val = date_get_num(&p, 1, 31, 2); yading@11: if (val == -1) yading@11: return NULL; yading@11: dt->tm_mday = val; yading@11: break; yading@11: case '%': yading@11: goto match; yading@11: default: yading@11: return NULL; yading@11: } yading@11: } else { yading@11: match: yading@11: if (c != *p) yading@11: return NULL; yading@11: p++; yading@11: } yading@11: } yading@11: } yading@11: yading@11: time_t av_timegm(struct tm *tm) yading@11: { yading@11: time_t t; yading@11: yading@11: int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday; yading@11: yading@11: if (m < 3) { yading@11: m += 12; yading@11: y--; yading@11: } yading@11: yading@11: t = 86400LL * yading@11: (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469); yading@11: yading@11: t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec; yading@11: yading@11: return t; yading@11: } yading@11: yading@11: int av_parse_time(int64_t *timeval, const char *timestr, int duration) yading@11: { yading@11: const char *p, *q; yading@11: int64_t t; yading@11: time_t now; yading@11: struct tm dt = { 0 }; yading@11: int today = 0, negative = 0, microseconds = 0; yading@11: int i; yading@11: static const char * const date_fmt[] = { yading@11: "%Y-%m-%d", yading@11: "%Y%m%d", yading@11: }; yading@11: static const char * const time_fmt[] = { yading@11: "%H:%M:%S", yading@11: "%H%M%S", yading@11: }; yading@11: yading@11: p = timestr; yading@11: q = NULL; yading@11: *timeval = INT64_MIN; yading@11: if (!duration) { yading@11: now = time(0); yading@11: yading@11: if (!av_strcasecmp(timestr, "now")) { yading@11: *timeval = (int64_t) now * 1000000; yading@11: return 0; yading@11: } yading@11: yading@11: /* parse the year-month-day part */ yading@11: for (i = 0; i < FF_ARRAY_ELEMS(date_fmt); i++) { yading@11: q = av_small_strptime(p, date_fmt[i], &dt); yading@11: if (q) yading@11: break; yading@11: } yading@11: yading@11: /* if the year-month-day part is missing, then take the yading@11: * current year-month-day time */ yading@11: if (!q) { yading@11: today = 1; yading@11: q = p; yading@11: } yading@11: p = q; yading@11: yading@11: if (*p == 'T' || *p == 't' || *p == ' ') yading@11: p++; yading@11: yading@11: /* parse the hour-minute-second part */ yading@11: for (i = 0; i < FF_ARRAY_ELEMS(time_fmt); i++) { yading@11: q = av_small_strptime(p, time_fmt[i], &dt); yading@11: if (q) yading@11: break; yading@11: } yading@11: } else { yading@11: /* parse timestr as a duration */ yading@11: if (p[0] == '-') { yading@11: negative = 1; yading@11: ++p; yading@11: } yading@11: /* parse timestr as HH:MM:SS */ yading@11: q = av_small_strptime(p, "%J:%M:%S", &dt); yading@11: if (!q) { yading@11: /* parse timestr as MM:SS */ yading@11: q = av_small_strptime(p, "%M:%S", &dt); yading@11: dt.tm_hour = 0; yading@11: } yading@11: if (!q) { yading@11: /* parse timestr as S+ */ yading@11: dt.tm_sec = strtol(p, (void *)&q, 10); yading@11: if (q == p) /* the parsing didn't succeed */ yading@11: return AVERROR(EINVAL); yading@11: dt.tm_min = 0; yading@11: dt.tm_hour = 0; yading@11: } yading@11: } yading@11: yading@11: /* Now we have all the fields that we can get */ yading@11: if (!q) yading@11: return AVERROR(EINVAL); yading@11: yading@11: /* parse the .m... part */ yading@11: if (*q == '.') { yading@11: int n; yading@11: q++; yading@11: for (n = 100000; n >= 1; n /= 10, q++) { yading@11: if (!av_isdigit(*q)) yading@11: break; yading@11: microseconds += n * (*q - '0'); yading@11: } yading@11: while (av_isdigit(*q)) yading@11: q++; yading@11: } yading@11: yading@11: if (duration) { yading@11: t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec; yading@11: } else { yading@11: int is_utc = *q == 'Z' || *q == 'z'; yading@11: q += is_utc; yading@11: if (today) { /* fill in today's date */ yading@11: struct tm dt2 = is_utc ? *gmtime(&now) : *localtime(&now); yading@11: dt2.tm_hour = dt.tm_hour; yading@11: dt2.tm_min = dt.tm_min; yading@11: dt2.tm_sec = dt.tm_sec; yading@11: dt = dt2; yading@11: } yading@11: t = is_utc ? av_timegm(&dt) : mktime(&dt); yading@11: } yading@11: yading@11: /* Check that we are at the end of the string */ yading@11: if (*q) yading@11: return AVERROR(EINVAL); yading@11: yading@11: t *= 1000000; yading@11: t += microseconds; yading@11: *timeval = negative ? -t : t; yading@11: return 0; yading@11: } yading@11: yading@11: int av_find_info_tag(char *arg, int arg_size, const char *tag1, const char *info) yading@11: { yading@11: const char *p; yading@11: char tag[128], *q; yading@11: yading@11: p = info; yading@11: if (*p == '?') yading@11: p++; yading@11: for(;;) { yading@11: q = tag; yading@11: while (*p != '\0' && *p != '=' && *p != '&') { yading@11: if ((q - tag) < sizeof(tag) - 1) yading@11: *q++ = *p; yading@11: p++; yading@11: } yading@11: *q = '\0'; yading@11: q = arg; yading@11: if (*p == '=') { yading@11: p++; yading@11: while (*p != '&' && *p != '\0') { yading@11: if ((q - arg) < arg_size - 1) { yading@11: if (*p == '+') yading@11: *q++ = ' '; yading@11: else yading@11: *q++ = *p; yading@11: } yading@11: p++; yading@11: } yading@11: } yading@11: *q = '\0'; yading@11: if (!strcmp(tag, tag1)) yading@11: return 1; yading@11: if (*p != '&') yading@11: break; yading@11: p++; yading@11: } yading@11: return 0; yading@11: } yading@11: yading@11: #ifdef TEST yading@11: yading@11: static uint32_t randomv = MKTAG('L','A','V','U'); yading@11: yading@11: static uint32_t av_get_random_seed_deterministic(void) yading@11: { yading@11: return randomv = randomv * 1664525 + 1013904223; yading@11: } yading@11: yading@11: int main(void) yading@11: { yading@11: printf("Testing av_parse_video_rate()\n"); yading@11: { yading@11: int i; yading@11: static const char *const rates[] = { yading@11: "-inf", yading@11: "inf", yading@11: "nan", yading@11: "123/0", yading@11: "-123 / 0", yading@11: "", yading@11: "/", yading@11: " 123 / 321", yading@11: "foo/foo", yading@11: "foo/1", yading@11: "1/foo", yading@11: "0/0", yading@11: "/0", yading@11: "1/", yading@11: "1", yading@11: "0", yading@11: "-123/123", yading@11: "-foo", yading@11: "123.23", yading@11: ".23", yading@11: "-.23", yading@11: "-0.234", yading@11: "-0.0000001", yading@11: " 21332.2324 ", yading@11: " -21332.2324 ", yading@11: }; yading@11: yading@11: for (i = 0; i < FF_ARRAY_ELEMS(rates); i++) { yading@11: int ret; yading@11: AVRational q = { 0, 0 }; yading@11: ret = av_parse_video_rate(&q, rates[i]); yading@11: printf("'%s' -> %d/%d %s\n", yading@11: rates[i], q.num, q.den, ret ? "ERROR" : "OK"); yading@11: } yading@11: } yading@11: yading@11: printf("\nTesting av_parse_color()\n"); yading@11: { yading@11: int i; yading@11: uint8_t rgba[4]; yading@11: static const char *const color_names[] = { yading@11: "bikeshed", yading@11: "RaNdOm", yading@11: "foo", yading@11: "red", yading@11: "Red ", yading@11: "RED", yading@11: "Violet", yading@11: "Yellow", yading@11: "Red", yading@11: "0x000000", yading@11: "0x0000000", yading@11: "0xff000000", yading@11: "0x3e34ff", yading@11: "0x3e34ffaa", yading@11: "0xffXXee", yading@11: "0xfoobar", yading@11: "0xffffeeeeeeee", yading@11: "#ff0000", yading@11: "#ffXX00", yading@11: "ff0000", yading@11: "ffXX00", yading@11: "red@foo", yading@11: "random@10", yading@11: "0xff0000@1.0", yading@11: "red@", yading@11: "red@0xfff", yading@11: "red@0xf", yading@11: "red@2", yading@11: "red@0.1", yading@11: "red@-1", yading@11: "red@0.5", yading@11: "red@1.0", yading@11: "red@256", yading@11: "red@10foo", yading@11: "red@-1.0", yading@11: "red@-0.0", yading@11: }; yading@11: yading@11: av_log_set_level(AV_LOG_DEBUG); yading@11: yading@11: for (i = 0; i < FF_ARRAY_ELEMS(color_names); i++) { yading@11: if (av_parse_color(rgba, color_names[i], -1, NULL) >= 0) yading@11: printf("%s -> R(%d) G(%d) B(%d) A(%d)\n", yading@11: color_names[i], rgba[0], rgba[1], rgba[2], rgba[3]); yading@11: else yading@11: printf("%s -> error\n", color_names[i]); yading@11: } yading@11: } yading@11: yading@11: printf("\nTesting av_small_strptime()\n"); yading@11: { yading@11: int i; yading@11: struct tm tm = { 0 }; yading@11: struct fmt_timespec_entry { yading@11: const char *fmt, *timespec; yading@11: } fmt_timespec_entries[] = { yading@11: { "%Y-%m-%d", "2012-12-21" }, yading@11: { "%Y - %m - %d", "2012-12-21" }, yading@11: { "%Y-%m-%d %H:%M:%S", "2012-12-21 20:12:21" }, yading@11: { " %Y - %m - %d %H : %M : %S", " 2012 - 12 - 21 20 : 12 : 21" }, yading@11: }; yading@11: yading@11: av_log_set_level(AV_LOG_DEBUG); yading@11: for (i = 0; i < FF_ARRAY_ELEMS(fmt_timespec_entries); i++) { yading@11: char *p; yading@11: struct fmt_timespec_entry *e = &fmt_timespec_entries[i]; yading@11: printf("fmt:'%s' spec:'%s' -> ", e->fmt, e->timespec); yading@11: p = av_small_strptime(e->timespec, e->fmt, &tm); yading@11: if (p) { yading@11: printf("%04d-%02d-%2d %02d:%02d:%02d\n", yading@11: 1900+tm.tm_year, tm.tm_mon+1, tm.tm_mday, yading@11: tm.tm_hour, tm.tm_min, tm.tm_sec); yading@11: } else { yading@11: printf("error\n"); yading@11: } yading@11: } yading@11: } yading@11: yading@11: printf("\nTesting av_parse_time()\n"); yading@11: { yading@11: int i; yading@11: int64_t tv; yading@11: time_t tvi; yading@11: struct tm *tm; yading@11: static char tzstr[] = "TZ=CET-1"; yading@11: const char *time_string[] = { yading@11: "now", yading@11: "12:35:46", yading@11: "2000-12-20 0:02:47.5z", yading@11: "2000-12-20T010247.6", yading@11: }; yading@11: const char *duration_string[] = { yading@11: "2:34:56.79", yading@11: "-1:23:45.67", yading@11: "42.1729", yading@11: "-1729.42", yading@11: "12:34", yading@11: }; yading@11: yading@11: av_log_set_level(AV_LOG_DEBUG); yading@11: putenv(tzstr); yading@11: printf("(now is 2012-03-17 09:14:13 +0100, local time is UTC+1)\n"); yading@11: for (i = 0; i < FF_ARRAY_ELEMS(time_string); i++) { yading@11: printf("%-24s -> ", time_string[i]); yading@11: if (av_parse_time(&tv, time_string[i], 0)) { yading@11: printf("error\n"); yading@11: } else { yading@11: tvi = tv / 1000000; yading@11: tm = gmtime(&tvi); yading@11: printf("%14"PRIi64".%06d = %04d-%02d-%02dT%02d:%02d:%02dZ\n", yading@11: tv / 1000000, (int)(tv % 1000000), yading@11: tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, yading@11: tm->tm_hour, tm->tm_min, tm->tm_sec); yading@11: } yading@11: } yading@11: for (i = 0; i < FF_ARRAY_ELEMS(duration_string); i++) { yading@11: printf("%-24s -> ", duration_string[i]); yading@11: if (av_parse_time(&tv, duration_string[i], 1)) { yading@11: printf("error\n"); yading@11: } else { yading@11: printf("%+21"PRIi64"\n", tv); yading@11: } yading@11: } yading@11: } yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: #endif /* TEST */