Skip to content

Commit 45ccc78

Browse files
Fix crashes
1 parent c0f29c0 commit 45ccc78

File tree

3 files changed

+50
-33
lines changed

3 files changed

+50
-33
lines changed

src/zm_event.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ mJpegCodecContext->bit_rate = 2000000;
221221

222222
mJpegSwsContext = sws_getContext(
223223
image->Width(), image->Height(), image->AVPixFormat(),
224-
mJpegCodecContext->width, mJpegCodecContext->height, AV_PIX_FMT_YUV420P,
224+
mJpegCodecContext->width, mJpegCodecContext->height, AV_PIX_FMT_YUVJ420P,
225225
SWS_BICUBIC, nullptr, nullptr, nullptr);
226226
if (!mJpegSwsContext) {
227227
return -1;

src/zm_ffmpeg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static CodecData enc_codecs[] = {
5858
{ AV_CODEC_ID_H265, "h265", "h265_ni_quadra_enc", AV_PIX_FMT_YUV420P, AV_PIX_FMT_NI_QUAD, AV_HWDEVICE_TYPE_NI_QUADRA, "-1" },
5959
{ AV_CODEC_ID_H264, "h264", "h264_ni_quadra_enc", AV_PIX_FMT_YUV420P, AV_PIX_FMT_NI_QUAD, AV_HWDEVICE_TYPE_NI_QUADRA, "-1" },
6060
{ AV_CODEC_ID_AV1, "av1", "av1_ni_quadra_enc", AV_PIX_FMT_YUV420P, AV_PIX_FMT_NI_QUAD, AV_HWDEVICE_TYPE_NI_QUADRA, "-1" },
61-
//{ AV_CODEC_ID_MJPEG, "mjpeg", "jpeg_ni_quadra_enc", AV_PIX_FMT_YUV420P, AV_PIX_FMT_NI_QUAD, AV_HWDEVICE_TYPE_NI_QUADRA, "-1" },
61+
{ AV_CODEC_ID_MJPEG, "mjpeg", "jpeg_ni_quadra_enc", AV_PIX_FMT_YUV420P, AV_PIX_FMT_NI_QUAD, AV_HWDEVICE_TYPE_NI_QUADRA, "-1" },
6262
//#endif
6363
{ AV_CODEC_ID_H265, "h265", "hevc_vaapi", AV_PIX_FMT_NV12, AV_PIX_FMT_VAAPI, AV_HWDEVICE_TYPE_VAAPI, nullptr },
6464
{ AV_CODEC_ID_H265, "h265", "hevc_qsv", AV_PIX_FMT_YUV420P, AV_PIX_FMT_QSV, AV_HWDEVICE_TYPE_QSV, nullptr },

src/zm_monitor.cpp

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,9 +1134,12 @@ bool Monitor::connect() {
11341134
analysis_image_pixelformats = (AVPixelFormat *)(image_pixelformats + (image_buffer_count*sizeof(AVPixelFormat)));
11351135

11361136
for (int32_t i = 0; i < image_buffer_count; i++) {
1137-
image_buffer[i] = new Image(width, height, camera->Colours(), camera->SubpixelOrder(), &(shared_images[i*image_size]));
1137+
image_buffer[i] = new Image(width, height, ZM_COLOUR_YUV420P, ZM_SUBPIX_ORDER_YUV420P, &(shared_images[i*image_size]));
1138+
//image_buffer[i] = new Image(width, height, camera->Colours(), camera->SubpixelOrder(), &(shared_images[i*image_size]));
11381139
image_buffer[i]->HoldBuffer(true); /* Don't release the internal buffer or replace it with another */
1139-
analysis_image_buffer[i] = new Image(width, height, camera->Colours(), camera->SubpixelOrder(), &(shared_analysis_images[i*image_size]));
1140+
image_pixelformats[i] = AV_PIX_FMT_NONE;
1141+
analysis_image_buffer[i] = new Image(width, height, ZM_COLOUR_YUV420P, ZM_SUBPIX_ORDER_YUV420P, &(shared_analysis_images[i*image_size]));
1142+
analysis_image_pixelformats[i] = AV_PIX_FMT_NONE;
11401143
analysis_image_buffer[i]->HoldBuffer(true); /* Don't release the internal buffer or replace it with another */
11411144
analysis_image_pixelformats[i] = AV_PIX_FMT_NONE;
11421145
}
@@ -2150,10 +2153,12 @@ bool Monitor::Analyse() {
21502153
}
21512154
Debug(1, "Quadra setting up on %d", deviceid);
21522155
if (!quadra_yolo->setup(camera->getVideoStream(),
2153-
camera->getVideoCodecContext(), "yolov5", "/usr/share/zoneminder/network_binary_yolov5s_improved.nb", deviceid)) {
2156+
mVideoCodecContext, "yolov5", "/usr/share/zoneminder/network_binary_yolov5s_improved.nb", deviceid)) {
21542157
delete quadra_yolo;
21552158
quadra_yolo = nullptr;
21562159
}
2160+
} else {
2161+
Debug(1, "Have quadra %p and hw_frame%d", quadra_yolo, packet->hw_frame.get());
21572162
}
21582163
}
21592164

@@ -2547,29 +2552,29 @@ bool Monitor::Analyse() {
25472552
shared_data->last_read_index = packet->image_index;
25482553
shared_data->analysis_image_count++;
25492554

2550-
unsigned int index = shared_data->last_analysis_index;
2551-
index++;
2552-
index = index % image_buffer_count;
2553-
if (packet->ai_frame) {
2554-
analysis_image_buffer[index]->AVPixFormat(static_cast<AVPixelFormat>(packet->ai_frame->format));
2555-
Debug(1, "ai_frame pixformat %d, for index %d", packet->ai_frame->format, index);
2556-
analysis_image_buffer[index]->Assign(packet->ai_frame.get());
2557-
analysis_image_pixelformats[index] = static_cast<AVPixelFormat>(packet->ai_frame->format);
2558-
shared_data->last_analysis_index = index;
2559-
} else if (packet->analysis_image) {
2560-
analysis_image_buffer[index]->Assign(*packet->analysis_image);
2561-
analysis_image_pixelformats[index] = packet->analysis_image->AVPixFormat();
2562-
Debug(1, "analysis %d, for index %d", analysis_image_pixelformats[index], index);
2563-
shared_data->last_analysis_index = index;
2564-
} else if (packet->image) {
2565-
analysis_image_buffer[index]->Assign(*packet->image);
2566-
analysis_image_pixelformats[index] = packet->image->AVPixFormat();
2567-
shared_data->last_analysis_index = index;
2568-
Debug(1, "image %d, for index %d", analysis_image_pixelformats[index], index);
2569-
} else {
2570-
Debug(1, "Unable to find an image to assign");
2571-
}
2572-
shared_analysis_timestamps[index] = zm::chrono::duration_cast<timeval>(packet->timestamp.time_since_epoch());
2555+
unsigned int index = shared_data->last_analysis_index;
2556+
index++;
2557+
index = index % image_buffer_count;
2558+
if (packet->ai_frame) {
2559+
analysis_image_buffer[index]->AVPixFormat(static_cast<AVPixelFormat>(packet->ai_frame->format));
2560+
Debug(1, "ai_frame pixformat %d, for index %d", packet->ai_frame->format, index);
2561+
analysis_image_buffer[index]->Assign(packet->ai_frame.get());
2562+
analysis_image_pixelformats[index] = static_cast<AVPixelFormat>(packet->ai_frame->format);
2563+
shared_data->last_analysis_index = index;
2564+
} else if (packet->analysis_image) {
2565+
analysis_image_buffer[index]->Assign(*packet->analysis_image);
2566+
analysis_image_pixelformats[index] = packet->analysis_image->AVPixFormat();
2567+
Debug(1, "analysis %d, for index %d", analysis_image_pixelformats[index], index);
2568+
shared_data->last_analysis_index = index;
2569+
} else if (packet->image) {
2570+
analysis_image_buffer[index]->Assign(*packet->image);
2571+
analysis_image_pixelformats[index] = packet->image->AVPixFormat();
2572+
shared_data->last_analysis_index = index;
2573+
Debug(1, "image %d, for index %d", analysis_image_pixelformats[index], index);
2574+
} else {
2575+
Debug(1, "Unable to find an image to assign");
2576+
}
2577+
shared_analysis_timestamps[index] = zm::chrono::duration_cast<timeval>(packet->timestamp.time_since_epoch());
25732578
} else {
25742579
Debug(3, "Not video, not clearing packets");
25752580
}
@@ -2872,6 +2877,7 @@ int Monitor::Capture() {
28722877

28732878
bool Monitor::setupConvertContext(const AVFrame *input_frame, const Image *image) {
28742879
AVPixelFormat imagePixFormat = image->AVPixFormat();
2880+
imagePixFormat = AV_PIX_FMT_YUV420P;
28752881
AVPixelFormat inputPixFormat;
28762882
bool changeColorspaceDetails = false;
28772883
switch (input_frame->format) {
@@ -2893,7 +2899,6 @@ bool Monitor::setupConvertContext(const AVFrame *input_frame, const Image *image
28932899
break;
28942900
default:
28952901
inputPixFormat = (AVPixelFormat)input_frame->format;
2896-
imagePixFormat = AV_PIX_FMT_YUV420P;
28972902
}
28982903

28992904
convert_context = sws_getContext(
@@ -2989,6 +2994,12 @@ bool Monitor::Decode() {
29892994
mVideoCodecContext->flags2 |= CODEC_FLAG2_FAST | CODEC_FLAG_LOW_DELAY;
29902995
#endif
29912996

2997+
if (!options.empty()) {
2998+
av_dict_parse_string(&opts, options.c_str(), "=", ",", 0);
2999+
// reorder_queparse for avforpts, mOpcodec
3000+
av_dict_set(&opts, "reorder_queue_size", nullptr, AV_DICT_MATCH_CASE);
3001+
av_dict_set(&opts, "probesize", nullptr, AV_DICT_MATCH_CASE);
3002+
}
29923003
av_opt_set(mVideoCodecContext->priv_data, "dec", (decoder_hwaccel_device != "" ? decoder_hwaccel_device.c_str() : "-1"), 0);
29933004

29943005
int ret = avcodec_open2(mVideoCodecContext, mVideoCodec, &opts);
@@ -3052,8 +3063,11 @@ bool Monitor::Decode() {
30523063
if (packet->in_frame and !packet->image) {
30533064
const AVFrame *in_frame = packet->in_frame.get();
30543065

3055-
unsigned int subpix = packet->in_frame->format == AV_PIX_FMT_YUV420P ? ZM_SUBPIX_ORDER_YUV420P : camera->SubpixelOrder();
3056-
unsigned int colours = packet->in_frame->format == AV_PIX_FMT_YUV420P ? ZM_COLOUR_YUV420P : camera->Colours();
3066+
if (0) {
3067+
//unsigned int subpix = packet->in_frame->format == AV_PIX_FMT_YUV420P ? ZM_SUBPIX_ORDER_YUV420P : camera->SubpixelOrder();
3068+
unsigned int subpix = ZM_SUBPIX_ORDER_YUV420P;
3069+
unsigned int colours = AV_PIX_FMT_YUV420P;
3070+
//unsigned int colours = packet->in_frame->format == AV_PIX_FMT_YUV420P ? ZM_COLOUR_YUV420P : camera->Colours();
30573071
packet->image = new Image(camera_width, camera_height, colours, subpix);
30583072
//packet->image = new Image(camera_width, camera_height, camera->Colours(), camera->SubpixelOrder());
30593073

@@ -3068,6 +3082,7 @@ bool Monitor::Decode() {
30683082
delete packet->image;
30693083
packet->image = nullptr;
30703084
} // end if have convert_context
3085+
}
30713086
} // end if need transfer to image
30723087
} else if (ret <0) {
30733088
avcodec_free_context(&mVideoCodecContext);
@@ -3184,8 +3199,10 @@ bool Monitor::Decode() {
31843199
index++;
31853200
index = index % image_buffer_count;
31863201
decoding_image_count++;
3202+
if (packet->image) {
31873203
image_buffer[index]->AVPixFormat(image_pixelformats[index] = packet->image->AVPixFormat());
31883204
image_buffer[index]->Assign(*(packet->image));
3205+
}
31893206
shared_timestamps[index] = zm::chrono::duration_cast<timeval>(packet->timestamp.time_since_epoch());
31903207
shared_data->signal = (capture_image and signal_check_points) ? CheckSignal(capture_image) : true;
31913208
shared_data->last_write_index = index;
@@ -3823,8 +3840,8 @@ void Monitor::get_ref_image() {
38233840
packet->packet->stream_index, video_stream_id, packet->image_index, packet->image );
38243841
// Might not have been decoded yet FIXME
38253842
if (packet->image) {
3826-
ref_image.Assign(width, height, camera->Colours(),
3827-
camera->SubpixelOrder(), packet->image->Buffer(), camera->ImageSize());
3843+
ref_image.Assign(width, height, packet->image->Colours(),
3844+
packet->image->SubpixelOrder(), packet->image->Buffer(), camera->ImageSize());
38283845
Debug(2, "Have ref image about to unlock");
38293846
} else {
38303847
Debug(2, "Have no ref image about to unlock");

0 commit comments

Comments
 (0)