`

[音频处理API] ffmpeg视频文件解码代码流程分析

    博客分类:
  • C
阅读更多
通过学习网上博友“浩@子”的博客,搞懂了1)ffmpeg源码中libavformat和libaccodec之间的区别;2)ffmpeg中视频解码的代码流程。这里我先用简要语言和伪码来对这两点进行说明,然后在二楼贴出博客原文。
     libavformat库:对音频和视频流进行分离,输入是音视频文件,输出是单独的音频流和视频流。
     libavcodec库:对音频流和视频流按照其格式进行编码和解码。编码格式转换就是在这个库完成。
     视频解码的伪码流程:
     (1)打开视频文件  av_open_input_file(&pFormatCtx, filename)
          打开filename指向的文件,将文件信息存入上下文结构pFormatCtx中。
     (2)取出视频流  av_find_stream_info(pFormatCtx)
          取出的视频流也放在上下文结构中 pFormatCtx->stream field。
      (3) 根据上下文中编码信息找到对应的编码器  pCodec = avcodec_find_decoder(pFormatCtx->codec_id)
          编码器即对应libavcodec库中对应的编解码函数。
      (4) 打开编码器   avcodec_open(pCodecCtx)
      (5) 循环调用GetNextFrame将视频流转换成一帧一帧的RGB格式。
          GetNextFrame(pFormatCtx, pFrame)
          {
              avcodec_decode_video(pFormatCtx, pFrame) //将一帧解码到YUV格式的pFrame
          }
          while(GetNextFrame(pFormatCtx, pCodecCtx, pFrame))
          {
              img_covert(pFrameRGB, pFrame)  //格式从YUV转换为RGB
              DoSomething()  //帧图片存盘或播放
          }
       (6) 释放分配的资源  av_free(pFrame)
                           av_free(pFremeRGB)
                           avcodec_close(pCodecCtx) //关闭编解码器
                           av_close_input_file(pFormatCtx) //关闭原始视频文件
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics