⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utils.c

📁 Trolltech公司发布的图形界面操作系统。可在qt-embedded-2.3.7平台上编译为嵌入式图形界面操作系统。
💻 C
📖 第 1 页 / 共 3 页
字号:
static int has_codec_parameters(AVCodecContext *enc){    int val;    switch(enc->codec_type) {    case CODEC_TYPE_AUDIO:        val = enc->sample_rate;        break;    case CODEC_TYPE_VIDEO:        val = enc->width;        break;    default:        val = 1;        break;    }    return (val != 0);}/** * Read the beginning of a media file to get stream information. This * is useful for file formats with no headers such as MPEG. This * function also compute the real frame rate in case of mpeg2 repeat * frame mode. * * @param ic media file handle * @return >=0 if OK. AVERROR_xxx if error.   */int av_find_stream_info(AVFormatContext *ic){    int i, count, ret, got_picture, size, read_size;    AVCodec *codec;    AVStream *st;    AVPacket *pkt;    AVPicture picture;    AVPacketList *pktl=NULL, **ppktl;    short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];    UINT8 *ptr;    int min_read_size, max_read_size;    /* typical mpeg ts rate is 40 Mbits. DVD rate is about 10       Mbits. We read at most 0.1 second of file to find all streams */    /* XXX: base it on stream bitrate when possible */    if (ic->iformat == &mpegts_demux) {        /* maximum number of bytes we accept to read to find all the streams           in a file */        min_read_size = 3000000;    } else {        min_read_size = 125000;    }    /* max read size is 2 seconds of video max */    max_read_size = min_read_size * 20;    /* set initial codec state */    for(i=0;i<ic->nb_streams;i++) {        st = ic->streams[i];        if (has_codec_parameters(&st->codec))            st->codec_info_state = CSTATE_FOUND;        else            st->codec_info_state = CSTATE_NOTFOUND;        st->codec_info_nb_repeat_frames = 0;        st->codec_info_nb_real_frames = 0;    }    count = 0;    read_size = 0;    ppktl = &ic->packet_buffer;    for(;;) {        /* check if one codec still needs to be handled */        for(i=0;i<ic->nb_streams;i++) {            st = ic->streams[i];            if (st->codec_info_state != CSTATE_FOUND)                break;        }        if (i == ic->nb_streams) {            /* NOTE: if the format has no header, then we need to read               some packets to get most of the streams, so we cannot               stop here */            if (!(ic->iformat->flags & AVFMT_NOHEADER) ||                read_size >= min_read_size) {                /* if we found the info for all the codecs, we can stop */                ret = count;                break;            }        } else {            /* we did not get all the codec info, but we read too much data */            if (read_size >= max_read_size) {                ret = count;                break;            }        }        pktl = av_mallocz(sizeof(AVPacketList));        if (!pktl) {            ret = AVERROR_NOMEM;            break;        }        /* add the packet in the buffered packet list */        *ppktl = pktl;        ppktl = &pktl->next;        /* NOTE: a new stream can be added there if no header in file           (AVFMT_NOHEADER) */        pkt = &pktl->pkt;        if (ic->iformat->read_packet(ic, pkt) < 0) {            /* EOF or error */            ret = -1; /* we could not have all the codec parameters before EOF */            if ((ic->iformat->flags & AVFMT_NOHEADER) &&                i == ic->nb_streams)                ret = 0;            break;        }        read_size += pkt->size;        /* open new codecs */        for(i=0;i<ic->nb_streams;i++) {            st = ic->streams[i];            if (st->codec_info_state == CSTATE_NOTFOUND) {                /* set to found in case of error */                st->codec_info_state = CSTATE_FOUND;                 codec = avcodec_find_decoder(st->codec.codec_id);                if (codec) {                    if(codec->capabilities & CODEC_CAP_TRUNCATED)                        st->codec.flags |= CODEC_FLAG_TRUNCATED;                    ret = avcodec_open(&st->codec, codec);                    if (ret >= 0)                        st->codec_info_state = CSTATE_DECODING;                }            }        }        st = ic->streams[pkt->stream_index];        if (st->codec_info_state == CSTATE_DECODING) {            /* decode the data and update codec parameters */            ptr = pkt->data;            size = pkt->size;            while (size > 0) {                switch(st->codec.codec_type) {                case CODEC_TYPE_VIDEO:                    ret = avcodec_decode_video(&st->codec, &picture,                                                &got_picture, ptr, size);                    break;                case CODEC_TYPE_AUDIO:                    ret = avcodec_decode_audio(&st->codec, samples,                                                &got_picture, ptr, size);                    break;                default:                    ret = -1;                    break;                }                if (ret < 0) {                    /* if error, simply ignore because another packet                       may be OK */                    break;                }                if (got_picture) {                    /* we got the parameters - now we can stop                       examining this stream */                    /* XXX: add a codec info so that we can decide if                       the codec can repeat frames */                    if (st->codec.codec_id == CODEC_ID_MPEG1VIDEO &&                         ic->iformat != &mpegts_demux &&                        st->codec.sub_id == 2) {                        /* for mpeg2 video, we want to know the real                           frame rate, so we decode 40 frames. In mpeg                           TS case we do not do it because it would be                           too long */                        st->codec_info_nb_real_frames++;                        st->codec_info_nb_repeat_frames += st->codec.repeat_pict;#if 0                        /* XXX: testing */                        if ((st->codec_info_nb_real_frames % 24) == 23) {                            st->codec_info_nb_repeat_frames += 2;                        }#endif                        /* stop after 40 frames */                        if (st->codec_info_nb_real_frames >= 40) {                            st->r_frame_rate = (st->codec.frame_rate *                                                 st->codec_info_nb_real_frames) /                                (st->codec_info_nb_real_frames +                                  (st->codec_info_nb_repeat_frames >> 1));                            goto close_codec;                        }                    } else {                    close_codec:                        st->codec_info_state = CSTATE_FOUND;                        avcodec_close(&st->codec);                        break;                    }                }                ptr += ret;                size -= ret;            }        }        count++;    }    /* close each codec if there are opened */    for(i=0;i<ic->nb_streams;i++) {        st = ic->streams[i];        if (st->codec_info_state == CSTATE_DECODING)            avcodec_close(&st->codec);    }    /* set real frame rate info */    for(i=0;i<ic->nb_streams;i++) {        st = ic->streams[i];        if (st->codec.codec_type == CODEC_TYPE_VIDEO) {            if (!st->r_frame_rate)                st->r_frame_rate = st->codec.frame_rate;        }    }    return ret;}/** * Close a media file (but not its codecs) * * @param s media file handle */void av_close_input_file(AVFormatContext *s){    int i;    if (s->iformat->read_close)        s->iformat->read_close(s);    for(i=0;i<s->nb_streams;i++) {        av_free(s->streams[i]);    }    if (s->packet_buffer) {        AVPacketList *p, *p1;        p = s->packet_buffer;        while (p != NULL) {            p1 = p->next;            av_free_packet(&p->pkt);            av_free(p);            p = p1;        }        s->packet_buffer = NULL;    }    if (!(s->iformat->flags & AVFMT_NOFILE)) {        url_fclose(&s->pb);    }    av_freep(&s->priv_data);    av_free(s);}/** * Add a new stream to a media file. Can only be called in the * read_header function. If the flag AVFMT_NOHEADER is in the format * description, then new streams can be added in read_packet too. * * * @param s media file handle * @param id file format dependent stream id */AVStream *av_new_stream(AVFormatContext *s, int id){    AVStream *st;    if (s->nb_streams >= MAX_STREAMS)        return NULL;    st = av_mallocz(sizeof(AVStream));    if (!st)        return NULL;    st->index = s->nb_streams;    st->id = id;    s->streams[s->nb_streams++] = st;    return st;}/************************************************************//* output media file *//** * allocate the stream private data and write the stream header to an * output media file * * @param s media file handle * @return 0 if OK. AVERROR_xxx if error.   */int av_write_header(AVFormatContext *s){    int ret, i;    AVStream *st;    s->priv_data = av_mallocz(s->oformat->priv_data_size);    if (!s->priv_data)        return AVERROR_NOMEM;    /* default pts settings is MPEG like */    av_set_pts_info(s, 33, 1, 90000);    ret = s->oformat->write_header(s);    if (ret < 0)        return ret;    /* init PTS generation */    for(i=0;i<s->nb_streams;i++) {        st = s->streams[i];        switch (st->codec.codec_type) {        case CODEC_TYPE_AUDIO:            av_frac_init(&st->pts, 0, 0,                          (INT64)s->pts_num * st->codec.sample_rate);            break;        case CODEC_TYPE_VIDEO:            av_frac_init(&st->pts, 0, 0,                          (INT64)s->pts_num * st->codec.frame_rate);            break;        default:            break;        }    }    return 0;}/** * Write a packet to an output media file. The packet shall contain * one audio or video frame. * * @param s media file handle * @param stream_index stream index * @param buf buffer containing the frame data * @param size size of buffer * @return < 0 if error, = 0 if OK, 1 if end of stream wanted. */int av_write_frame(AVFormatContext *s, int stream_index, const uint8_t *buf,                    int size){    AVStream *st;    INT64 pts_mask;    int ret, frame_size;    st = s->streams[stream_index];    pts_mask = (1LL << s->pts_wrap_bits) - 1;    ret = s->oformat->write_packet(s, stream_index, (uint8_t *)buf, size,                                    st->pts.val & pts_mask);    if (ret < 0)        return ret;    /* update pts */    switch (st->codec.codec_type) {    case CODEC_TYPE_AUDIO:        if (st->codec.frame_size <= 1) {            frame_size = size / st->codec.channels;            /* specific hack for pcm codecs because no frame size is provided */            switch(st->codec.codec_id) {            case CODEC_ID_PCM_S16LE:            case CODEC_ID_PCM_S16BE:            case CODEC_ID_PCM_U16LE:            case CODEC_ID_PCM_U16BE:                frame_size >>= 1;                break;            default:                break;            }        } else {            frame_size = st->codec.frame_size;        }        av_frac_add(&st->pts,                     (INT64)s->pts_den * frame_size);        break;    case CODEC_TYPE_VIDEO:        av_frac_add(&st->pts,                     (INT64)s->pts_den * FRAME_RATE_BASE);        break;    default:        break;    }    return ret;}/** * write the stream trailer to an output media file and and free the * file private data. * * @param s media file handle * @return 0 if OK. AVERROR_xxx if error.  */int av_write_trailer(AVFormatContext *s){    int ret;    ret = s->oformat->write_trailer(s);    av_freep(&s->priv_data);    return ret;}/* "user interface" functions */void dump_format(AVFormatContext *ic,                 int index,                  const char *url,                 int is_output){    int i, flags;    char buf[256];    fprintf(stderr, "%s #%d, %s, %s '%s':\n",             is_output ? "Output" : "Input",            index,             is_output ? ic->oformat->name : ic->iformat->name,             is_output ? "to" : "from", url);    for(i=0;i<ic->nb_streams;i++) {        AVStream *st = ic->streams[i];        avcodec_string(buf, sizeof(buf), &st->codec, is_output);        fprintf(stderr, "  Stream #%d.%d", index, i);        /* the pid is an important information, so we display it */        /* XXX: add a generic system */        if (is_output)            flags = ic->oformat->flags;        else            flags = ic->iformat->flags;        if (flags & AVFMT_SHOW_IDS) {            fprintf(stderr, "[0x%x]", st->id);        }        fprintf(stderr, ": %s\n", buf);    }}typedef struct {    const char *str;    int width, height;} SizeEntry;static SizeEntry sizes[] = {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -