JCHub

  • Home
  • Category
    • A/V
    • WebRTC
    • Beauty of Programming
    • Linux
    • Windows
    • Moments of Life
    • Campus Life
  • Reference
    • API Reference
    • Utilities
    • AV Test
    • Doc
  • Message Board
  • About
JCHub
Code as My Sword, Lost in Obsession
  1. Main page
  2. A/V
  3. Main content

avcodec_decode_video2解码得不到图像

2016年4月8日 3009hotness 10likes 0comments

如下所示代码:

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while (av_read_frame(pFormatCtx, &packet) >= 0) {
// Is this a packet from the video stream?
if (packet.stream_index == videoStream) {
// Decode video frame
int iret = avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
 
// Did we get a video frame?
if (frameFinished) {
do something······
}
}
 
// Free the packet that was allocated by av_read_frame
av_packet_unref(&packet);
}

在使用avcodec_decode_video2函数解码时经常会遇到frameFinished == 0,也就是无法得到一帧解码后的图像。有些人可能会怀疑是哪里出错了,其实这是正常的现象,ffmpeg内部解码时做了处理。处理如下:

1)该帧为B帧,由于B帧是前后参考帧,需要结合前面的I帧或者P帧,以及后面的P帧来生成完整图像,所以该帧如果是B帧,就无法立即解码,所以返回的frameFinished为0,需要解码完后一帧后才可以解码该帧;

2)缓存处理,解码器解码时会缓存几帧提高程序的效率,防止程序在解码这一步等待太久。当解码到最后av_read_frame没有返回新的packet时,由于解码器存在缓存,所以最后我们需要清空解码器,通过传入空的packet给avcodec_decode_video2,直到没有新的解码后的frame返回这一方法来清空解码器。

This article is licensed with Creative Commons Attribution-NonCommercial-No Derivatives 4.0 International License
Tag: ffmpeg 音视频
Last updated:2016年4月8日

Jeff

管理员——代码为剑,如痴如醉

Tip the author Like
< Last article
Next article >

Comments

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
Cancel

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts
  • ffmpeg视频编码YUV与AVFrame对应关系
  • 安防视频播放秒开优化
  • mp4文件elst研究
  • 告别CapCut收费!开源视频剪辑工具OpenCut 来了
  • 音视频开发入门:视频基础
Categories

COPYRIGHT © 2026 jianchihu.net. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang