ffmpeg av_read_frame源码分析

本文详细解析了FFmpeg中的av_read_frame函数的工作原理,包括如何从缓冲区或IO读取数据包、处理时间戳、解析数据包以及如何初始化和使用解析器等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传入参数:格式上下文,包指针
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
{
    const int genpts = s->flags & AVFMT_FLAG_GENPTS;
    int eof = 0;
    int ret;
    AVStream *st;

if (!genpts) {
   如果没有设定生成pts标志,从包缓冲区中或io读取包
        ret = s->internal->packet_buffer
              ? read_from_packet_buffer(&s->internal->packet_buffer,
                                        &s->internal->packet_buffer_end, pkt)
              : read_frame_internal(s, pkt);
        if (ret < 0)
            return ret;
        goto return_packet;
    }

    for (;;) {
        AVPacketList *pktl = s->internal->packet_buffer;

        if (pktl) {
            AVPacket *next_pkt = &pktl->pkt;

            if (next_pkt->dts != AV_NOPTS_VALUE) {
                int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
                // last dts seen for this stream. if any of packets following
                // current one had no dts, we will set this to AV_NOPTS_VALUE.
                int64_t last_dts = next_pkt->dts;
                while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
                    if (pktl->pkt.stream_index == next_pkt->stream_index &&
                        (av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2LL << (wrap_bits - 1)) < 0)) {
                        if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2LL << (wrap_bits - 1))) {
                            // not B-frame
                            next_pkt->pts = pktl->pkt.dts;
                        }
                        if (last_dts != AV_NOPTS_VALUE) {
                            // Once last dts was set to AV_NOPTS_VALUE, we don't change it.
                            last_dts = pktl->pkt.dts;
                        }
                    }
                    pktl = pktl->next;
                }
                if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) {
                    // Fixing the last reference frame had none pts issue (For MXF etc).
                    // We only do this when
                    // 1. eof.
                    // 2. we are not able to resolve a pts value for current packet.
                    // 3. the packets for this stream at the end of the files had valid dts.
                    next_pkt->pts = last_dts + next_pkt->duration;
                }
                pktl = s->internal->packet_buffer;
            }

            /* read packet from packet buffer, if there is data */
            st = s->streams[next_pkt->stream_index];
            if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL &&
                  next_pkt->dts != AV_NOPTS_VALUE && !eof)) {
                ret = read_from_packet_buffer(&s->internal->packet_buffer,
                                               &s->internal->packet_buffer_end, pkt);
                goto return_packet;
            }
        }

        ret = read_frame_internal(s, pkt);
        if (ret < 0) {
            if (pktl && ret != AVERROR(EAGAIN)) {
                eof = 1;
                continue;
            } else
                return ret;
        }

        ret = add_to_pktbuf(&s->internal->packet_buffer, pkt,
                            &s->internal->packet_buffer_end, 1);
        av_packet_unref(pkt);
        if (ret < 0)
            return ret;
    }

return_packet:

    st = s->streams[pkt->stream_index];
    if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
        ff_reduce_index(s, st->index);
        av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
    }

    if (is_relative(pkt->dts))
        pkt->dts -= RELATIVE_TS_BASE;
    if (is_relative(pkt->pts))
        pkt->pts -= RELATIVE_TS_BASE;

    return ret;
}

从缓冲区中读取包,缓冲去是一个链表,读取链表头部
static int read_from_packet_buffer(AVPacketList **pkt_buffer,
                                   AVPacketList **pkt_buffer_end,
                                   AVPacket      *pkt)
{
    AVPacketList *pktl;
    av_assert0(*pkt_buffer);
    pktl        = *pkt_buffer;
    *pkt        = pktl->pkt;
    *pkt_buffer = pktl->next;
    if (!pktl->next)
        *pkt_buffer_end = NULL;
    av_freep(&pktl);
    return 0;
}

从io中读取包,可以读取实时流,文件流。
static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
{
    int ret = 0, i, got_packet = 0;
    AVDictionary *metadata = NULL;
    //初始化packet
    av_init_packet(pkt);
   //如果还没读取包或解析队列为空
   //从io中读取数据,送入相应的解析器中进行解析,直到解析器认为得到一帧完整的视频流数据为止
    while (!got_packet && !s->internal->parse_queue) {
        AVStream *st;
        AVPacket cur_pkt;

        /* read next packet */
       //从io中读取包
        ret = ff_read_packet(s, &cur_pkt);
        if (ret < 0) {
            if (ret == AVERROR(EAGAIN))
                return ret;
            /* flush the parsers */
            //释放解析器中的数据
            for (i = 0; i < s->nb_streams; i++) {
                st = s->streams[i];
                if (st->parser && st->need_parsing)
                    parse_packet(s, NULL, st->index);
            }
            /* all remaining packets are now in parse_queue =>
             * really terminate parsing */
            break;
        }
        ret = 0;
        st  = s->streams[cur_pkt.stream_index];

        /* update context if required */
        if (st->internal->need_context_update) {
            if (avcodec_is_open(st->internal->avctx)) {
                av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n");
                avcodec_close(st->internal->avctx);
                st->info->found_decoder = 0;
            }

            /* close parser, because it depends on the codec */
            if (st->parser && st->internal->avctx->codec_id != st->codecpar->codec_id) {
                av_parser_close(st->parser);
                st->parser = NULL;
            }

            ret = avcodec_parameters_to_context(st->internal->avctx, st->codecpar);
            if (ret < 0)
                return ret;

#if FF_API_LAVF_AVCTX
FF_DISABLE_DEPRECATION_WARNINGS
            /* update deprecated public codec context */
            ret = avcodec_parameters_to_context(st->codec, st->codecpar);
            if (ret < 0)
                return ret;
FF_ENABLE_DEPRECATION_WARNINGS
#endif

            st->internal->need_context_update = 0;
        }

        if (cur_pkt.pts != AV_NOPTS_VALUE &&
            cur_pkt.dts != AV_NOPTS_VALUE &&
            cur_pkt.pts < cur_pkt.dts) {
            av_log(s, AV_LOG_WARNING,
                   "Invalid timestamps stream=%d, pts=%s, dts=%s, size=%d\n",
                   cur_pkt.stream_index,
                   av_ts2str(cur_pkt.pts),
                   av_ts2str(cur_pkt.dts),
                   cur_pkt.size);
        }
        if (s->debug & FF_FDEBUG_TS)
            av_log(s, AV_LOG_DEBUG,
                   "ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%"PRId64", flags=%d\n",
                   cur_pkt.stream_index,
                   av_ts2str(cur_pkt.pts),
                   av_ts2str(cur_pkt.dts),
                   cur_pkt.size, cur_pkt.duration, cur_pkt.flags);
//如果流需要解析,并且解析器没有初始化,并且格式上下文标志没有设置为AVFMT_FLAG_NOPARSE,则初始化解析器。
        if (st->need_parsing && !st->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
            st->parser = av_parser_init(st->codecpar->codec_id);
            if (!st->parser) {
                av_log(s, AV_LOG_VERBOSE, "parser not found for codec "
                       "%s, packets or times may be invalid.\n",
                       avcodec_get_name(st->codecpar->codec_id));
                /* no parser available: just output the raw packets */
                st->need_parsing = AVSTREAM_PARSE_NONE;
            } else if (st->need_parsing == AVSTREAM_PARSE_HEADERS)
                st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
            else if (st->need_parsing == AVSTREAM_PARSE_FULL_ONCE)
                st->parser->flags |= PARSER_FLAG_ONCE;
            else if (st->need_parsing == AVSTREAM_PARSE_FULL_RAW)
                st->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
        }
       //如果stream不需要解析或者解析器为空
        if (!st->need_parsing || !st->parser) {
            /* no parsing needed: we just output the packet as is */
            *pkt = cur_pkt;
            compute_pkt_fields(s, st, NULL, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
            if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
                (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
                ff_reduce_index(s, st->index);
                av_add_index_entry(st, pkt->pos, pkt->dts,
                                   0, 0, AVINDEX_KEYFRAME);
            }
            got_packet = 1;
        } else if (st->discard < AVDISCARD_ALL) {
//解析包,这里会调用相应的视频或音频解析器            
if ((ret = parse_packet(s, &cur_pkt, cur_pkt.stream_index)) < 0)
                return ret;
//如果是音频,得到采样率、比特率、通道数量、通道layout
            st->codecpar->sample_rate = st->internal->avctx->sample_rate;
            st->codecpar->bit_rate = st->internal->avctx->bit_rate;
            st->codecpar->channels = st->internal->avctx->channels;
            st->codecpar->channel_layout = st->internal->avctx->channel_layout;
            st->codecpar->codec_id = st->internal->avctx->codec_id;
        } else {
            /* free packet */
//释放packet
            av_packet_unref(&cur_pkt);
        }
        if (pkt->flags & AV_PKT_FLAG_KEY)
//如果packet为关键packet
            st->skip_to_keyframe = 0;
        if (st->skip_to_keyframe) {
            av_packet_unref(&cur_pkt);
            if (got_packet) {
                *pkt = cur_pkt;
            }
            got_packet = 0;
        }
    }
 //如果没有得到完整的packet,并且解析队列不为空,从packet队列读取packet
    if (!got_packet && s->internal->parse_queue)
        ret = read_from_packet_buffer(&s->internal->parse_queue, &s->internal->parse_queue_end, pkt);
   //读取成功,计算时间戳等参数
    if (ret >= 0) {
        AVStream *st = s->streams[pkt->stream_index];
        int discard_padding = 0;
        if (st->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) {
            int64_t pts = pkt->pts - (is_relative(pkt->pts) ? RELATIVE_TS_BASE : 0);
            int64_t sample = ts_to_samples(st, pts);
            int duration = ts_to_samples(st, pkt->duration);
            int64_t end_sample = sample + duration;
            if (duration > 0 && end_sample >= st->first_discard_sample &&
                sample < st->last_discard_sample)
                discard_padding = FFMIN(end_sample - st->first_discard_sample, duration);
        }
        if (st->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE))
            st->skip_samples = st->start_skip_samples;
        if (st->skip_samples || discard_padding) {
            uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
            if (p) {
                AV_WL32(p, st->skip_samples);
                AV_WL32(p + 4, discard_padding);
                av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %d / discard %d\n", st->skip_samples, discard_padding);
            }
            st->skip_samples = 0;
        }

        if (st->inject_global_side_data) {
            for (i = 0; i < st->nb_side_data; i++) {
                AVPacketSideData *src_sd = &st->side_data[i];
                uint8_t *dst_data;

                if (av_packet_get_side_data(pkt, src_sd->type, NULL))
                    continue;

                dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size);
                if (!dst_data) {
                    av_log(s, AV_LOG_WARNING, "Could not inject global side data\n");
                    continue;
                }

                memcpy(dst_data, src_sd->data, src_sd->size);
            }
            st->inject_global_side_data = 0;
        }

#if FF_API_LAVF_MERGE_SD
FF_DISABLE_DEPRECATION_WARNINGS
        if (!(s->flags & AVFMT_FLAG_KEEP_SIDE_DATA))
            av_packet_merge_side_data(pkt);
FF_ENABLE_DEPRECATION_WARNINGS
#endif
    }

    av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
    if (metadata) {
        s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
        av_dict_copy(&s->metadata, metadata, 0);
        av_dict_free(&metadata);
        av_opt_set_dict_val(s, "metadata", NULL, AV_OPT_SEARCH_CHILDREN);
    }

#if FF_API_LAVF_AVCTX
    update_stream_avctx(s);
#endif

    if (s->debug & FF_FDEBUG_TS)
        av_log(s, AV_LOG_DEBUG,
               "read_frame_internal stream=%d, pts=%s, dts=%s, "
               "size=%d, duration=%"PRId64", flags=%d\n",
               pkt->stream_index,
               av_ts2str(pkt->pts),
               av_ts2str(pkt->dts),
               pkt->size, pkt->duration, pkt->flags);

    return ret;
}
<think>我们讨论的是FFmpeg中`av_read_frame`函数是否可能返回`AVERROR(EAGAIN)`错误。 根据FFmpeg的文档和源码分析,`av_read_frame`函数用于从输入媒体文件中读取下一个数据包(packet)。该函数的返回值可能有以下几种情况: - 成功读取到一个packet,返回0。 - 到达文件末尾,返回`AVERROR_EOF`。 - 其他错误,返回负的错误码。 在FFmpeg的官方文档中,并没有明确说明`av_read_frame`会返回`AVERROR(EAGAIN)`。但是,我们需要考虑实际使用场景和底层实现。 引用[2]中提到,`av_read_frame`内部会调用`read_frame_internal`,而该函数最终会调用对应格式的`read_packet`函数。不同的解复用器(demuxer)可能有不同的行为。 在FFmpeg的源码中,我们可以查看`libavformat/utils.c`中的`av_read_frame`函数实现。在正常情况下,如果数据还没有准备好(例如在非阻塞模式下),有些解复用器可能会返回`AVERROR(EAGAIN)`。但是,默认情况下,FFmpeg的解复用操作是阻塞的,即它会等待直到有数据可读。 然而,如果我们将输入上下文(`AVFormatContext`)设置为非阻塞模式(通过设置`flags`为`AVFMT_FLAG_NONBLOCK`),那么当没有数据可读时,`av_read_frame`可能会返回`AVERROR(EAGAIN)`。 设置非阻塞模式的示例: ```c AVFormatContext *fmt_ctx = ...; fmt_ctx->flags |= AVFMT_FLAG_NONBLOCK; ``` 因此,结论是: - 在默认的阻塞模式下,`av_read_frame`不会返回`AVERROR(EAGAIN)`,它会一直阻塞直到有数据可读或遇到错误/文件尾。 - 在非阻塞模式下,如果当前没有数据包可读,则`av_read_frame`会返回`AVERROR(EAGAIN)`,表示需要再次尝试读取。 所以,回答用户的问题:**是的,`av_read_frame`可能返回`AVERROR(EAGAIN)`,但仅当在非阻塞模式下使用FFmpeg的解复用时。** 如果用户没有显式设置非阻塞模式,那么通常不会遇到这个错误。如果用户遇到了这个错误,那么需要检查是否设置了非阻塞模式,并在出现该错误时进行重试(使用轮询或等待)。 处理非阻塞模式下`av_read_frame`返回`AVERROR(EAGAIN)`的示例代码: ```c AVPacket pkt; av_init_packet(&pkt); int ret; while ((ret = av_read_frame(fmt_ctx, &pkt)) == AVERROR(EAGAIN)) { // 等待一段时间再重试,或者使用select/poll等机制等待文件描述符可读 usleep(10000); // 等待10毫秒 } if (ret < 0 && ret != AVERROR_EOF) { // 处理其他错误 } else if (ret == 0) { // 成功读取到packet // ... 处理pkt av_packet_unref(&pkt); } ``` 注意:非阻塞模式通常用于需要同时处理多个输入流或需要与事件循环集成的场景(如GUI应用、网络流媒体服务器等)。 另外,引用[3]中提到关于`av_read_frame`的超时设置,实际上在阻塞模式下,我们可以通过设置`AVFormatContext`的`interrupt_callback`来设置超时回调,从而中断阻塞的读取操作。但这与返回`EAGAIN`不同。 总结: - 阻塞模式(默认):不会返回`EAGAIN`,要么等到数据,要么返回错误或EOF。 - 非阻塞模式:可能返回`EAGAIN`,表示暂时没有数据,需要重试。 因此,用户在使用`av_read_frame`时,如果遇到`AVERROR(EAGAIN)`,应该检查是否启用了非阻塞模式,并相应地处理。 §§相关问题§§ 1. 如何设置FFmpeg的解复用为非阻塞模式? 2. 在非阻塞模式下,除了循环等待,还有哪些方法处理`AVERROR(EAGAIN)`? 3. 如何为`av_read_frame`设置超时中断? 4. FFmpeg中非阻塞模式对网络流媒体的影响?</think>### FFmpeg `av_read_frame` 与 `AVERROR(EAGAIN)` 错误分析 #### 1. **`av_read_frame` 是否可能返回 `AVERROR(EAGAIN)`?** **是**,但**仅特定场景下**会发生: - **默认阻塞模式**:不会返回 `EAGAIN`,会阻塞直到数据就绪或出错 - **非阻塞模式**:需显式启用(通过 `AVFMT_FLAG_NONBLOCK` 标志),此时可能返回 `AVERROR(EAGAIN)` 表示数据未就绪[^3] #### 2. 触发 `EAGAIN` 的场景 ```c // 设置非阻塞模式示例 AVFormatContext *fmt_ctx = ...; fmt_ctx->flags |= AVFMT_FLAG_NONBLOCK; // 关键标志 ``` - **网络流媒体**:网络波动导致数据未及时到达 - **实时采集设备**:摄像头/麦克风数据未就绪 - **自定义 I/O 上下文**:用户实现 `URLProtocol` 时返回 `EAGAIN` - **多路复用输入**:同时处理多个流时资源竞争 #### 3. 错误处理方案 ```c AVPacket pkt; av_init_packet(&pkt); int ret = av_read_frame(fmt_ctx, &pkt); if (ret == AVERROR(EAGAIN)) { // 非阻塞模式下的重试机制 usleep(10 * 1000); // 等待10ms continue; // 重新尝试读取 } else if (ret < 0) { // 其他错误处理 char err_buf[256]; av_strerror(ret, err_buf, sizeof(err_buf)); fprintf(stderr, "Read error: %s\n", err_buf); break; } ``` #### 4. 与解码错误 `EAGAIN` 的区别 | 场景 | `av_read_frame` 的 `EAGAIN` | `avcodec_receive_frame` 的 `EAGAIN` | |---------------------|-----------------------------------|--------------------------------------| | **触发位置** | 解复用层(Demuxer) | 解码层(Decoder) | | **根本原因** | 输入数据未就绪 | 解码器内部缓存不足 | | **解决方案** | 等待数据或重试 | 发送更多 packet 到解码器 | | **相关引用** | [^2][^3] | [^1] | #### 5. 最佳实践建议 1. **实时流媒体处理** ```c // 设置超时回调 AVFormatContext *fmt_ctx; fmt_ctx->interrupt_callback.callback = interrupt_cb; // 自定义超时检测函数 ``` 2. **精确控制读取** 结合 `avio_read` 和自定义 I/O 缓冲区实现精细控制[^2] 3. **格式解析器集成** 如 H.264 解析器(`ff_h264_parser`)可能影响数据就绪判断[^4]: ```c // H.264解析器结构 AVCodecParser ff_h264_parser = { .parser_parse = h264_parse, // 解析函数 .split = h264_split // 数据分割 }; ``` > **关键结论**:在标准阻塞模式下无需处理 `EAGAIN`,但非阻塞模式或实时流场景需主动处理该错误,通过重试机制保证数据连续性[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值