从这张图开始,主要介绍ffplay的读取线程部分。
从图中可以看出,解码线程的主要工作内容是将资源包从待解码列队中取出,然后送进解码器,最后将解码出的数据帧放入帧队列中,等待SDL获取播放。
【学习地址】:FFmpeg/WebRTC/RTMP/NDK/Android音视频流媒体高级开发
【文章福利】:免费领取更多音视频学习资料包、大厂面试题、技术视频和学习路线图,资料包括(C/C++,Linux,FFmpeg webRTC rtmp hls rtsp ffplay srs 等等)有需要的可以点击1079654574加群领取哦~
解码过程
解码线程是在打开流的时候创建的,也就是在函数stream_component_open创建的,视频解码线程的工作函数是video_thread。
以下是函数video_thread的内容,可以看到去掉filter的相关处理后,这个函数是非常精简的,就是在for循环中通过函数get_video_frame获取到一帧图像数据后,调整数据帧的pts,然后将数据帧放入帧队列中:
/** * 视频解码 * @param arg * @return */ static int video_thread(void *arg) { VideoState *is = arg; // 分配frame AVFrame *frame = av_frame_alloc(); double pts; double duration; int ret; AVRational tb = is->video_st->time_base; AVRational frame_rate = av_guess_frame_rate(is->ic, is->video_st, NULL); #if CONFIG_AVFILTER AVFilterGraph *graph = NULL; AVFilterContext *filt_out = NULL, *filt_in = NULL; int last_w = 0; int last_h = 0; enum AVPixelFormat last_format = -2; int last_serial = -1; int last_vfilter_idx = 0; #endif if (!frame) return AVERROR(ENOMEM); for (;;) { // 获取一帧视频图像,返回负值会退出线程,什么时候会返回负值?问管家VideoState ret = get_video_frame(is, frame); if (ret < 0) goto the_end; if (!ret) continue; #if CONFIG_AVFILTER if ( last_w != frame->width || last_h != frame->height || last_format != frame->format || last_serial != is->viddec.pkt_serial || last_vfilter_idx != is->vfilter_idx) { av_log(NULL, AV_LOG_DEBUG, "Video frame changed from size:%dx%d format:%s serial:%d to size:%dx%d format:%s serial:%d\n", last_w, last_h, (const char *)av_x_if_null(av_get_pix_fmt_name(last_format), "none"), last_serial, frame->width, frame->height, (const char *)av_x_if_null(av_get_pix_fmt_name