Skip to content
Snippets Groups Projects
utils.c 104 KiB
Newer Older
  • Learn to ignore specific revisions
  • Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (len > 0)
            lastch = datestr[len - 1];
        else
            lastch = '\0';
    
        is_utc = (lastch == 'z' || lastch == 'Z');
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
        p = datestr;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        q = NULL;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (!duration) {
    
            if (!strncasecmp(datestr, "now", len))
                return (int64_t) now * 1000000;
    
    
            /* parse the year-month-day part */
    
            for (i = 0; i < FF_ARRAY_ELEMS(date_fmt); i++) {
    
                q = small_strptime(p, date_fmt[i], &dt);
    
            /* if the year-month-day part is missing, then take the
             * current year-month-day time */
    
            if (!q) {
                if (is_utc) {
                    dt = *gmtime(&now);
                } else {
                    dt = *localtime(&now);
                }
                dt.tm_hour = dt.tm_min = dt.tm_sec = 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            } else {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
    
            /* parse the hour-minute-second part */
    
            for (i = 0; i < FF_ARRAY_ELEMS(time_fmt); i++) {
    
                q = small_strptime(p, time_fmt[i], &dt);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                if (q) {
                    break;
                }
            }
        } else {
    
            /* parse datestr as a duration */
    
            if (p[0] == '-') {
                negative = 1;
                ++p;
            }
    
            /* parse datestr as HH:MM:SS */
    
            q = small_strptime(p, time_fmt[0], &dt);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (!q) {
    
                /* parse datestr as S+ */
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                dt.tm_sec = strtol(p, (char **)&q, 10);
    
                if (q == p)
                    /* the parsing didn't succeed */
                    return INT64_MIN;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                dt.tm_min = 0;
                dt.tm_hour = 0;
    
            }
        }
    
        /* Now we have all the fields that we can get */
        if (!q) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
            t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec;
    
            dt.tm_isdst = -1;       /* unknown */
            if (is_utc) {
                t = mktimegm(&dt);
            } else {
                t = mktime(&dt);
            }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
        /* parse the .m... part */
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            int val, n;
    
            q++;
            for (val = 0, n = 100000; n >= 1; n /= 10, q++) {
    
                if (!isdigit(*q))
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
            t += val;
        }
    
        return negative ? -t : t;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
    {
        const char *p;
        char tag[128], *q;
    
        p = info;
        if (*p == '?')
            p++;
        for(;;) {
            q = tag;
            while (*p != '\0' && *p != '=' && *p != '&') {
                if ((q - tag) < sizeof(tag) - 1)
                    *q++ = *p;
                p++;
            }
            *q = '\0';
            q = arg;
            if (*p == '=') {
                p++;
                while (*p != '&' && *p != '\0') {
    
                    if ((q - arg) < arg_size - 1) {
                        if (*p == '+')
                            *q++ = ' ';
                        else
                            *q++ = *p;
                    }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    p++;
                }
                *q = '\0';
            }
    
            if (!strcmp(tag, tag1))
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                return 1;
            if (*p != '&')
                break;
    
    int av_get_frame_filename(char *buf, int buf_size,
                              const char *path, int number)
    
    {
        const char *p;
    
    
        q = buf;
        p = path;
        percentd_found = 0;
        for(;;) {
            c = *p++;
            if (c == '\0')
                break;
            if (c == '%') {
    
                do {
                    nd = 0;
                    while (isdigit(*p)) {
                        nd = nd * 10 + *p++ - '0';
                    }
                    c = *p++;
                } while (isdigit(c));
    
    
                switch(c) {
                case '%':
                    goto addchar;
                case 'd':
                    if (percentd_found)
                        goto fail;
                    percentd_found = 1;
                    snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
                    len = strlen(buf1);
                    if ((q - buf + len) > buf_size - 1)
                        goto fail;
                    memcpy(q, buf1, len);
                    q += len;
                    break;
                default:
                    goto fail;
                }
            } else {
            addchar:
                if ((q - buf) < buf_size - 1)
                    *q++ = c;
            }
        }
        if (!percentd_found)
            goto fail;
        *q = '\0';
        return 0;
     fail:
        *q = '\0';
        return -1;
    }
    
    
    static void hex_dump_internal(void *avcl, FILE *f, int level, uint8_t *buf, int size)
    
    #define PRINT(...) do { if (!f) av_log(avcl, level, __VA_ARGS__); else fprintf(f, __VA_ARGS__); } while(0)
    
    #undef PRINT
    }
    
    void av_hex_dump(FILE *f, uint8_t *buf, int size)
    {
        hex_dump_internal(NULL, f, 0, buf, size);
    }
    
    void av_hex_dump_log(void *avcl, int level, uint8_t *buf, int size)
    {
        hex_dump_internal(avcl, NULL, level, buf, size);
    
     //FIXME needs to know the time_base
    
    static void pkt_dump_internal(void *avcl, FILE *f, int level, AVPacket *pkt, int dump_payload)
    
    #define PRINT(...) do { if (!f) av_log(avcl, level, __VA_ARGS__); else fprintf(f, __VA_ARGS__); } while(0)
        PRINT("stream #%d:\n", pkt->stream_index);
        PRINT("  keyframe=%d\n", ((pkt->flags & PKT_FLAG_KEY) != 0));
        PRINT("  duration=%0.3f\n", (double)pkt->duration / AV_TIME_BASE);
    
        /* DTS is _always_ valid after av_read_frame() */
    
        if (pkt->dts == AV_NOPTS_VALUE)
    
            PRINT("%0.3f", (double)pkt->dts / AV_TIME_BASE);
    
        /* PTS may not be known if B-frames are present. */
    
        if (pkt->pts == AV_NOPTS_VALUE)
    
            PRINT("%0.3f", (double)pkt->pts / AV_TIME_BASE);
        PRINT("\n");
        PRINT("  size=%d\n", pkt->size);
    #undef PRINT
    
        if (dump_payload)
            av_hex_dump(f, pkt->data, pkt->size);
    }
    
    
    void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload)
    {
        pkt_dump_internal(NULL, f, 0, pkt, dump_payload);
    }
    
    void av_pkt_dump_log(void *avcl, int level, AVPacket *pkt, int dump_payload)
    {
        pkt_dump_internal(avcl, NULL, level, pkt, dump_payload);
    }
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    void url_split(char *proto, int proto_size,
    
                   char *authorization, int authorization_size,
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                   char *hostname, int hostname_size,
                   int *port_ptr,
                   char *path, int path_size,
                   const char *url)
    {
    
    
        if (port_ptr)               *port_ptr = -1;
        if (proto_size > 0)         proto[0] = 0;
        if (authorization_size > 0) authorization[0] = 0;
        if (hostname_size > 0)      hostname[0] = 0;
        if (path_size > 0)          path[0] = 0;
    
        /* parse protocol */
        if ((p = strchr(url, ':'))) {
            av_strlcpy(proto, url, FFMIN(proto_size, p + 1 - url));
            p++; /* skip ':' */
            if (*p == '/') p++;
            if (*p == '/') p++;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        } else {
    
            /* no protocol means plain filename */
            av_strlcpy(path, url, path_size);
            return;
        }
    
        /* separate path from hostname */
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
            av_strlcpy(path, ls, path_size);
    
            ls = &p[strlen(p)]; // XXX
    
        /* the rest is hostname, use that to parse auth/port */
        if (ls != p) {
            /* authorization (user[:pass]@hostname) */
            if ((at = strchr(p, '@')) && at < ls) {
                av_strlcpy(authorization, p,
                           FFMIN(authorization_size, at + 1 - p));
                p = at + 1; /* skip '@' */
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
    
            if (*p == '[' && (brk = strchr(p, ']')) && brk < ls) {
                /* [host]:port */
                av_strlcpy(hostname, p + 1,
                           FFMIN(hostname_size, brk - p));
                if (brk[1] == ':' && port_ptr)
                    *port_ptr = atoi(brk + 2);
            } else if ((col = strchr(p, ':')) && col < ls) {
                av_strlcpy(hostname, p,
                           FFMIN(col + 1 - p, hostname_size));
                if (port_ptr) *port_ptr = atoi(col + 1);
            } else
                av_strlcpy(hostname, p,
                           FFMIN(ls + 1 - p, hostname_size));
    
    char *ff_data_to_hex(char *buff, const uint8_t *src, int s)
    {
        int i;
    
        static const char hex_table[16] = { '0', '1', '2', '3',
                                            '4', '5', '6', '7',
                                            '8', '9', 'A', 'B',
                                            'C', 'D', 'E', 'F' };
    
            buff[i * 2]     = hex_table[src[i] >> 4];
            buff[i * 2 + 1] = hex_table[src[i] & 0xF];
    
    void av_set_pts_info(AVStream *s, int pts_wrap_bits,
    
                         unsigned int pts_num, unsigned int pts_den)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        s->pts_wrap_bits = pts_wrap_bits;
    
        if(av_reduce(&s->time_base.num, &s->time_base.den, pts_num, pts_den, INT_MAX)){
            if(s->time_base.num != pts_num)
                av_log(NULL, AV_LOG_DEBUG, "st:%d removing common factor %d from timebase\n", s->index, pts_num/s->time_base.num);
        }else
            av_log(NULL, AV_LOG_WARNING, "st:%d has too large timebase, reducing\n", s->index);
    
    
        if(!s->time_base.num || !s->time_base.den)
            s->time_base.num= s->time_base.den= 0;