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

📄 utils.c

📁 arm平台下的H264编码和解码源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
    /* check filename in case of an image number is expected */    if (fmt->flags & AVFMT_NEEDNUMBER) {        if (filename_number_test(filename) < 0) {             err = AVERROR_NUMEXPECTED;            goto fail;        }    }    err = av_open_input_stream(ic_ptr, pb, filename, fmt, ap);    if (err)        goto fail;    return 0; fail:    if (file_opened)        url_fclose(pb);    *ic_ptr = NULL;    return err;    }/*******************************************************//** * Read a transport packet from a media file. This function is * absolete and should never be used. Use av_read_frame() instead. *  * @param s media file handle * @param pkt is filled  * @return 0 if OK. AVERROR_xxx if error.   */int av_read_packet(AVFormatContext *s, AVPacket *pkt){    return s->iformat->read_packet(s, pkt);}/**********************************************************//* convert the packet time stamp units and handle wrapping. The   wrapping is handled by considering the next PTS/DTS as a delta to   the previous value. We handle the delta as a fraction to avoid any   rounding errors. */static inline int64_t convert_timestamp_units(AVFormatContext *s,                                        int64_t *plast_pkt_pts,                                        int *plast_pkt_pts_frac,                                        int64_t *plast_pkt_stream_pts,                                        int64_t pts){    int64_t stream_pts;    int64_t delta_pts;    int shift, pts_frac;    if (pts != AV_NOPTS_VALUE) {        stream_pts = pts;        if (*plast_pkt_stream_pts != AV_NOPTS_VALUE) {            shift = 64 - s->pts_wrap_bits;            delta_pts = ((stream_pts - *plast_pkt_stream_pts) << shift) >> shift;            /* XXX: overflow possible but very unlikely as it is a delta */            delta_pts = delta_pts * AV_TIME_BASE * s->pts_num;            pts = *plast_pkt_pts + (delta_pts / s->pts_den);            pts_frac = *plast_pkt_pts_frac + (delta_pts % s->pts_den);            if (pts_frac >= s->pts_den) {                pts_frac -= s->pts_den;                pts++;            }        } else {            /* no previous pts, so no wrapping possible */            pts = (int64_t)(((double)stream_pts * AV_TIME_BASE * s->pts_num) /                             (double)s->pts_den);            pts_frac = 0;        }        *plast_pkt_stream_pts = stream_pts;        *plast_pkt_pts = pts;        *plast_pkt_pts_frac = pts_frac;    }    return pts;}/* get the number of samples of an audio frame. Return (-1) if error */static int get_audio_frame_size(AVCodecContext *enc, int size){    int frame_size;    if (enc->frame_size <= 1) {        /* specific hack for pcm codecs because no frame size is           provided */        switch(enc->codec_id) {        case CODEC_ID_PCM_S16LE:        case CODEC_ID_PCM_S16BE:        case CODEC_ID_PCM_U16LE:        case CODEC_ID_PCM_U16BE:            if (enc->channels == 0)                return -1;            frame_size = size / (2 * enc->channels);            break;        case CODEC_ID_PCM_S8:        case CODEC_ID_PCM_U8:        case CODEC_ID_PCM_MULAW:        case CODEC_ID_PCM_ALAW:            if (enc->channels == 0)                return -1;            frame_size = size / (enc->channels);            break;        default:            /* used for example by ADPCM codecs */            if (enc->bit_rate == 0)                return -1;            frame_size = (size * 8 * enc->sample_rate) / enc->bit_rate;            break;        }    } else {        frame_size = enc->frame_size;    }    return frame_size;}/* return the frame duration in seconds, return 0 if not available */static void compute_frame_duration(int *pnum, int *pden,                                   AVFormatContext *s, AVStream *st,                                    AVCodecParserContext *pc, AVPacket *pkt){    int frame_size;    *pnum = 0;    *pden = 0;    switch(st->codec.codec_type) {    case CODEC_TYPE_VIDEO:        *pnum = st->codec.frame_rate_base;        *pden = st->codec.frame_rate;        if (pc && pc->repeat_pict) {            *pden *= 2;            *pnum = (*pnum) * (2 + pc->repeat_pict);        }        break;    case CODEC_TYPE_AUDIO:        frame_size = get_audio_frame_size(&st->codec, pkt->size);        if (frame_size < 0)            break;        *pnum = frame_size;        *pden = st->codec.sample_rate;        break;    default:        break;    }}static void compute_pkt_fields(AVFormatContext *s, AVStream *st,                                AVCodecParserContext *pc, AVPacket *pkt){    int num, den, presentation_delayed;    if (pkt->duration == 0) {        compute_frame_duration(&num, &den, s, st, pc, pkt);        if (den && num) {            pkt->duration = (num * (int64_t)AV_TIME_BASE) / den;        }    }    /* do we have a video B frame ? */    presentation_delayed = 0;    if (st->codec.codec_type == CODEC_TYPE_VIDEO) {        /* XXX: need has_b_frame, but cannot get it if the codec is           not initialized */        if ((st->codec.codec_id == CODEC_ID_MPEG1VIDEO ||             st->codec.codec_id == CODEC_ID_MPEG2VIDEO ||             st->codec.codec_id == CODEC_ID_MPEG4 ||             st->codec.codec_id == CODEC_ID_H264) &&             pc && pc->pict_type != FF_B_TYPE)            presentation_delayed = 1;    }    /* interpolate PTS and DTS if they are not present */    if (presentation_delayed) {        /* DTS = decompression time stamp */        /* PTS = presentation time stamp */        if (pkt->dts == AV_NOPTS_VALUE) {            pkt->dts = st->cur_dts;        } else {            st->cur_dts = pkt->dts;        }        /* this is tricky: the dts must be incremented by the duration           of the frame we are displaying, i.e. the last I or P frame */        if (st->last_IP_duration == 0)            st->cur_dts += pkt->duration;        else            st->cur_dts += st->last_IP_duration;        st->last_IP_duration  = pkt->duration;        /* cannot compute PTS if not present (we can compute it only           by knowing the futur */    } else {        /* presentation is not delayed : PTS and DTS are the same */        if (pkt->pts == AV_NOPTS_VALUE) {            if (pkt->dts == AV_NOPTS_VALUE) {                pkt->pts = st->cur_dts;                pkt->dts = st->cur_dts;            }            else {                st->cur_dts = pkt->dts;                pkt->pts = pkt->dts;            }        } else {            st->cur_dts = pkt->pts;            pkt->dts = pkt->pts;        }        st->cur_dts += pkt->duration;    }        /* update flags */    if (pc) {        pkt->flags = 0;        /* key frame computation */        switch(st->codec.codec_type) {        case CODEC_TYPE_VIDEO:            if (pc->pict_type == FF_I_TYPE)                pkt->flags |= PKT_FLAG_KEY;            break;        case CODEC_TYPE_AUDIO:            pkt->flags |= PKT_FLAG_KEY;            break;        default:            break;        }    }}static void av_destruct_packet_nofree(AVPacket *pkt){    pkt->data = NULL; pkt->size = 0;}static int av_read_frame_internal(AVFormatContext *s, AVPacket *pkt){    AVStream *st;    int len, ret, i;    for(;;) {        /* select current input stream component */        st = s->cur_st;        if (st) {            if (!st->parser) {                /* no parsing needed: we just output the packet as is */                /* raw data support */                *pkt = s->cur_pkt;                compute_pkt_fields(s, st, NULL, pkt);                s->cur_st = NULL;                return 0;            } else if (s->cur_len > 0) {                len = av_parser_parse(st->parser, &st->codec, &pkt->data, &pkt->size,                                       s->cur_ptr, s->cur_len,                                      s->cur_pkt.pts, s->cur_pkt.dts);                s->cur_pkt.pts = AV_NOPTS_VALUE;                s->cur_pkt.dts = AV_NOPTS_VALUE;                /* increment read pointer */                s->cur_ptr += len;                s->cur_len -= len;                                /* return packet if any */                if (pkt->size) {                got_packet:                    pkt->duration = 0;                    pkt->stream_index = st->index;                    pkt->pts = st->parser->pts;                    pkt->dts = st->parser->dts;                    pkt->destruct = av_destruct_packet_nofree;                    compute_pkt_fields(s, st, st->parser, pkt);                    return 0;                }            } else {                /* free packet */                av_free_packet(&s->cur_pkt);                 s->cur_st = NULL;            }        } else {            /* read next packet */            ret = av_read_packet(s, &s->cur_pkt);            if (ret < 0) {                if (ret == -EAGAIN)                    return ret;                /* return the last frames, if any */                for(i = 0; i < s->nb_streams; i++) {                    st = s->streams[i];                    if (st->parser) {                        av_parser_parse(st->parser, &st->codec,                                         &pkt->data, &pkt->size,                                         NULL, 0,                                         AV_NOPTS_VALUE, AV_NOPTS_VALUE);                        if (pkt->size)                            goto got_packet;                    }                }                /* no more packets: really terminates parsing */                return ret;            }            /* convert the packet time stamp units and handle wrapping */            s->cur_pkt.pts = convert_timestamp_units(s,                                                &s->last_pkt_pts, &s->last_pkt_pts_frac,                                               &s->last_pkt_stream_pts,                                               s->cur_pkt.pts);            s->cur_pkt.dts = convert_timestamp_units(s,                                                &s->last_pkt_dts,  &s->last_pkt_dts_frac,                                               &s->last_pkt_stream_dts,                                               s->cur_pkt.dts);#if 0            if (s->cur_pkt.stream_index == 0) {                if (s->cur_pkt.pts != AV_NOPTS_VALUE)                     printf("PACKET pts=%0.3f\n",                            (double)s->cur_pkt.pts / AV_TIME_BASE);                if (s->cur_pkt.dts != AV_NOPTS_VALUE)                     printf("PACKET dts=%0.3f\n",                            (double)s->cur_pkt.dts / AV_TIME_BASE);            }#endif                        /* duration field */            if (s->cur_pkt.duration != 0) {                s->cur_pkt.duration = ((int64_t)s->cur_pkt.duration * AV_TIME_BASE * s->pts_num) /                     s->pts_den;            }            st = s->streams[s->cur_pkt.stream_index];            s->cur_st = st;            s->cur_ptr = s->cur_pkt.data;            s->cur_len = s->cur_pkt.size;            if (st->need_parsing && !st->parser) {                st->parser = av_parser_init(st->codec.codec_id);                if (!st->parser) {                    /* no parser available : just output the raw packets */                    st->need_parsing = 0;                }            }        }    }}/** * Return the next frame of a stream. The returned packet is valid * until the next av_read_frame() or until av_close_input_file() and * must be freed with av_free_packet. For video, the packet contains * exactly one frame. For audio, it contains an integer number of * frames if each frame has a known fixed size (e.g. PCM or ADPCM * data). If the audio frames have a variable size (e.g. MPEG audio), * then it contains one frame. *  * pkt->pts, pkt->dts and pkt->duration are always set to correct * values in AV_TIME_BASE unit (and guessed if the format cannot * provided them). pkt->pts can be AV_NOPTS_VALUE if the video format * has B frames, so it is better to rely on pkt->dts if you do not * decompress the payload. *  * Return 0 if OK, < 0 if error or end of file.   */int av_read_frame(AVFormatContext *s, AVPacket *pkt){    AVPacketList *pktl;    pktl = s->packet_buffer;    if (pktl) {        /* read packet from packet buffer, if there is data */        *pkt = pktl->pkt;        s->packet_buffer = pktl->next;        av_free(pktl);        return 0;    } else {        return av_read_frame_internal(s, pkt);    }}/* XXX: suppress the packet queue */static void flush_packet_queue(AVFormatContext *s){    AVPacketList *pktl;    for(;;) {        pktl = s->packet_buffer;        if (!pktl)             break;        s->packet_buffer = pktl->next;        av_free_packet(&pktl->pkt);        av_free(pktl);    }}/*******************************************************//* seek support */int av_find_default_stream_index(AVFormatContext *s){    int i;    AVStream *st;    if (s->nb_streams <= 0)        return -1;    for(i = 0; i < s->nb_streams; i++) {        st = s->streams[i];        if (st->codec.codec_type == CODEC_TYPE_VIDEO) {            return i;        }    }    return 0;}/* flush the frame reader */static void av_read_frame_flush(AVFormatContext *s){    AVStream *st;    int i;    flush_packet_queue(s);    /* free previous packet */    if (s->cur_st) {        if (s->cur_st->parser)            av_free_packet(&s->cur_pkt);        s->cur_st = NULL;    }    /* fail safe */    s->cur_ptr = NULL;    s->cur_len = 0;        /* for each stream, reset read state */    for(i = 0; i < s->nb_streams; i++) {        st = s->streams[i];                if (st->parser) {            av_parser_close(st->parser);            st->parser = NULL;        }        st->cur_dts = 0; /* we set the current DTS to an unspecified origin */    }}/* add a index entry into a sorted list updateing if it is already there */int av_add_index_entry(AVStream *st,                            int64_t pos, int64_t timestamp, int distance, int flags){    AVIndexEntry *entries, *ie;    int index;        entries = av_fast_realloc(st->index_entries,                              &st->index_entries_allocated_size,                              (st->nb_index_entries + 1) *                               sizeof(AVIndexEntry));    st->index_entries= entries;    if(st->nb_index_entries){        index= av_index_search_timestamp(st, timestamp);        ie= &entries[index];        if(ie->timestamp != timestamp){            if(ie->timestamp < timestamp){                index++; //index points to next instead of previous entry, maybe nonexistant                ie= &st->index_entries[index];            }else                assert(index==0);                

⌨️ 快捷键说明

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