Skip to content
Snippets Groups Projects
utils.c 30.3 KiB
Newer Older
  • Learn to ignore specific revisions
  •     for(;;) {
            c = *p++;
            if (c == '\0')
                break;
            if (c == '%') {
                nd = 0;
                while (*p >= '0' && *p <= '9') {
                    nd = nd * 10 + *p++ - '0';
                }
                c = *p++;
                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;
    }
    
    
    /**
     *
     * Print on stdout a nice hexa dump of a buffer
     * @param buf buffer
     * @param size buffer size
     */
    void av_hex_dump(UINT8 *buf, int size)
    {
        int len, i, j, c;
    
        for(i=0;i<size;i+=16) {
            len = size - i;
            if (len > 16)
                len = 16;
            printf("%08x ", i);
            for(j=0;j<16;j++) {
                if (j < len)
                    printf(" %02x", buf[i+j]);
                else
                    printf("   ");
            }
            printf(" ");
            for(j=0;j<len;j++) {
                c = buf[i+j];
                if (c < ' ' || c > '~')
                    c = '.';
                printf("%c", c);
            }
            printf("\n");
        }
    }
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    void url_split(char *proto, int proto_size,
                   char *hostname, int hostname_size,
                   int *port_ptr,
                   char *path, int path_size,
                   const char *url)
    {
        const char *p;
        char *q;
        int port;
    
        port = -1;
    
        p = url;
        q = proto;
        while (*p != ':' && *p != '\0') {
            if ((q - proto) < proto_size - 1)
                *q++ = *p;
            p++;
        }
        if (proto_size > 0)
            *q = '\0';
        if (*p == '\0') {
            if (proto_size > 0)
                proto[0] = '\0';
            if (hostname_size > 0)
                hostname[0] = '\0';
            p = url;
        } else {
            p++;
            if (*p == '/')
                p++;
            if (*p == '/')
                p++;
            q = hostname;
            while (*p != ':' && *p != '/' && *p != '?' && *p != '\0') {
                if ((q - hostname) < hostname_size - 1)
                    *q++ = *p;
                p++;
            }
            if (hostname_size > 0)
                *q = '\0';
            if (*p == ':') {
                p++;
                port = strtoul(p, (char **)&p, 10);
            }
        }
        if (port_ptr)
            *port_ptr = port;
        pstrcpy(path, path_size, p);
    }
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    /**
     * Set the pts for a given stream
     * @param s stream 
     * @param pts_wrap_bits number of bits effectively used by the pts
     *        (used for wrap control, 33 is the value for MPEG) 
     * @param pts_num numerator to convert to seconds (MPEG: 1) 
     * @param pts_den denominator to convert to seconds (MPEG: 90000)
     */
    void av_set_pts_info(AVFormatContext *s, int pts_wrap_bits,
                         int pts_num, int pts_den)
    {
        s->pts_wrap_bits = pts_wrap_bits;
        s->pts_num = pts_num;
        s->pts_den = pts_den;
    }
    
    /* fraction handling */
    
    /**
     * f = val + (num / den) + 0.5. 'num' is normalized so that it is such
     * as 0 <= num < den.
     *
     * @param f fractional number
     * @param val integer value
     * @param num must be >= 0
     * @param den must be >= 1 
     */
    void av_frac_init(AVFrac *f, INT64 val, INT64 num, INT64 den)
    {
        num += (den >> 1);
        if (num >= den) {
            val += num / den;
            num = num % den;
        }
        f->val = val;
        f->num = num;
        f->den = den;
    }
    
    /* set f to (val + 0.5) */
    void av_frac_set(AVFrac *f, INT64 val)
    {
        f->val = val;
        f->num = f->den >> 1;
    }
    
    /**
     * Fractionnal addition to f: f = f + (incr / f->den)
     *
     * @param f fractional number
     * @param incr increment, can be positive or negative
     */
    void av_frac_add(AVFrac *f, INT64 incr)
    {
        INT64 num, den;
    
        num = f->num + incr;
        den = f->den;
        if (num < 0) {
            f->val += num / den;
            num = num % den;
            if (num < 0) {
                num += den;
                f->val--;
            }
        } else if (num >= den) {
            f->val += num / den;
            num = num % den;
        }
        f->num = num;
    }