ffplay源码分析-注重流程

113 阅读7分钟

ffplay源码分析

-------------------------------------------------------------------解码部分

4.4版本的源码简要分析,不会深究细节,仅以流程和原理为主,做为学习和记录之用,希望能在音视频领域有所成就。ffplay在windows平台,使用sdl显示渲染, 其余解码同步等流程基本一致,ffplay.c总计3000多行,不算太多,以本人有限的C语言功力,尝试解读一把。

入口函数 Main 的重要函数

int main(int argc, char **argv)
{
//懒加载主要是加载sdl库
init_dynload();

//设置相关日志级别
av_log_set_flags(AV_LOG_SKIP_REPEATED);
parse_loglevel(argc, argv, options);

//初始化相关参数?注册信号量,解析命令参数
init_opts();
signal(SIGINT , sigterm_handler); /* Interrupt (ANSI).    */
signal(SIGTERM, sigterm_handler); /* Termination (ANSI).  */
parse_options(NULL, argc, argv, options, opt_input_file);
/**
创建命令SDL render 事件监听等等就不说明了
...
*/

//打开文件开始解码,此处为重点
VideoState *is;
is = stream_open(input_filename, file_iformat);    

//监听事件,播放命令等
event_loop(is);
}

1、开始解码函数 stream_open(input_filename, file_iformat);

//此函数返回VideoState结构体指针,包含了当前音视频输入的一系列内存数据、状态,和几乎所有与解码播放相关的队列和控制信息等数据和结构体
static VideoState *stream_open(const char *filename,
                               const AVInputFormat *iformat)
{
//malloc VideoState ... 初始化等
VideoState *is;
is = av_mallocz(sizeof(VideoState));
//...
    
/* start video display 有几个重要函数   frame_queue_init  packet_queue_init*/

    
    if (frame_queue_init(&is->pictq, &is->videoq, VIDEO_PICTURE_QUEUE_SIZE, 1) < 0)
        goto fail;
    if (frame_queue_init(&is->subpq, &is->subtitleq, SUBPICTURE_QUEUE_SIZE, 0) < 0)
        goto fail;
    if (frame_queue_init(&is->sampq, &is->audioq, SAMPLE_QUEUE_SIZE, 1) < 0)
        goto fail;

    if (packet_queue_init(&is->videoq) < 0 ||
        packet_queue_init(&is->audioq) < 0 ||
        packet_queue_init(&is->subtitleq) < 0)
        goto fail;

//...初始化时钟,音量等方法

//启动读取线程,执行读取方法
is->read_tid = SDL_CreateThread(read_thread, "read_thread", is);
}


1.1 初始化Frame Queue,frame_queue_init

//frame_queue_init 是对framequeue进行初始化,遍历frame初始化,frame就是包装了一下avframe,
//把avframe的数据取出来了,而FrameQueue是一个ringbuffer,保存的就是 frame的队列
/**
typedef struct  FrameQueue  {
    Frame  queue[ FRAME_QUEUE_SIZE  ];
    int  rindex; //表示循环队列的结尾处
    int  windex; //表示循环队列的开始处
    int  size;
    int  max_size;
    int  keep_last;
    int  rindex_shown;//一开始为0,之后一直为1
    SDL_mutex  *mutex;
    SDL_cond  *cond;
    PacketQueue  *pktq;
}  FrameQueue ;

typedef struct Frame {
    AVFrame *frame;
    AVSubtitle sub;
    int serial;
    double pts;           /* presentation timestamp for the frame */
    double duration;      /* estimated duration of the frame */
    int64_t pos;          /* byte position of the frame in the input file */
    int width;
    int height;
    int format;
    AVRational sar;
    int uploaded;
    int flip_v;
} Frame;
*/
    
static int frame_queue_init(FrameQueue *f, PacketQueue *pktq, int max_size, int keep_last)
{
    int i;
    memset(f, 0, sizeof(FrameQueue));
    if (!(f->mutex = SDL_CreateMutex())) { //创建锁
        av_log(NULL, AV_LOG_FATAL, "SDL_CreateMutex(): %s\n", SDL_GetError());
        return AVERROR(ENOMEM);
    }
    if (!(f->cond = SDL_CreateCond())) { //创建条件变量
        av_log(NULL, AV_LOG_FATAL, "SDL_CreateCond(): %s\n", SDL_GetError());
        return AVERROR(ENOMEM);
    }
    f->pktq = pktq;
    f->max_size = FFMIN(max_size, FRAME_QUEUE_SIZE);
    f->keep_last = !!keep_last;
    for (i = 0; i < f->max_size; i++)
        if (!(f->queue[i].frame = av_frame_alloc()))
            return AVERROR(ENOMEM);
    return 0;
}

1.2 初始化Packet Queue,packet_queue_init

//4.4多了一个pkt_list的初始化  av_fifo_alloc,是通过新的方法 libavutil/fifo.c 创建了一个环形缓冲区
static int packet_queue_init(PacketQueue *q)
{
    memset(q, 0, sizeof(PacketQueue));
    q->pkt_list = av_fifo_alloc(sizeof(MyAVPacketList));
    if (!q->pkt_list)
        return AVERROR(ENOMEM);
    q->mutex = SDL_CreateMutex();
    if (!q->mutex) {
        av_log(NULL, AV_LOG_FATAL, "SDL_CreateMutex(): %s\n", SDL_GetError());
        return AVERROR(ENOMEM);
    }
    q->cond = SDL_CreateCond();
    if (!q->cond) {
        av_log(NULL, AV_LOG_FATAL, "SDL_CreateCond(): %s\n", SDL_GetError());
        return AVERROR(ENOMEM);
    }
    q->abort_request = 1;
    return 0;
}

1.3 开始执行解码线程中的方法read_thread

方法非常长,简言之,初始化ffmpeg相关的解封装,解复用环境︮ / rom the disk or the network */ static int read_thread(void *arg) {

AVPacket *pkt = NULL;
pkt = av_packet_alloc();

//avformat初始化 ic = avformat_alloc_context();

ic->interrupt_callback.callback = decode_interrupt_cb;
ic->interrupt_callback.opaque = is;
if (!av_dict_get(format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
    av_dict_set(&format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
    scan_all_pmts_set = 1;
}

//打开文件 err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);

if (scan_all_pmts_set)
    av_dict_set(&format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);

is->ic = ic;

av_format_inject_global_side_data(ic);

if (find_stream_info) {
    AVDictionary **opts = setup_find_stream_info_opts(ic, codec_opts);
    int orig_nb_streams = ic->nb_streams;

    err = avformat_find_stream_info(ic, opts);

    for (i = 0; i < orig_nb_streams; i++)
        av_dict_free(&opts[i]);
    av_freep(&opts);

}

if (ic->pb)
    ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use avio_feof() to test for the end

if (seek_by_bytes < 0)
    seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT) && strcmp("ogg", ic->iformat->name);

is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;

if (!window_title && (t = av_dict_get(ic->metadata, "title", NULL, 0)))
    window_title = av_asprintf("%s - %s", t->value, input_filename);

/* if seeking requested, we execute it */
if (start_time != AV_NOPTS_VALUE) {
    int64_t timestamp;

    timestamp = start_time;
    /* add the stream start time */
    if (ic->start_time != AV_NOPTS_VALUE)
        timestamp += ic->start_time;
    ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);

}

is->realtime = is_realtime(ic);


if (!video_disable)
    st_index[AVMEDIA_TYPE_VIDEO] =
        av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
                            st_index[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
if (!audio_disable)
    st_index[AVMEDIA_TYPE_AUDIO] =
        av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
                            st_index[AVMEDIA_TYPE_AUDIO],
                            st_index[AVMEDIA_TYPE_VIDEO],
                            NULL, 0);
if (!video_disable && !subtitle_disable)
    st_index[AVMEDIA_TYPE_SUBTITLE] =
        av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
                            st_index[AVMEDIA_TYPE_SUBTITLE],
                            (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
                             st_index[AVMEDIA_TYPE_AUDIO] :
                             st_index[AVMEDIA_TYPE_VIDEO]),
                            NULL, 0);

is->show_mode = show_mode;
if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
    AVStream *st = ic->streams[st_index[AVMEDIA_TYPE_VIDEO]];
    AVCodecParameters *codecpar = st->codecpar;
    AVRational sar = av_guess_sample_aspect_ratio(ic, st, NULL);
    if (codecpar->width)
        set_default_window_size(codecpar->width, codecpar->height, sar);
}

/*下面这三个stream_component_open函数分别打开音频流,视频流,字幕流,并在其中开启了解码线程, 留待后面再说 */ if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) { stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]); }

ret = -1;
if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
    ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
}
if (is->show_mode == SHOW_MODE_NONE)
    is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;

if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
    stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
}



if (infinite_buffer < 0 && is->realtime)
    infinite_buffer = 1;
    //阻塞等操作命令    
for (;;) {
    if (is->abort_request)
        break;
    if (is->paused != is->last_paused) {
        is->last_paused = is->paused;
        if (is->paused)
            is->read_pause_return = av_read_pause(ic);
        else
            av_read_play(ic);
    }
    #if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
    if (is->paused &&
            (!strcmp(ic->iformat->name, "rtsp") ||
             (ic->pb && !strncmp(input_filename, "mmsh:", 5)))) {
        /* wait 10 ms to avoid trying to get another packet */
        /* XXX: horrible */
        SDL_Delay(10);
        continue;
    }
    #endif
    if (is->seek_req) {
        int64_t seek_target = is->seek_pos;
        int64_t seek_min    = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
        int64_t seek_max    = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
        // FIXME the +-2 is due to rounding being not done in the correct direction in generation
        //      of the seek_pos/seek_rel variables

        
        //seek操作发生时,清理PacketQueue          
        ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
        if (ret < 0) {
            av_log(NULL, AV_LOG_ERROR,
                   "%s: error while seeking\n", is->ic->url);
        } else {
            if (is->audio_stream >= 0)
                packet_queue_flush(&is->audioq);
            if (is->subtitle_stream >= 0)
                packet_queue_flush(&is->subtitleq);
            if (is->video_stream >= 0)
                packet_queue_flush(&is->videoq);
            if (is->seek_flags & AVSEEK_FLAG_BYTE) {
               set_clock(&is->extclk, NAN, 0);
            } else {
               set_clock(&is->extclk, seek_target / (double)AV_TIME_BASE, 0);
            }
        }
        is->seek_req = 0;
        is->queue_attachments_req = 1;
        is->eof = 0;
        if (is->paused)
            step_to_next_frame(is);
    }
    if (is->queue_attachments_req) {
        if (is->video_st && is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC) {
            if ((ret = av_packet_ref(pkt, &is->video_st->attached_pic)) < 0)
                goto fail;
            packet_queue_put(&is->videoq, pkt);
            packet_queue_put_nullpacket(&is->videoq, pkt, is->video_stream);
        }
        is->queue_attachments_req = 0;
    }

    /* if the queue are full, no need to read more */
    if (infinite_buffer<1 &&
          (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
        || (stream_has_enough_packets(is->audio_st, is->audio_stream, &is->audioq) &&
            stream_has_enough_packets(is->video_st, is->video_stream, &is->videoq) &&
            stream_has_enough_packets(is->subtitle_st, is->subtitle_stream, &is->subtitleq)))) {
        /* wait 10 ms */
        SDL_LockMutex(wait_mutex);
        SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
        SDL_UnlockMutex(wait_mutex);
        continue;
    }
    if (!is->paused &&
        (!is->audio_st || (is->auddec.finished == is->audioq.serial && frame_queue_nb_remaining(&is->sampq) == 0)) &&
        (!is->video_st || (is->viddec.finished == is->videoq.serial && frame_queue_nb_remaining(&is->pictq) == 0))) {
        if (loop != 1 && (!loop || --loop)) {
            stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
        } else if (autoexit) {
            ret = AVERROR_EOF;
            goto fail;
        }
    }
    ret = av_read_frame(ic, pkt);
    if (ret < 0) {
        if ((ret == AVERROR_EOF || avio_feof(ic->pb)) && !is->eof) {
            if (is->video_stream >= 0)
                packet_queue_put_nullpacket(&is->videoq, pkt, is->video_stream);
            if (is->audio_stream >= 0)
                packet_queue_put_nullpacket(&is->audioq, pkt, is->audio_stream);
            if (is->subtitle_stream >= 0)
                packet_queue_put_nullpacket(&is->subtitleq, pkt, is->subtitle_stream);
            is->eof = 1;
        }
        if (ic->pb && ic->pb->error) {
            if (autoexit)
                goto fail;
            else
                break;
        }
        SDL_LockMutex(wait_mutex);
        SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
        SDL_UnlockMutex(wait_mutex);
        continue;
    } else {
        is->eof = 0;
    }
    /* check if packet is in play range specified by user, then queue, otherwise discard */
    stream_start_time = ic->streams[pkt->stream_index]->start_time;
    pkt_ts = pkt->pts == AV_NOPTS_VALUE ? pkt->dts : pkt->pts;
    pkt_in_play_range = duration == AV_NOPTS_VALUE ||
            (pkt_ts - (stream_start_time != AV_NOPTS_VALUE ? stream_start_time : 0)) *
            av_q2d(ic->streams[pkt->stream_index]->time_base) -
            (double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
            <= ((double)duration / 1000000);
    if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
        packet_queue_put(&is->audioq, pkt);
    } else if (pkt->stream_index == is->video_stream && pkt_in_play_range
               && !(is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
        packet_queue_put(&is->videoq, pkt);
    } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
        packet_queue_put(&is->subtitleq, pkt);
    } else {
        av_packet_unref(pkt);
    }
}

ret = 0;
fail:
if (ic && !is->ic)
    avformat_close_input(&ic);

av_packet_free(&pkt);
if (ret != 0) {
    SDL_Event event;

    event.type = FF_QUIT_EVENT;
    event.user.data1 = is;
    SDL_PushEvent(&event);
}
SDL_DestroyMutex(wait_mutex);
return 0;

}


### 2  启动对应流的解码线程stream_component_open

```C
/* open a given stream. Return 0 if OK */
static int stream_component_open(VideoState *is, int stream_index)
{
    AVFormatContext *ic = is->ic;
    AVCodecContext *avctx;
    const AVCodec *codec;
    const char *forced_codec_name = NULL;
    AVDictionary *opts = NULL;
    AVDictionaryEntry *t = NULL;
    int sample_rate, nb_channels;
    int64_t channel_layout;
    int ret = 0;
    int stream_lowres = lowres;

    if (stream_index < 0 || stream_index >= ic->nb_streams)
        return -1;

//初始化avcodec   
    avctx = avcodec_alloc_context3(NULL);
    if (!avctx)
        return AVERROR(ENOMEM);
    
    ret = avcodec_parameters_to_context(avctx, ic->streams[stream_index]->codecpar);
    if (ret < 0)
        goto fail;
    avctx->pkt_timebase = ic->streams[stream_index]->time_base;

//拿到对应的avcodec    
    codec = avcodec_find_decoder(avctx->codec_id);

switch(avctx->codec_type){
        case AVMEDIA_TYPE_AUDIO   : is->last_audio_stream    = stream_index; forced_codec_name =    audio_codec_name; break;
        case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; forced_codec_name = subtitle_codec_name; break;
        case AVMEDIA_TYPE_VIDEO   : is->last_video_stream    = stream_index; forced_codec_name =    video_codec_name; break;
    }
    if (forced_codec_name)
        codec = avcodec_find_decoder_by_name(forced_codec_name);
    if (!codec) {
        if (forced_codec_name) av_log(NULL, AV_LOG_WARNING,
                                      "No codec could be found with name '%s'\n", forced_codec_name);
        else                   av_log(NULL, AV_LOG_WARNING,
                                      "No decoder could be found for codec %s\n", avcodec_get_name(avctx->codec_id));
        ret = AVERROR(EINVAL);
        goto fail;
    }
    
    avctx->codec_id = codec->id;
    if (stream_lowres > codec->max_lowres) {
        av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
                codec->max_lowres);
        stream_lowres = codec->max_lowres;
    }
    avctx->lowres = stream_lowres;
    
    if (fast)
        avctx->flags2 |= AV_CODEC_FLAG2_FAST;
    
    opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
    if (!av_dict_get(opts, "threads", NULL, 0))
        av_dict_set(&opts, "threads", "auto", 0);
    if (stream_lowres)
        av_dict_set_int(&opts, "lowres", stream_lowres, 0);
    if ((ret = avcodec_open2(avctx, codec, &opts)) < 0) {
        goto fail;
    }
    if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
        av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
        ret =  AVERROR_OPTION_NOT_FOUND;
        goto fail;
    }
    
    is->eof = 0;
    ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
    switch (avctx->codec_type) {
    case AVMEDIA_TYPE_AUDIO:

#if CONFIG_AVFILTER
        {
            AVFilterContext *sink;

            is->audio_filter_src.freq           = avctx->sample_rate;
            is->audio_filter_src.channels       = avctx->channels;
            is->audio_filter_src.channel_layout = get_valid_channel_layout(avctx->channel_layout, avctx->channels);
            is->audio_filter_src.fmt            = avctx->sample_fmt;
            if ((ret = configure_audio_filters(is, afilters, 0)) < 0)
                goto fail;
            sink = is->out_audio_filter;
            sample_rate    = av_buffersink_get_sample_rate(sink);
            nb_channels    = av_buffersink_get_channels(sink);
            channel_layout = av_buffersink_get_channel_layout(sink);
        }

#else
        sample_rate    = avctx->sample_rate;
        nb_channels    = avctx->channels;
        channel_layout = avctx->channel_layout;
#endif

        /* prepare audio output */
        if ((ret = audio_open(is, channel_layout, nb_channels, sample_rate, &is->audio_tgt)) < 0)
            goto fail;
        is->audio_hw_buf_size = ret;
        is->audio_src = is->audio_tgt;
        is->audio_buf_size  = 0;
        is->audio_buf_index = 0;
    
        /* init averaging filter */
        is->audio_diff_avg_coef  = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
        is->audio_diff_avg_count = 0;
        /* since we do not have a precise anough audio FIFO fullness,
           we correct audio sync only if larger than this threshold */
        is->audio_diff_threshold = (double)(is->audio_hw_buf_size) / is->audio_tgt.bytes_per_sec;
    
        is->audio_stream = stream_index;
        is->audio_st = ic->streams[stream_index];
    
        if ((ret = decoder_init(&is->auddec, avctx, &is->audioq, is->continue_read_thread)) < 0)
            goto fail;
        if ((is->ic->iformat->flags & (AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK)) && !is->ic->iformat->read_seek) {
            is->auddec.start_pts = is->audio_st->start_time;
            is->auddec.start_pts_tb = is->audio_st->time_base;
        }
        if ((ret = decoder_start(&is->auddec, audio_thread, "audio_decoder", is)) < 0)
            goto out;
        SDL_PauseAudioDevice(audio_dev, 0);
        break;
    case AVMEDIA_TYPE_VIDEO:
        is->video_stream = stream_index;
        is->video_st = ic->streams[stream_index];
    
        if ((ret = decoder_init(&is->viddec, avctx, &is->videoq, is->continue_read_thread)) < 0)
            goto fail;
        if ((ret = decoder_start(&is->viddec, video_thread, "video_decoder", is)) < 0)
            goto out;
        is->queue_attachments_req = 1;
        break;
    case AVMEDIA_TYPE_SUBTITLE:
        is->subtitle_stream = stream_index;
        is->subtitle_st = ic->streams[stream_index];
    
        if ((ret = decoder_init(&is->subdec, avctx, &is->subtitleq, is->continue_read_thread)) < 0)
            goto fail;
        if ((ret = decoder_start(&is->subdec, subtitle_thread, "subtitle_decoder", is)) < 0)
            goto out;
        break;
    default:
        break;
    }
    goto out;

fail:
    avcodec_free_context(&avctx);
out:
    av_dict_free(&opts);

    return ret;

}

启动线程解码,执行解码方法video_thread和audio_thread

static int decoder_start(Decoder *d, int (*fn)(void *), const char *thread_name, void* arg)
{
    packet_queue_start(d->queue);
    d->decoder_tid = SDL_CreateThread(fn, thread_name, arg);
    if (!d->decoder_tid) {
        av_log(NULL, AV_LOG_ERROR, "SDL_CreateThread(): %s\n", SDL_GetError());
        return AVERROR(ENOMEM);
    }
    return 0;
}

内部解码音频线程和视频线程都调用对应的Decoder,decoder_decode_frame解码对应的frame

static int decoder_decode_frame(Decoder *d, AVFrame *frame, AVSubtitle *sub) {
    int ret = AVERROR(EAGAIN);
    

    for (;;) {
        AVPacket pkt;
        
        /*
         如果Decoder所解码队列的serail 和 Decoder即将要解码的serial不相等,则表示有seek操作,要放弃seek前的pkt
         第一次调用时两值不相等
         */
        if (d->queue->serial == d->pkt_serial) {
            do {
                if (d->queue->abort_request)
                    return -1;
                
                switch (d->avctx->codec_type) {
                    case AVMEDIA_TYPE_VIDEO:
                        ret = avcodec_receive_frame(d->avctx, frame);
                        if (ret >= 0) {
                            if (decoder_reorder_pts == -1) {
                                frame->pts = frame->best_effort_timestamp;
                            } else if (!decoder_reorder_pts) {
                                frame->pts = frame->pkt_dts;
                            }
                        }
                        break;
                    case AVMEDIA_TYPE_AUDIO:
                        ret = avcodec_receive_frame(d->avctx, frame);
                        if (ret >= 0) {
                            AVRational tb = (AVRational){1, frame->sample_rate};
                            if (frame->pts != AV_NOPTS_VALUE)
                                frame->pts = av_rescale_q(frame->pts, d->avctx->pkt_timebase, tb);
                            else if (d->next_pts != AV_NOPTS_VALUE)
                                frame->pts = av_rescale_q(d->next_pts, d->next_pts_tb, tb);
                            if (frame->pts != AV_NOPTS_VALUE) {
                                d->next_pts = frame->pts + frame->nb_samples;
                                d->next_pts_tb = tb;
                            }
                        }
                        break;
                }
                if (ret == AVERROR_EOF) {
                    //所有的解码结束后,Decoder的finished等于Decoder的pkt_serial
                    d->finished = d->pkt_serial;
                    avcodec_flush_buffers(d->avctx);
                    return 0;
                }
                if (ret >= 0)
                    return 1;
            } while (ret != AVERROR(EAGAIN));
        }
        
        //循环找到serial为当前packet队列的serial的pkt,也就是放弃seek前的pkt
        do {
            if (d->queue->nb_packets == 0)
                SDL_CondSignal(d->empty_queue_cond);
            if (d->packet_pending) {
                av_packet_move_ref(&pkt, &d->pkt);
                d->packet_pending = 0;
            } else {
                //packet_queue_get目标是获得pkt,如果队列为空,则等待,否则获取到pkt,删除头节点
                if (packet_queue_get(d->queue, &pkt, 1, &d->pkt_serial) < 0)
                    return -1;
            }
        } while (d->queue->serial != d->pkt_serial);
        
        //表示有seek操作,清除解码器内部状态
        if (pkt.data == flush_pkt.data) {
            avcodec_flush_buffers(d->avctx);
            d->finished = 0;
            d->next_pts = d->start_pts;
            d->next_pts_tb = d->start_pts_tb;
        } else {
            if (d->avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
                int got_frame = 0;
                ret = avcodec_decode_subtitle2(d->avctx, sub, &got_frame, &pkt);
                if (ret < 0) {
                    ret = AVERROR(EAGAIN);
                } else {
                    if (got_frame && !pkt.data) {
                        d->packet_pending = 1;
                        av_packet_move_ref(&d->pkt, &pkt);
                    }
                    ret = got_frame ? 0 : (pkt.data ? AVERROR(EAGAIN) : AVERROR_EOF);
                }
            } else {
                if (avcodec_send_packet(d->avctx, &pkt) == AVERROR(EAGAIN)) {
                    av_log(d->avctx, AV_LOG_ERROR, "Receive_frame and send_packet both returned EAGAIN, which is an API violation.\n");
                    d->packet_pending = 1;
                    av_packet_move_ref(&d->pkt, &pkt);
                }
            }
            av_packet_unref(&pkt);
        }
    }

}

avcodec_send_packet还有非常复杂的调用栈,纯好奇看了一下,没有完全看懂,只能附一个链接,后面再看看了。

avcodec_send_packet-> decode_receive_frame_internal-> decode_simple_receive_frame-> decode_simple_internal-> ff_decode_get_packet, ff_thread_decode_frame-> ff_thread_decode_frame-> submit_packet-> ff_attach_decode_data-> ff_attach_decode_data->

blog.csdn.net/weixin_4336…