马老爷 發表於 2020-7-16 09:00:40

ffmpeg播放器实现详解之视频显示(推荐)

<p>FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。它包括了目前领先的音/视频编码库libavcodec。 FFmpeg是在 Linux 下开发出来的,但它可以在包括 Windows 在内的大多数操作系统中编译。这个项目是由 Fabrice Bellard 发起的,现在由 Michael Niedermayer 主持。可以轻易地实现多种视频格式之间的相互转换,例如可以将摄录下的视频avi等转成现在视频网站所采用的flv格式。</p>
<p>ffplay是ffmpeg源码中一个自带的开源播放器实例,同时支持本地视频文件的播放以及在线流媒体播放,功能非常强大。</p>
<blockquote>
<p>FFplay: FFplay is a very simple and portable media player using the FFmpeg libraries and the SDL library. It is mostly used as a testbed for the various FFmpeg APIs.</p>
</blockquote>
<p>ffplay中的代码充分调用了ffmpeg中的函数库,因此,想学习ffmpeg的使用,或基于ffmpeg开发一个自己的播放器,ffplay都是一个很好的切入点。</p>
<p>由于ffmpeg本身的开发文档比较少,且ffplay播放器源码的实现相对复杂,除了基础的ffmpeg组件调用外,还包含视频帧的渲染、音频帧的播放、音视频同步策略及线程调度等问题。</p>
<p>因此,这里我们以ffmpeg官网推荐的一个ffplay播放器简化版本的开发例程为基础,在此基础上循序渐进由浅入深,最终探讨实现一个视频播放器的完整逻辑。</p>
<p>在上篇文章中介绍了如果搭建一个基于ffmpeg的播放器框架<br />
本文在上篇文章的基础上,继续讨论如何将ffmpeg解码出的视频帧进行渲染显示</p>
<p><span style="color: #ff0000"><strong>1、视频帧渲染</strong></span></p>
<p>上篇文章中介绍了如何基于ffmpeg搭建一个视频播放器框架,运行程序后可以看到,除了生成几张图片外,程序好像什么也做不了。</p>
<p>这是因为ffmpeg通过其封装的api及组件,为我们屏蔽了不同视频封装格式及编码格式的差异,以统一的api接口提供给开发者使用,开发者不需要了解每种编码方式及封装方式具体的技术细节,只需要调用ffmpeg提供的api就可以完成解封装和解码的操作了。</p>
<p>至于视频帧的渲染及音频帧的播放,ffmpeg就无能为力了,因此需要借助类似sdl库等其他第三方组件来完成。</p>
<p>这里讲述如何使用sdl库完成视频帧的渲染,sdl在底层封装了opengl图形库,sdl提供的api简化了opengl的绘图操作,为开发者提供了很多便利的操作,当然,你也可以采用其他系统支持的图形库来绘制视频帧。</p>
<p>sdl库的编译安装详见[公众号:断点实验室]的前述文章 。</p>
<p><strong>1.1 渲染环境搭建</strong></p>
<p>一个视频帧在显示前,需要准备一个用于显示视频的窗口对象,以及附着在窗口上的画布对象</p>
<p>创建SDL窗口,并指定图像尺寸及像素个数</p>
<div class="jb51code">
<pre class="brush:plain;">
// 创建SDL窗口,并指定图像尺寸
screen = SDL_SetVideoMode(pCodecCtx-&gt;width, pCodecCtx-&gt;height, 24, 0);</pre>
</div>
<p>创建画布对象</p>
<div class="jb51code">
<pre class="brush:plain;">
// 创建画布对象
bmp = SDL_CreateYUVOverlay(pCodecCtx-&gt;width, pCodecCtx-&gt;height, SDL_YV12_OVERLAY, screen);</pre>
</div>
<p><strong>1.2 视频帧渲染</strong></p>
<p>在窗口和画布对象创建完成后,就可以开始视频帧的渲染显示了。</p>
<p>在对画布对象操作前,需要对其加线程锁保护,避免其他线程对画布中的内容进行竞争性访问(后面的内容很快会涉及到多线程环境的开发)。对线程操作不熟悉的同学可以了解一下在多线程环境下,多个线程对临界区资源的竞争性访问与线程同步操作。</p>
<blockquote>
<p>SDL_LockYUVOverlay(bmp);//locks the overlay for direct access to pixel data</p>
</blockquote>
<p>向画布注入解码后的视频帧</p>
<div class="jb51code">
<pre class="brush:plain;">
sws_scale(sws_ctx, (uint8_t const * const *)pFrame-&gt;data, pFrame-&gt;linesize, 0, pCodecCtx-&gt;height, pict.data, pict.linesize);</pre>
</div>
<p>在画布对象的视频帧填充操作完成后,释放sdl线程锁。</p>
<div class="jb51code">
<pre class="brush:plain;">
//Unlocks a previously locked overlay. An overlay must be unlocked before it can be displayed
SDL_UnlockYUVOverlay(bmp);</pre>
</div>
<p>对视频帧的渲染</p>
<div class="jb51code">
<pre class="brush:plain;">
SDL_DisplayYUVOverlay(bmp, &amp;rect);//图像渲染</pre>
</div>
<p>可以看到,由于借助了sdl封装的api绘图接口,视频帧的渲染还是非常容易的,如果直接采用opengl绘图,绘制过程会相对复杂些,例程主要的目的是为了介绍ffmpeg的使用,因此,这里采用sdl简化了渲染流程。</p>
<p><strong>1.3 项目源码编译</strong></p>
<p>本例程和上篇文章中用到的编译方法完全一样</p>
<div class="jb51code">
<pre class="brush:plain;">
tutorial02: tutorial02.c
        gcc -o tutorial02 -g3 tutorial02.c -I${FFMPEG_INCLUDE} -I${SDL_INCLUDE} \
        -L${FFMPEG_LIB} -lavutil -lavformat -lavcodec -lswscale -lswresample -lz -lm \
        `sdl-config --cflags --libs`

clean:
        rm -rf tutorial02</pre>
</div>
<p>执行make命令开始编译,编译完成后,可在源码目录生成名为的可执行文件。</p>
<p>可通过ldd命令查询当前可执行文件所有依赖的动态库。</p>
<p><strong>1.4 验证</strong></p>
<p>执行命令,可以看到有画面输出了。</p>
<div class="jb51code">
<pre class="brush:plain;">
./tutorial02 rtmp://58.200.131.2:1935/livetv/hunantv</pre>
</div>
<p style="text-align: center"><img alt="" loading="lazy" src="https://img.jbzj.com/file_images/article/202007/202007160833391.jpg" /></p>
<p>虽然画面已经有了,但还缺少声音,下篇文章会继续完善我们的播放器开发,讨论如何播放声音。</p>
<p><span style="color: #ff0000"><strong>2、视频播放中可能出现的问题</strong></span></p>
<p>视频播放中可能会出现以下两个问题</p>
<p>sdl找不到音频设备 SDL_OpenAudio no such audio device</p>
<p>sdl无法初始化 Could not initialize SDL, no available video device</p>
<p>解决方法见[公众号:断点实验室]的前述文章 。</p>
<p><span style="color: #ff0000"><strong>3、源码清单</strong></span></p>
<p>源码非常的简单,仅在上篇的内容基础上,增加了sdl渲染环境的搭建,整个源码仍然运行在main的主线程中,后面的内容会涉及多个线程的调度及同步的场景。</p>
<div class="jb51code">
<pre class="brush:java;">
// tutorial02.c
// A pedagogical video player that will stream through every video frame as fast as it can.
//
// This tutorial was written by Stephen Dranger (dranger@gmail.com).
//
// Code based on FFplay, Copyright (c) 2003 Fabrice Bellard,
// and a tutorial by Martin Bohme (boehme@inb.uni-luebeckREMOVETHIS.de)
// Tested on Gentoo, CVS version 5/01/07 compiled with GCC 4.1.1
//
// Updates tested on:
// Mac OS X 10.11.6
// Apple LLVM version 8.0.0 (clang-800.0.38)
//
// Use
//
// $ gcc -o tutorial02 tutorial02.c -lavutil -lavformat -lavcodec -lswscale -lz -lm `sdl-config --cflags --libs`
//
// to build (assuming libavutil/libavformat/libavcodec/libswscale are correctly installed your system).
//
// Run using
//
// $ tutorial02 myvideofile.mpg
//
// to play the video stream on your screen.


#include &lt;libavcodec/avcodec.h&gt;
#include &lt;libavformat/avformat.h&gt;
#include &lt;libswscale/swscale.h&gt;

#include &lt;SDL.h&gt;
#include &lt;SDL_thread.h&gt;

#ifdef __MINGW32__
#undef main // Prevents SDL from overriding main().
#endif

#include &lt;stdio.h&gt;

int main(int argc, char *argv[]) {
/*--------------参数定义-------------*/
        AVFormatContext *pFormatCtx = NULL;//保存文件容器封装信息及码流参数的结构体
        AVCodecContext *pCodecCtx = NULL;//解码器上下文对象,解码器依赖的相关环境、状态、资源以及参数集的接口指针
        AVCodec *pCodec = NULL;//保存编解码器信息的结构体,提供编码与解码的公共接口,可以看作是编码器与解码器的一个全局变量
        AVPacket packet;//负责保存压缩编码数据相关信息的结构体,每帧图像由一到多个packet包组成
        AVFrame *pFrame = NULL;//保存音视频解码后的数据,如状态信息、编解码器信息、宏块类型表,QP表,运动矢量表等数据
        struct SwsContext *sws_ctx = NULL;//描述转换器参数的结构体
        AVDictionary *optionsDict = NULL;

        SDL_Surface *screen = NULL;//SDL绘图窗口,A structure that contains a collection of pixels used in software blitting
        SDL_Overlay *bmp = NULL;//SDL画布
        SDL_Rect rect;//SDL矩形对象
        SDL_Event event;//SDL事件对象

        int i, videoStream;//循环变量,视频流类型标号
        int frameFinished;//解码操作是否成功标识

/*-------------参数初始化------------*/
        if (argc&lt;2) {//检查输入参数个数是否正确
                fprintf(stderr, "Usage: test &lt;file&gt;\n");
                exit(1);
        }
        // Register all available formats and codecs,注册所有ffmpeg支持的多媒体格式及编解码器
        av_register_all();

        /*-----------------------
       * Open video file,打开视频文件,读文件头内容,取得文件容器的封装信息及码流参数并存储在pFormatCtx中
       * read the file header and stores information about the file format in the AVFormatContext structure
       * The last three arguments are used to specify the file format, buffer size, and format options
       * but by setting this to NULL or 0, libavformat will auto-detect these
       -----------------------*/
        if (avformat_open_input(&amp;pFormatCtx, argv, NULL, NULL) != 0) {
                return -1; // Couldn't open file.
        }

        /*-----------------------
       * 取得文件中保存的码流信息,并填充到pFormatCtx-&gt;stream 字段
       * check out &amp; Retrieve the stream information in the file
       * then populate pFormatCtx-&gt;stream with the proper information
       * pFormatCtx-&gt;streams is just an array of pointers, of size pFormatCtx-&gt;nb_streams
       -----------------------*/
        if (avformat_find_stream_info(pFormatCtx, NULL) &lt; 0) {
                return -1; // Couldn't find stream information.
        }

        // Dump information about file onto standard error,打印pFormatCtx中的码流信息
        av_dump_format(pFormatCtx, 0, argv, 0);

        // Find the first video stream.
        videoStream = -1;//视频流类型标号初始化为-1
        for(i = 0; i &lt; pFormatCtx-&gt;nb_streams; i++) {//遍历文件中包含的所有流媒体类型(视频流、音频流、字幕流等)
                if (pFormatCtx-&gt;streams-&gt;codec-&gt;codec_type == AVMEDIA_TYPE_VIDEO) {//若文件中包含有视频流
                        videoStream = i;//用视频流类型的标号修改标识,使之不为-1
                        break;
                }
        }
        if (videoStream == -1) {//检查文件中是否存在视频流
                return -1; // Didn't find a video stream.
        }

        // Get a pointer to the codec context for the video stream,根据流类型标号从pFormatCtx-&gt;streams中取得视频流对应的解码器上下文
        pCodecCtx = pFormatCtx-&gt;streams-&gt;codec;

        /*-----------------------
       * Find the decoder for the video stream,根据视频流对应的解码器上下文查找对应的解码器,返回对应的解码器(信息结构体)
       * The stream's information about the codec is in what we call the "codec context.
       * This contains all the information about the codec that the stream is using
       -----------------------*/
        pCodec = avcodec_find_decoder(pCodecCtx-&gt;codec_id);
        if (pCodec == NULL) {//检查解码器是否匹配
                fprintf(stderr, "Unsupported codec!\n");
                return -1; // Codec not found.
        }

        // Open codec,打开解码器
        if (avcodec_open2(pCodecCtx, pCodec, &amp;optionsDict) &lt; 0) {
                return -1; // Could not open codec.
        }

        // Allocate video frame,为解码后的视频信息结构体分配空间并完成初始化操作(结构体中的图像缓存按照下面两步手动安装)
        pFrame = av_frame_alloc();

        // Initialize SWS context for software scaling,设置图像转换像素格式为AV_PIX_FMT_YUV420P
        sws_ctx = sws_getContext(pCodecCtx-&gt;width, pCodecCtx-&gt;height, pCodecCtx-&gt;pix_fmt, pCodecCtx-&gt;width, pCodecCtx-&gt;height, AV_PIX_FMT_YUV420P, SWS_BILINEAR, NULL, NULL, NULL);

        //SDL_Init initialize the Event Handling, File I/O, and Threading subsystems,初始化SDL
        if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {//initialize the video audio &amp; timer subsystem
                fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());//tell the library what features we're going to use
                exit(1);
        }

        // Make a screen to put our video,在SDL2.0中SDL_SetVideoMode及SDL_Overlay已经弃用,改为SDL_CreateWindow及SDL_CreateRenderer创建窗口及着色器
#ifndef __DARWIN__
        screen = SDL_SetVideoMode(pCodecCtx-&gt;width, pCodecCtx-&gt;height, 24, 0);//创建SDL窗口,并指定图像尺寸
#else
        screen = SDL_SetVideoMode(pCodecCtx-&gt;width, pCodecCtx-&gt;height, 24, 0);//创建SDL窗口,并指定图像尺寸
#endif
        if (!screen) {//检查SDL窗口是否创建成功
                fprintf(stderr, "SDL: could not set video mode - exiting\n");
                exit(1);
        }
        SDL_WM_SetCaption(argv,0);//用输入文件名设置SDL窗口标题

        // Allocate a place to put our YUV image on that screen,创建画布对象
        bmp = SDL_CreateYUVOverlay(pCodecCtx-&gt;width, pCodecCtx-&gt;height, SDL_YV12_OVERLAY, screen);

/*--------------循环解码-------------*/
        i = 0;// Read frames and save first five frames to disk
        /*-----------------------
       * read in a packet and store it in the AVPacket struct
       * ffmpeg allocates the internal data for us,which is pointed to by packet.data
       * this is freed by the av_free_packet()
       -----------------------*/
        while(av_read_frame(pFormatCtx, &amp;packet) &gt;= 0) {//从文件中依次读取每个图像编码数据包,并存储在AVPacket数据结构中
                // Is this a packet from the video stream,检查数据包类型
                if (packet.stream_index == videoStream) {
               /*-----------------------
                      * Decode video frame,解码完整的一帧数据,并将frameFinished设置为true
                       * 可能无法通过只解码一个packet就获得一个完整的视频帧frame,可能需要读取多个packet才行
                      * avcodec_decode_video2()会在解码到完整的一帧时设置frameFinished为真
                       * Technically a packet can contain partial frames or other bits of data
                       * ffmpeg's parser ensures that the packets we get contain either complete or multiple frames
                       * convert the packet to a frame for us and set frameFinisned for us when we have the next frame
                     -----------------------*/
                        avcodec_decode_video2(pCodecCtx, pFrame, &amp;frameFinished, &amp;packet);

                        // Did we get a video frame,检查是否解码出完整一帧图像
                        if (frameFinished) {
                                SDL_LockYUVOverlay(bmp);//locks the overlay for direct access to pixel data,原子操作,保护像素缓冲区临界资源

                                AVFrame pict;//保存转换为AV_PIX_FMT_YUV420P格式的视频帧
                                pict.data = bmp-&gt;pixels;//将转码后的图像与画布的像素缓冲器关联
                                pict.data = bmp-&gt;pixels;
                                pict.data = bmp-&gt;pixels;

                                pict.linesize = bmp-&gt;pitches;//将转码后的图像扫描行长度与画布像素缓冲区的扫描行长度相关联
                                pict.linesize = bmp-&gt;pitches;//linesize-Size, in bytes, of the data for each picture/channel plane
                                pict.linesize = bmp-&gt;pitches;//For audio, only linesize may be set

                                // Convert the image into YUV format that SDL uses,将解码后的图像转换为AV_PIX_FMT_YUV420P格式,并赋值到pict对象
                                sws_scale(sws_ctx, (uint8_t const * const *)pFrame-&gt;data, pFrame-&gt;linesize, 0, pCodecCtx-&gt;height, pict.data, pict.linesize);

                                SDL_UnlockYUVOverlay(bmp);//Unlocks a previously locked overlay. An overlay must be unlocked before it can be displayed

                                //设置矩形显示区域
                                rect.x = 0;
                                rect.y = 0;
                                rect.w = pCodecCtx-&gt;width;
                                rect.h = pCodecCtx-&gt;height;
                                SDL_DisplayYUVOverlay(bmp, &amp;rect);//图像渲染
                        }
                }

                // Free the packet that was allocated by av_read_frame,释放AVPacket数据结构中编码数据指针
                av_packet_unref(&amp;packet);
               
                /*-------------------------
               * 在每次循环中从SDL后台队列取事件并填充到SDL_Event对象中
               * SDL的事件系统使得你可以接收用户的输入,从而完成一些控制操作
               * SDL_PollEvent() is the favored way of receiving system events
               * since it can be done from the main loop and does not suspend the main loop
               * while waiting on an event to be posted
               * poll for events right after we finish processing a packet
               ------------------------*/
                SDL_PollEvent(&amp;event);
                switch (event.type) {//检查SDL事件对象
                        case SDL_QUIT://退出事件
                                printf("SDL_QUIT\n");
                                SDL_Quit();//退出操作
                                exit(0);//结束进程
                                break;
                        default:
                                break;
                }//end for switch
        }//end for while
/*--------------参数撤销-------------*/
        // Free the YUV frame.
        av_free(pFrame);

        // Close the codec.
        avcodec_close(pCodecCtx);

        // Close the video file.
        avformat_close_input(&amp;pFormatCtx);

        return 0;
}</pre>
</div>
<p>到此这篇关于ffmpeg播放器实现详解 - 视频显示的文章就介绍到这了,更多相关ffmpeg播放器实现内容请搜索琼殿技术社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持琼殿技术社区!</p>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>Python&nbsp;如何利用ffmpeg&nbsp;处理视频素材</li><li>音视频基本概念和FFmpeg的简单入门教程详解</li><li>ffmpeg播放器实现详解之框架搭建过程</li><li>FFmpeg视频处理入门教程(新手必看)</li><li>ffmpeg网页视频流m3u8&nbsp;ts实现视频下载</li></ul>
                            </div>

                        </div>
                        <!--endmain-->
頁: [1]
查看完整版本: ffmpeg播放器实现详解之视频显示(推荐)