Skip to content
Snippets Groups Projects
utils.c 97.5 KiB
Newer Older
  • Learn to ignore specific revisions
  •             }
            } 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));
    
    void av_set_pts_info(AVStream *s, int pts_wrap_bits,
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                         int pts_num, int pts_den)
    {
        s->pts_wrap_bits = pts_wrap_bits;
    
        s->time_base.num = pts_num;
        s->time_base.den = pts_den;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    /* fraction handling */
    
    /**
    
     * f = val + (num / den) + 0.5.
     *
     * 'num' is normalized so that it is such as 0 <= num < den.
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     *
     * @param f fractional number
     * @param val integer value
     * @param num must be >= 0
    
     * @param den must be >= 1
    
    static void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        num += (den >> 1);
        if (num >= den) {
            val += num / den;
            num = num % den;
        }
        f->val = val;
        f->num = num;
        f->den = den;
    }
    
    /**
    
     * Fractional addition to f: f = f + (incr / f->den).
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     *
     * @param f fractional number
     * @param incr increment, can be positive or negative
     */
    
    static void av_frac_add(AVFrac *f, int64_t incr)
    
        int64_t num, den;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
        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;
    }