Skip to content
Snippets Groups Projects
utils.c 94.8 KiB
Newer Older
  • Learn to ignore specific revisions
  •     int len, i, j, c;
    
        for(i=0;i<size;i+=16) {
            len = size - i;
            if (len > 16)
                len = 16;
    
            fprintf(f, "%08x ", i);
    
                    fprintf(f, " %02x", buf[i+j]);
    
    /**
     * Print on 'f' a nice dump of a packet
     * @param f stream for output
     * @param pkt packet to dump
     * @param dump_payload true if the payload must be displayed too
     */
    
     //FIXME needs to know the time_base
    
    void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload)
    {
        fprintf(f, "stream #%d:\n", pkt->stream_index);
        fprintf(f, "  keyframe=%d\n", ((pkt->flags & PKT_FLAG_KEY) != 0));
        fprintf(f, "  duration=%0.3f\n", (double)pkt->duration / AV_TIME_BASE);
    
        /* DTS is _always_ valid after av_read_frame() */
        fprintf(f, "  dts=");
        if (pkt->dts == AV_NOPTS_VALUE)
            fprintf(f, "N/A");
        else
            fprintf(f, "%0.3f", (double)pkt->dts / AV_TIME_BASE);
    
        /* PTS may be not known if B frames are present */
        fprintf(f, "  pts=");
        if (pkt->pts == AV_NOPTS_VALUE)
            fprintf(f, "N/A");
        else
            fprintf(f, "%0.3f", (double)pkt->pts / AV_TIME_BASE);
        fprintf(f, "\n");
        fprintf(f, "  size=%d\n", pkt->size);
        if (dump_payload)
            av_hex_dump(f, pkt->data, pkt->size);
    }
    
    
    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)
    {
        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 (authorization_size > 0)
            authorization[0] = '\0';
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (*p == '\0') {
            if (proto_size > 0)
                proto[0] = '\0';
            if (hostname_size > 0)
                hostname[0] = '\0';
            p = url;
        } else {
    
            char *at,*slash; // PETR: position of '@' character and '/' character
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            p++;
            if (*p == '/')
                p++;
            if (*p == '/')
                p++;
    
            at = strchr(p,'@'); // PETR: get the position of '@'
            slash = strchr(p,'/');  // PETR: get position of '/' - end of hostname
            if (at && slash && at > slash) at = NULL; // PETR: not interested in '@' behind '/'
    
            q = at ? authorization : hostname;  // PETR: if '@' exists starting with auth.
    
             while ((at || *p != ':') && *p != '/' && *p != '?' && *p != '\0') { // PETR:
                if (*p == '@') {    // PETR: passed '@'
                  if (authorization_size > 0)
                      *q = '\0';
                  q = hostname;
                  at = NULL;
                } else if (!at) {   // PETR: hostname
                  if ((q - hostname) < hostname_size - 1)
                      *q++ = *p;
                } else {
                  if ((q - authorization) < authorization_size - 1)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    *q++ = *p;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                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
    /**
    
     * @param s stream
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     * @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)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     * @param pts_den denominator to convert to seconds (MPEG: 90000)
     */
    
    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;
    }
    
    
    static void av_frac_set(AVFrac *f, int64_t val)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        f->val = val;
        f->num = f->den >> 1;
    }
    
    /**
    
     * Fractionnal 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;
    }
    
    
    /**
     * register a new image format
     * @param img_fmt Image format descriptor
     */
    void av_register_image_format(AVImageFormat *img_fmt)
    {
        AVImageFormat **p;
    
        p = &first_image_format;
        while (*p != NULL) p = &(*p)->next;
        *p = img_fmt;
        img_fmt->next = NULL;
    }
    
    
    /**
     * Guesses image format based on data in the image.
     */
    
    AVImageFormat *av_probe_image_format(AVProbeData *pd)
    {
        AVImageFormat *fmt1, *fmt;
        int score, score_max;
    
        fmt = NULL;
        score_max = 0;
        for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) {
            if (fmt1->img_probe) {
                score = fmt1->img_probe(pd);
                if (score > score_max) {
                    score_max = score;
                    fmt = fmt1;
                }
            }
        }
        return fmt;
    }
    
    
    /**
     * Guesses image format based on file name extensions.
     */
    
    AVImageFormat *guess_image_format(const char *filename)
    {
        AVImageFormat *fmt1;
    
        for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) {
            if (fmt1->extensions && match_ext(filename, fmt1->extensions))
                return fmt1;
        }
        return NULL;
    }
    
    /**
    
     * Read an image from a stream.
    
     * @param gb byte stream containing the image
     * @param fmt image format, NULL if probing is required
     */
    int av_read_image(ByteIOContext *pb, const char *filename,
                      AVImageFormat *fmt,
                      int (*alloc_cb)(void *, AVImageInfo *info), void *opaque)
    {
    
        AVProbeData probe_data, *pd = &probe_data;
        offset_t pos;
        int ret;
    
        if (!fmt) {
    
            pd->filename = filename;
    
            pd->buf = buf;
            pos = url_ftell(pb);
    
            pd->buf_size = get_buffer(pb, buf, PROBE_BUF_MIN);
    
            url_fseek(pb, pos, SEEK_SET);
            fmt = av_probe_image_format(pd);
        }
        if (!fmt)
            return AVERROR_NOFMT;
        ret = fmt->img_read(pb, alloc_cb, opaque);
        return ret;
    }
    
    /**
     * Write an image to a stream.
     * @param pb byte stream for the image output
     * @param fmt image format
     * @param img image data and informations
     */
    int av_write_image(ByteIOContext *pb, AVImageFormat *fmt, AVImageInfo *img)
    {
        return fmt->img_write(pb, img);
    }