Skip to content

Commit f3d9837

Browse files
Add looping when sending_frame
1 parent 99396e7 commit f3d9837

File tree

1 file changed

+49
-20
lines changed

1 file changed

+49
-20
lines changed

src/zm_videostore.cpp

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "zm_logger.h"
2424
#include "zm_monitor.h"
25+
#include "zm_signal.h"
2526
#include "zm_time.h"
2627

2728
extern "C" {
@@ -303,6 +304,7 @@ bool VideoStore::open() {
303304
video_out_ctx->time_base = AV_TIME_BASE_Q;
304305
video_out_ctx->codec_id = chosen_codec_data->codec_id;
305306
video_out_ctx->pix_fmt = chosen_codec_data->hw_pix_fmt;
307+
video_out_ctx->sw_pix_fmt = chosen_codec_data->sw_pix_fmt;
306308
Debug(1, "Setting pix fmt to %d %s", chosen_codec_data->hw_pix_fmt, av_get_pix_fmt_name(chosen_codec_data->hw_pix_fmt));
307309
const AVDictionaryEntry *opts_level = av_dict_get(opts, "level", nullptr, AV_DICT_MATCH_CASE);
308310
if (opts_level) {
@@ -334,7 +336,7 @@ bool VideoStore::open() {
334336
* the motion of the chroma plane does not match the luma plane. */
335337
video_out_ctx->mb_decision = 2;
336338
}
337-
if (setup_hwaccel(video_out_ctx,
339+
if (0 and setup_hwaccel(video_out_ctx,
338340
chosen_codec_data, hw_device_ctx, monitor->EncoderHWAccelDevice(), monitor->Width(), monitor->Height())) {
339341
continue;
340342
}
@@ -1043,22 +1045,43 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> zm_packet)
10431045
if (zm_packet->image) {
10441046
Debug(2, "Have an image, convert it");
10451047
//Go straight to out frame
1046-
swscale.Convert(
1047-
zm_packet->image,
1048-
zm_packet->out_frame->buf[0]->data,
1049-
zm_packet->codec_imgsize,
1050-
zm_packet->image->AVPixFormat(),
1051-
chosen_codec_data->sw_pix_fmt,
1052-
video_out_ctx->width,
1053-
video_out_ctx->height
1054-
);
1048+
if (
1049+
zm_packet->image->Width() == video_out_ctx->width
1050+
and
1051+
zm_packet->image->Height() == video_out_ctx->height
1052+
and
1053+
zm_packet->image->AVPixFormat() == chosen_codec_data->sw_pix_fmt
1054+
) {
1055+
zm_packet->image->PopulateFrame(zm_packet->out_frame.get());
1056+
} else {
1057+
swscale.Convert(
1058+
zm_packet->image,
1059+
zm_packet->out_frame->buf[0]->data,
1060+
zm_packet->codec_imgsize,
1061+
zm_packet->image->AVPixFormat(),
1062+
chosen_codec_data->sw_pix_fmt,
1063+
video_out_ctx->width,
1064+
video_out_ctx->height
1065+
);
1066+
}
10551067
} else if (!zm_packet->in_frame) {
10561068
Debug(1, "Have neither in_frame or image in packet %d!",
10571069
zm_packet->image_index);
10581070
return 0;
10591071
} else {
1060-
// Have in_frame.... may need to convert it to out_frame
1061-
swscale.Convert(zm_packet->in_frame.get(), zm_packet->out_frame.get());
1072+
if (
1073+
zm_packet->in_frame->width == video_out_ctx->width
1074+
and
1075+
zm_packet->in_frame->height == video_out_ctx->height
1076+
and
1077+
static_cast<AVPixelFormat>(zm_packet->in_frame->format) == chosen_codec_data->sw_pix_fmt
1078+
) {
1079+
zm_packet->out_frame = std::move(zm_packet->in_frame);
1080+
zm_packet->in_frame = nullptr;
1081+
} else {
1082+
// Have in_frame.... may need to convert it to out_frame
1083+
swscale.Convert(zm_packet->in_frame.get(), zm_packet->out_frame.get());
1084+
}
10621085
} // end if no in_frame
10631086
} // end if no out_frame
10641087

@@ -1154,14 +1177,20 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> zm_packet)
11541177

11551178
// Some hwaccel codecs may get full if we only receive one pkt for one frame
11561179

1157-
//int ret = zm_send_frame_receive_packet(video_out_ctx, frame, *opkt);
1158-
int ret = avcodec_send_frame(video_out_ctx, frame);
1159-
if (ret < 0) {
1160-
Error("Could not send frame (error '%s')", av_make_error_string(ret).c_str());
1161-
return ret;
1162-
}
1163-
while (true) {
1164-
ret = avcodec_receive_packet(video_out_ctx, opkt.get());
1180+
do {
1181+
int ret = avcodec_send_frame(video_out_ctx, frame);
1182+
if (ret == AVERROR(EAGAIN)) {
1183+
continue;
1184+
}
1185+
if (ret < 0) {
1186+
Error("Could not send frame (error '%s')", av_make_error_string(ret).c_str());
1187+
return ret;
1188+
}
1189+
break;
1190+
} while(!zm_terminate);
1191+
1192+
while (!zm_terminate) {
1193+
int ret = avcodec_receive_packet(video_out_ctx, opkt.get());
11651194
if (ret < 0) {
11661195
if (ret != AVERROR(EAGAIN)) {
11671196
Error("Could not receive packet (error %d = %s)", ret, av_make_error_string(ret).c_str());

0 commit comments

Comments
 (0)