FFmpeg - avcodec_receive_frame doesn't receive all frames and lose frames before receving frames The 2019 Stack Overflow Developer Survey Results Are InFetch frame count with ffmpegWhat are all codecs and formats supported by FFmpeg?Fastest way to extract frames using ffmpeg?Frame Number Overlay With FFmpegFFmpeg av_read_frame returns a size but no data?FFMPEG Dropping all framesFFmpeg resample audio while decodingReplacing av_read_frame() to reduce delayFFmpeg: Decoding AVPackets received from UDP socketLosing frames when clipping videos with ffmpeg
Understanding the implication of what "well-defined" means for the operation in quotient group
Is "plugging out" electronic devices an American expression?
How to deal with fear of taking dependencies
Could JWST stay at L2 "forever"?
The difference between dialogue marks
Time travel alters history but people keep saying nothing's changed
CiviEvent: Public link for events of a specific type
Why Did Howard Stark Use All The Vibranium They Had On A Prototype Shield?
Should I use my personal or workplace e-mail when registering to external websites for work purpose?
Is it possible for the two major parties in the UK to form a coalition with each other instead of a much smaller party?
Carnot-Caratheodory metric
Inversion Puzzle
Is domain driven design an anti-SQL pattern?
What tool would a Roman-age civilization have to grind silver and other metals into dust?
What do hard-Brexiteers want with respect to the Irish border?
Where does the "burst of radiance" from Holy Weapon originate?
Landlord wants to switch my lease to a "Land contract" to "get back at the city"
"What time...?" or "At what time...?" - what is more grammatically correct?
How long do I have to send payment?
Why is my p-value correlated to difference between means in two sample tests?
What does "rabbited" mean/imply in this sentence?
How are circuits which use complex ICs normally simulated?
Is flight data recorder erased after every flight?
Idiomatic way to prevent slicing?
FFmpeg - avcodec_receive_frame doesn't receive all frames and lose frames before receving frames
The 2019 Stack Overflow Developer Survey Results Are InFetch frame count with ffmpegWhat are all codecs and formats supported by FFmpeg?Fastest way to extract frames using ffmpeg?Frame Number Overlay With FFmpegFFmpeg av_read_frame returns a size but no data?FFMPEG Dropping all framesFFmpeg resample audio while decodingReplacing av_read_frame() to reduce delayFFmpeg: Decoding AVPackets received from UDP socketLosing frames when clipping videos with ffmpeg
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
The vcodec_receive_frame
function didn't receive the rest of the frames. I tested that there were totally 132 frames of the video and it only received 125 frames losing 7 frames at the END of the video. How can I get the lost frames back?
But something weird happened. As you can see the output from my MyDecode::receiveFrame()
function. The code inside the block if (ret != 0)
executed first, but the lost frames are at the end of the video. So how could they come out first? What caused this happen?
MyDecode.cpp
AVFrame* MyDecode::receiveFrame()
mux.lock();
if (!codecCtx)
mux.unlock();
return 0;
AVFrame* frame = av_frame_alloc();
int ret = avcodec_receive_frame(codecCtx, frame);
mux.unlock();
if (ret != 0)
static int lost_frames = 1;
std::cout << "Lost frames: " << lost_frames << std::endl;
lost_frames += 1;
av_frame_free(&frame);
return nullptr;
std::cout << "Received frames: " << received_frame_num << std::endl;
received_frame_num += 1;
return frame;
bool MyDecode::sendPacket(AVPacket* packet)
!packet->data
Console output
Total frames: 132
Lost frames: 1
Lost frames: 2
Lost frames: 3
Lost frames: 4
Lost frames: 5
Lost frames: 6
Lost frames: 7
Received frames: 1
Received frames: 2
Received frames: 3
................
Received frames: 125
UPDATE:
MyDemux.cpp
AVPacket* MyDemux::readFrame()
mux.lock();
if (!formatCtx)
std::cout << "formaetCtx is null" << std::endl;
mux.unlock();
return nullptr;
AVPacket* packet = av_packet_alloc();
if (!packet)
std::cout << "packet is null" << std::endl;
mux.unlock();
return nullptr;
int ret = av_read_frame(formatCtx, packet);
if (ret != 0)
while (true)
av_read_frame(formatCtx, nullptr);
mux.unlock();
av_packet_free(&packet);
av_packet_unref(packet);
return nullptr;
media_type = packet->stream_index;
mux.unlock();
return packet;
main.cpp
while (true)
AVPacket* pkt = demux.readFrame();
if (demux.get_media_type() == 0)
AVFrame* frame = video_decode.receiveFrame();
videoWidget->paintFrame(frame);
else if (demux.get_media_type() == 1)
if (!pkt)
std::cout << "to break" << std::endl;
break;
ffmpeg
add a comment |
The vcodec_receive_frame
function didn't receive the rest of the frames. I tested that there were totally 132 frames of the video and it only received 125 frames losing 7 frames at the END of the video. How can I get the lost frames back?
But something weird happened. As you can see the output from my MyDecode::receiveFrame()
function. The code inside the block if (ret != 0)
executed first, but the lost frames are at the end of the video. So how could they come out first? What caused this happen?
MyDecode.cpp
AVFrame* MyDecode::receiveFrame()
mux.lock();
if (!codecCtx)
mux.unlock();
return 0;
AVFrame* frame = av_frame_alloc();
int ret = avcodec_receive_frame(codecCtx, frame);
mux.unlock();
if (ret != 0)
static int lost_frames = 1;
std::cout << "Lost frames: " << lost_frames << std::endl;
lost_frames += 1;
av_frame_free(&frame);
return nullptr;
std::cout << "Received frames: " << received_frame_num << std::endl;
received_frame_num += 1;
return frame;
bool MyDecode::sendPacket(AVPacket* packet)
!packet->data
Console output
Total frames: 132
Lost frames: 1
Lost frames: 2
Lost frames: 3
Lost frames: 4
Lost frames: 5
Lost frames: 6
Lost frames: 7
Received frames: 1
Received frames: 2
Received frames: 3
................
Received frames: 125
UPDATE:
MyDemux.cpp
AVPacket* MyDemux::readFrame()
mux.lock();
if (!formatCtx)
std::cout << "formaetCtx is null" << std::endl;
mux.unlock();
return nullptr;
AVPacket* packet = av_packet_alloc();
if (!packet)
std::cout << "packet is null" << std::endl;
mux.unlock();
return nullptr;
int ret = av_read_frame(formatCtx, packet);
if (ret != 0)
while (true)
av_read_frame(formatCtx, nullptr);
mux.unlock();
av_packet_free(&packet);
av_packet_unref(packet);
return nullptr;
media_type = packet->stream_index;
mux.unlock();
return packet;
main.cpp
while (true)
AVPacket* pkt = demux.readFrame();
if (demux.get_media_type() == 0)
AVFrame* frame = video_decode.receiveFrame();
videoWidget->paintFrame(frame);
else if (demux.get_media_type() == 1)
if (!pkt)
std::cout << "to break" << std::endl;
break;
ffmpeg
add a comment |
The vcodec_receive_frame
function didn't receive the rest of the frames. I tested that there were totally 132 frames of the video and it only received 125 frames losing 7 frames at the END of the video. How can I get the lost frames back?
But something weird happened. As you can see the output from my MyDecode::receiveFrame()
function. The code inside the block if (ret != 0)
executed first, but the lost frames are at the end of the video. So how could they come out first? What caused this happen?
MyDecode.cpp
AVFrame* MyDecode::receiveFrame()
mux.lock();
if (!codecCtx)
mux.unlock();
return 0;
AVFrame* frame = av_frame_alloc();
int ret = avcodec_receive_frame(codecCtx, frame);
mux.unlock();
if (ret != 0)
static int lost_frames = 1;
std::cout << "Lost frames: " << lost_frames << std::endl;
lost_frames += 1;
av_frame_free(&frame);
return nullptr;
std::cout << "Received frames: " << received_frame_num << std::endl;
received_frame_num += 1;
return frame;
bool MyDecode::sendPacket(AVPacket* packet)
!packet->data
Console output
Total frames: 132
Lost frames: 1
Lost frames: 2
Lost frames: 3
Lost frames: 4
Lost frames: 5
Lost frames: 6
Lost frames: 7
Received frames: 1
Received frames: 2
Received frames: 3
................
Received frames: 125
UPDATE:
MyDemux.cpp
AVPacket* MyDemux::readFrame()
mux.lock();
if (!formatCtx)
std::cout << "formaetCtx is null" << std::endl;
mux.unlock();
return nullptr;
AVPacket* packet = av_packet_alloc();
if (!packet)
std::cout << "packet is null" << std::endl;
mux.unlock();
return nullptr;
int ret = av_read_frame(formatCtx, packet);
if (ret != 0)
while (true)
av_read_frame(formatCtx, nullptr);
mux.unlock();
av_packet_free(&packet);
av_packet_unref(packet);
return nullptr;
media_type = packet->stream_index;
mux.unlock();
return packet;
main.cpp
while (true)
AVPacket* pkt = demux.readFrame();
if (demux.get_media_type() == 0)
AVFrame* frame = video_decode.receiveFrame();
videoWidget->paintFrame(frame);
else if (demux.get_media_type() == 1)
if (!pkt)
std::cout << "to break" << std::endl;
break;
ffmpeg
The vcodec_receive_frame
function didn't receive the rest of the frames. I tested that there were totally 132 frames of the video and it only received 125 frames losing 7 frames at the END of the video. How can I get the lost frames back?
But something weird happened. As you can see the output from my MyDecode::receiveFrame()
function. The code inside the block if (ret != 0)
executed first, but the lost frames are at the end of the video. So how could they come out first? What caused this happen?
MyDecode.cpp
AVFrame* MyDecode::receiveFrame()
mux.lock();
if (!codecCtx)
mux.unlock();
return 0;
AVFrame* frame = av_frame_alloc();
int ret = avcodec_receive_frame(codecCtx, frame);
mux.unlock();
if (ret != 0)
static int lost_frames = 1;
std::cout << "Lost frames: " << lost_frames << std::endl;
lost_frames += 1;
av_frame_free(&frame);
return nullptr;
std::cout << "Received frames: " << received_frame_num << std::endl;
received_frame_num += 1;
return frame;
bool MyDecode::sendPacket(AVPacket* packet)
!packet->data
Console output
Total frames: 132
Lost frames: 1
Lost frames: 2
Lost frames: 3
Lost frames: 4
Lost frames: 5
Lost frames: 6
Lost frames: 7
Received frames: 1
Received frames: 2
Received frames: 3
................
Received frames: 125
UPDATE:
MyDemux.cpp
AVPacket* MyDemux::readFrame()
mux.lock();
if (!formatCtx)
std::cout << "formaetCtx is null" << std::endl;
mux.unlock();
return nullptr;
AVPacket* packet = av_packet_alloc();
if (!packet)
std::cout << "packet is null" << std::endl;
mux.unlock();
return nullptr;
int ret = av_read_frame(formatCtx, packet);
if (ret != 0)
while (true)
av_read_frame(formatCtx, nullptr);
mux.unlock();
av_packet_free(&packet);
av_packet_unref(packet);
return nullptr;
media_type = packet->stream_index;
mux.unlock();
return packet;
main.cpp
while (true)
AVPacket* pkt = demux.readFrame();
if (demux.get_media_type() == 0)
AVFrame* frame = video_decode.receiveFrame();
videoWidget->paintFrame(frame);
else if (demux.get_media_type() == 1)
if (!pkt)
std::cout << "to break" << std::endl;
break;
ffmpeg
ffmpeg
edited Mar 22 at 7:13
Jinx
asked Mar 22 at 2:51
JinxJinx
1256
1256
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You have to send NULL pkts to the decoder to drain all pending frames.
From avcodec.h
End of stream situations. These require "flushing" (aka draining)
the codec, as the codec might buffer multiple frames or packets
internally for performance or out of necessity (consider B-frames).
This is handled as follows:
- Instead of valid input, send NULL to the avcodec_send_packet() (decoding)
or avcodec_send_frame() (encoding) functions. This will enter draining
mode.
- Call avcodec_receive_frame() (decoding) or avcodec_receive_packet()
(encoding) in a loop until AVERROR_EOF is returned. The functions will
not return AVERROR(EAGAIN), unless you forgot to enter draining mode.
- Before decoding can be resumed again, the codec has to be reset with
avcodec_flush_buffers().
I've tried. But I have no ide how to do this sepecifically like where should I placeavcodec_send_frame(codecCtx, NULL)
.
– Jinx
Mar 22 at 7:01
Are you reading the input using av_read_frame?
– Gyan
Mar 22 at 7:10
Yes I updated it
– Jinx
Mar 22 at 7:14
Once you receive EOF from the input, skip this check:if (!packet || !packet->data || packet->size == 0)
and send NULL till you receive no frames.
– Gyan
Mar 22 at 7:29
int ret = avcodec_receive_frame(codecCtx, frame); if (ret == AVERROR_EOF)
like this? not working
– Jinx
Mar 22 at 8:04
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55292212%2fffmpeg-avcodec-receive-frame-doesnt-receive-all-frames-and-lose-frames-before%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have to send NULL pkts to the decoder to drain all pending frames.
From avcodec.h
End of stream situations. These require "flushing" (aka draining)
the codec, as the codec might buffer multiple frames or packets
internally for performance or out of necessity (consider B-frames).
This is handled as follows:
- Instead of valid input, send NULL to the avcodec_send_packet() (decoding)
or avcodec_send_frame() (encoding) functions. This will enter draining
mode.
- Call avcodec_receive_frame() (decoding) or avcodec_receive_packet()
(encoding) in a loop until AVERROR_EOF is returned. The functions will
not return AVERROR(EAGAIN), unless you forgot to enter draining mode.
- Before decoding can be resumed again, the codec has to be reset with
avcodec_flush_buffers().
I've tried. But I have no ide how to do this sepecifically like where should I placeavcodec_send_frame(codecCtx, NULL)
.
– Jinx
Mar 22 at 7:01
Are you reading the input using av_read_frame?
– Gyan
Mar 22 at 7:10
Yes I updated it
– Jinx
Mar 22 at 7:14
Once you receive EOF from the input, skip this check:if (!packet || !packet->data || packet->size == 0)
and send NULL till you receive no frames.
– Gyan
Mar 22 at 7:29
int ret = avcodec_receive_frame(codecCtx, frame); if (ret == AVERROR_EOF)
like this? not working
– Jinx
Mar 22 at 8:04
|
show 1 more comment
You have to send NULL pkts to the decoder to drain all pending frames.
From avcodec.h
End of stream situations. These require "flushing" (aka draining)
the codec, as the codec might buffer multiple frames or packets
internally for performance or out of necessity (consider B-frames).
This is handled as follows:
- Instead of valid input, send NULL to the avcodec_send_packet() (decoding)
or avcodec_send_frame() (encoding) functions. This will enter draining
mode.
- Call avcodec_receive_frame() (decoding) or avcodec_receive_packet()
(encoding) in a loop until AVERROR_EOF is returned. The functions will
not return AVERROR(EAGAIN), unless you forgot to enter draining mode.
- Before decoding can be resumed again, the codec has to be reset with
avcodec_flush_buffers().
I've tried. But I have no ide how to do this sepecifically like where should I placeavcodec_send_frame(codecCtx, NULL)
.
– Jinx
Mar 22 at 7:01
Are you reading the input using av_read_frame?
– Gyan
Mar 22 at 7:10
Yes I updated it
– Jinx
Mar 22 at 7:14
Once you receive EOF from the input, skip this check:if (!packet || !packet->data || packet->size == 0)
and send NULL till you receive no frames.
– Gyan
Mar 22 at 7:29
int ret = avcodec_receive_frame(codecCtx, frame); if (ret == AVERROR_EOF)
like this? not working
– Jinx
Mar 22 at 8:04
|
show 1 more comment
You have to send NULL pkts to the decoder to drain all pending frames.
From avcodec.h
End of stream situations. These require "flushing" (aka draining)
the codec, as the codec might buffer multiple frames or packets
internally for performance or out of necessity (consider B-frames).
This is handled as follows:
- Instead of valid input, send NULL to the avcodec_send_packet() (decoding)
or avcodec_send_frame() (encoding) functions. This will enter draining
mode.
- Call avcodec_receive_frame() (decoding) or avcodec_receive_packet()
(encoding) in a loop until AVERROR_EOF is returned. The functions will
not return AVERROR(EAGAIN), unless you forgot to enter draining mode.
- Before decoding can be resumed again, the codec has to be reset with
avcodec_flush_buffers().
You have to send NULL pkts to the decoder to drain all pending frames.
From avcodec.h
End of stream situations. These require "flushing" (aka draining)
the codec, as the codec might buffer multiple frames or packets
internally for performance or out of necessity (consider B-frames).
This is handled as follows:
- Instead of valid input, send NULL to the avcodec_send_packet() (decoding)
or avcodec_send_frame() (encoding) functions. This will enter draining
mode.
- Call avcodec_receive_frame() (decoding) or avcodec_receive_packet()
(encoding) in a loop until AVERROR_EOF is returned. The functions will
not return AVERROR(EAGAIN), unless you forgot to enter draining mode.
- Before decoding can be resumed again, the codec has to be reset with
avcodec_flush_buffers().
answered Mar 22 at 4:47
GyanGyan
34.5k23071
34.5k23071
I've tried. But I have no ide how to do this sepecifically like where should I placeavcodec_send_frame(codecCtx, NULL)
.
– Jinx
Mar 22 at 7:01
Are you reading the input using av_read_frame?
– Gyan
Mar 22 at 7:10
Yes I updated it
– Jinx
Mar 22 at 7:14
Once you receive EOF from the input, skip this check:if (!packet || !packet->data || packet->size == 0)
and send NULL till you receive no frames.
– Gyan
Mar 22 at 7:29
int ret = avcodec_receive_frame(codecCtx, frame); if (ret == AVERROR_EOF)
like this? not working
– Jinx
Mar 22 at 8:04
|
show 1 more comment
I've tried. But I have no ide how to do this sepecifically like where should I placeavcodec_send_frame(codecCtx, NULL)
.
– Jinx
Mar 22 at 7:01
Are you reading the input using av_read_frame?
– Gyan
Mar 22 at 7:10
Yes I updated it
– Jinx
Mar 22 at 7:14
Once you receive EOF from the input, skip this check:if (!packet || !packet->data || packet->size == 0)
and send NULL till you receive no frames.
– Gyan
Mar 22 at 7:29
int ret = avcodec_receive_frame(codecCtx, frame); if (ret == AVERROR_EOF)
like this? not working
– Jinx
Mar 22 at 8:04
I've tried. But I have no ide how to do this sepecifically like where should I place
avcodec_send_frame(codecCtx, NULL)
.– Jinx
Mar 22 at 7:01
I've tried. But I have no ide how to do this sepecifically like where should I place
avcodec_send_frame(codecCtx, NULL)
.– Jinx
Mar 22 at 7:01
Are you reading the input using av_read_frame?
– Gyan
Mar 22 at 7:10
Are you reading the input using av_read_frame?
– Gyan
Mar 22 at 7:10
Yes I updated it
– Jinx
Mar 22 at 7:14
Yes I updated it
– Jinx
Mar 22 at 7:14
Once you receive EOF from the input, skip this check:
if (!packet || !packet->data || packet->size == 0)
and send NULL till you receive no frames.– Gyan
Mar 22 at 7:29
Once you receive EOF from the input, skip this check:
if (!packet || !packet->data || packet->size == 0)
and send NULL till you receive no frames.– Gyan
Mar 22 at 7:29
int ret = avcodec_receive_frame(codecCtx, frame); if (ret == AVERROR_EOF)
like this? not working– Jinx
Mar 22 at 8:04
int ret = avcodec_receive_frame(codecCtx, frame); if (ret == AVERROR_EOF)
like this? not working– Jinx
Mar 22 at 8:04
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55292212%2fffmpeg-avcodec-receive-frame-doesnt-receive-all-frames-and-lose-frames-before%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown