Skip to content

Commit a00ac82

Browse files
Move code around to be better.. Don't read_frame unless we actually need to send_packet
1 parent fce73cf commit a00ac82

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

src/zm_ffmpeg_input.cpp

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,13 @@ int FFmpeg_Input::Open(const char *filepath) {
9898

9999
streams[i].context = avcodec_alloc_context3(streams[i].codec);
100100
avcodec_parameters_to_context(streams[i].context, input_format_context->streams[i]->codecpar);
101+
101102
if (setup_hwaccel(streams[i].context,
102-
chosen_codec_data, hw_device_ctx, "", input_format_context->streams[i]->codecpar->width,
103+
chosen_codec_data, hw_device_ctx, "",
104+
input_format_context->streams[i]->codecpar->width,
103105
input_format_context->streams[i]->codecpar->height
104106
)) {
107+
Warning("Failed to setup hw_accel");
105108
continue;
106109
}
107110

@@ -111,14 +114,16 @@ int FFmpeg_Input::Open(const char *filepath) {
111114
if (error < 0) {
112115
Error("Could not open input codec (error '%s')",
113116
av_make_error_string(error).c_str());
114-
avcodec_free_context(&streams[i].context);
117+
118+
avcodec_free_context(&(streams[i].context));
115119
streams[i].context = nullptr;
116120
continue;
117121
}
118122
break;
119123
} // end foreach codec_data
120124

121125
if (!streams[i].context) {
126+
Debug(1, "Failed with known codecs, trying harder");
122127
if ((streams[i].codec = avcodec_find_decoder(input_format_context->streams[i]->codecpar->codec_id))) {
123128
Debug(1, "Using codec (%s) for stream %d", streams[i].codec->name, i);
124129
streams[i].context = avcodec_alloc_context3(streams[i].codec);
@@ -189,41 +194,18 @@ AVFrame *FFmpeg_Input::get_frame(int stream_id) {
189194
return nullptr;
190195
}
191196

192-
while (!frameComplete) {
193-
int ret = av_read_frame(input_format_context, packet.get());
194-
if (ret < 0) {
195-
if (
196-
// Check if EOF.
197-
(ret == AVERROR_EOF || (input_format_context->pb && input_format_context->pb->eof_reached)) ||
198-
// Check for Connection failure.
199-
(ret == -110)
200-
) {
201-
Info("av_read_frame returned %s.", av_make_error_string(ret).c_str());
202-
return nullptr;
203-
}
204-
Error("Unable to read packet from stream %d: error %d \"%s\".",
205-
packet->stream_index, ret, av_make_error_string(ret).c_str());
206-
return nullptr;
207-
}
208-
ZM_DUMP_STREAM_PACKET(input_format_context->streams[packet->stream_index], packet, "Received packet");
209-
210-
//av_packet_guard pkt_guard{packet};
211-
212-
if ((stream_id >= 0) && (packet->stream_index != stream_id)) {
213-
Debug(4, "Packet is not for our stream (%d)", packet->stream_index );
214-
continue;
215-
}
197+
AVCodecContext *context = streams[stream_id].context;
216198

217-
AVCodecContext *context = streams[packet->stream_index].context;
199+
while (!frameComplete) {
218200

219201
frame = av_frame_ptr{zm_av_frame_alloc()};
220202
if (!frame) {
221203
Error("Unable to allocate frame.");
222204
return nullptr;
223205
}
224-
206+
225207
// Since technically sending a packet can result in multiple frames (or buffered_frames) try receive_frame first.
226-
ret = avcodec_receive_frame(context, frame.get());
208+
int ret = avcodec_receive_frame(context, frame.get());
227209
Debug(1, "Ret from receive_frame ret: %d %s", ret, av_make_error_string(ret).c_str());
228210
if (ret == AVERROR(EAGAIN)) {
229211
// Perfectly normal
@@ -240,6 +222,30 @@ AVFrame *FFmpeg_Input::get_frame(int stream_id) {
240222
break;
241223
}
242224

225+
ret = av_read_frame(input_format_context, packet.get());
226+
if (ret < 0) {
227+
if (
228+
// Check if EOF.
229+
(ret == AVERROR_EOF || (input_format_context->pb && input_format_context->pb->eof_reached)) ||
230+
// Check for Connection failure.
231+
(ret == -110)
232+
) {
233+
Info("av_read_frame returned %s.", av_make_error_string(ret).c_str());
234+
return nullptr;
235+
}
236+
Error("Unable to read packet from stream %d: error %d \"%s\".",
237+
packet->stream_index, ret, av_make_error_string(ret).c_str());
238+
return nullptr;
239+
}
240+
ZM_DUMP_STREAM_PACKET(input_format_context->streams[packet->stream_index], packet, "Received packet");
241+
242+
//av_packet_guard pkt_guard{packet};
243+
244+
if ((stream_id >= 0) && (packet->stream_index != stream_id)) {
245+
Debug(4, "Packet is not for our stream (%d)", packet->stream_index );
246+
continue;
247+
}
248+
243249
ret = avcodec_send_packet(context, packet.get());
244250
if (ret == AVERROR(EAGAIN)) {
245251
Debug(2, "Unable to send packet %d %s", ret, av_make_error_string(ret).c_str());

0 commit comments

Comments
 (0)