Skip to content

Commit 7794689

Browse files
Merge branch 'yuv420' of github.com:ZoneMinder/zoneminder into webgeek1234-avcodec-jpeg
2 parents 1cbc1bd + 6054726 commit 7794689

File tree

5 files changed

+34
-37
lines changed

5 files changed

+34
-37
lines changed

src/zm_camera.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ Camera::Camera(
3939
type(p_type),
4040
width(p_width),
4141
height(p_height),
42-
colours(p_colours),
43-
subpixelorder(p_subpixelorder),
42+
colours(ZM_COLOUR_RGB24),
43+
subpixelorder(ZM_SUBPIX_ORDER_YUV420P),
4444
brightness(p_brightness),
4545
hue(p_hue),
4646
colour(p_colour),
@@ -61,9 +61,9 @@ Camera::Camera(
6161
mLastAudioPTS(0),
6262
bytes(0),
6363
mIsPrimed(false) {
64-
linesize = width * colours;
64+
linesize = FFALIGN(av_image_get_linesize(AV_PIX_FMT_YUVJ420P, width, 0), 32); // hardcoded hack
6565
pixels = width * height;
66-
imagesize = static_cast<unsigned long long>(height) * linesize;
66+
imagesize = av_image_get_buffer_size(AV_PIX_FMT_YUVJ420P, width, height, 32); // hardcoded hack
6767

6868
Debug(2, "New camera id: %d width: %d line size: %d height: %d colours: %d subpixelorder: %d capture: %d, size: %llu",
6969
monitor->Id(), width, linesize, height, colours, subpixelorder, capture, imagesize);

src/zm_event.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,8 @@ bool Event::WriteFrameImage(Image *image, SystemTimePoint timestamp, const char
304304
Image ts_image(*image);
305305
monitor->TimestampImage(&ts_image, timestamp);
306306
return ts_image.WriteJpeg(event_file, mJpegCodecContext, mJpegSwsContext);
307-
} else {
308-
return image->WriteJpeg(event_file, mJpegCodecContext, mJpegSwsContext);
309307
}
308+
return image->WriteJpeg(event_file, mJpegCodecContext, mJpegSwsContext);
310309
}
311310

312311
bool Event::WritePacket(const std::shared_ptr<ZMPacket>packet) {

src/zm_eventstream.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,6 @@ bool EventStream::sendFrame(Microseconds delta_us) {
894894
switch ( type ) {
895895
case STREAM_SINGLE :
896896
case STREAM_JPEG :
897-
898897
if ((!mJpegCodecContext) || (mJpegCodecContext->width != l_width || mJpegCodecContext->height != l_height)) {
899898
initContexts(l_width, l_height, config.jpeg_stream_quality);
900899
}

src/zm_image.cpp

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -174,23 +174,21 @@ Image::Image(int p_width, int p_height, int p_colours, int p_subpixelorder, uint
174174
if (!initialised)
175175
Initialise();
176176
pixels = width * height;
177-
linesize = p_width * p_colours;
178177

179178
if (!subpixelorder and (colours>1)) {
180179
// Default to RGBA when no subpixelorder is specified.
181180
subpixelorder = ZM_SUBPIX_ORDER_RGBA;
182181
}
183182

184183
imagePixFormat = AVPixFormat();
184+
linesize = FFALIGN(av_image_get_linesize(imagePixFormat, width, 0), 32);
185+
size = av_image_get_buffer_size(imagePixFormat, width, height, 32);
185186

186187
if (p_buffer) {
187-
size = linesize * height + padding;
188188
allocation = size;
189189
buffertype = ZM_BUFTYPE_DONTFREE;
190190
buffer = p_buffer;
191191
} else {
192-
size = av_image_get_buffer_size(imagePixFormat, width, height, 32);
193-
linesize = FFALIGN(av_image_get_linesize(imagePixFormat, width, 0), 32);
194192

195193
Debug(4, "line size: %d =? %d width %d Size %d ?= %d", linesize,
196194
av_image_get_linesize(imagePixFormat, width, 0),
@@ -233,10 +231,10 @@ Image::Image(int p_width, int p_linesize, int p_height, int p_colours, int p_sub
233231
}
234232

235233
Image::Image(const AVFrame *frame, int p_width, int p_height) :
236-
colours(ZM_COLOUR_RGB32),
234+
colours(ZM_COLOUR_RGB24),
237235
padding(0),
238-
subpixelorder(ZM_SUBPIX_ORDER_RGBA),
239-
imagePixFormat(AV_PIX_FMT_RGBA),
236+
subpixelorder(ZM_SUBPIX_ORDER_YUV420P),
237+
imagePixFormat(AV_PIX_FMT_YUVJ420P),
240238
buffer(0),
241239
holdbuffer(0) {
242240
width = (p_width == -1 ? frame->width : p_width);
@@ -247,9 +245,9 @@ Image::Image(const AVFrame *frame, int p_width, int p_height) :
247245
// FIXME
248246
//(AVPixelFormat)frame->format;
249247

250-
size = av_image_get_buffer_size(AV_PIX_FMT_RGBA, width, height, 32);
248+
size = av_image_get_buffer_size(AV_PIX_FMT_YUVJ420P, width, height, 32);
251249
// av_image_get_linesize isn't aligned, so we have to do that.
252-
linesize = FFALIGN(av_image_get_linesize(AV_PIX_FMT_RGBA, width, 0), 32);
250+
linesize = FFALIGN(av_image_get_linesize(AV_PIX_FMT_YUVJ420P, width, 0), 32);
253251

254252
AllocImgBuffer(size);
255253
this->Assign(frame);
@@ -676,7 +674,7 @@ void Image::AssignDirect(
676674
return;
677675
}
678676

679-
size_t new_buffer_size = static_cast<size_t>(p_width) * p_height * p_colours;
677+
size_t new_buffer_size = static_cast<size_t>(av_image_get_buffer_size(AV_PIX_FMT_YUVJ420P, width, height, 32)); // hardcoded hack
680678

681679
if ( buffer_size < new_buffer_size ) {
682680
Error("Attempt to directly assign buffer from an undersized buffer of size: %zu, needed %dx%d*%d colours = %zu",
@@ -707,8 +705,9 @@ void Image::AssignDirect(
707705
width = p_width;
708706
height = p_height;
709707
colours = p_colours;
710-
linesize = width * colours;
711708
subpixelorder = p_subpixelorder;
709+
imagePixFormat = AVPixFormat();
710+
linesize = FFALIGN(av_image_get_linesize(imagePixFormat, width, 0), 32);
712711
pixels = width * height;
713712
size = new_buffer_size;
714713
update_function_pointers();
@@ -727,7 +726,7 @@ void Image::Assign(
727726
return;
728727
}
729728

730-
unsigned int new_size = p_width * p_height * p_colours;
729+
unsigned int new_size = av_image_get_buffer_size(AV_PIX_FMT_YUVJ420P, width, height, 32); // hardcoded hack
731730
if ( buffer_size < new_size ) {
732731
Error("Attempt to assign buffer from an undersized buffer of size: %zu", buffer_size);
733732
return;
@@ -762,6 +761,8 @@ void Image::Assign(
762761
pixels = width*height;
763762
colours = p_colours;
764763
subpixelorder = p_subpixelorder;
764+
imagePixFormat = AVPixFormat();
765+
linesize = FFALIGN(av_image_get_linesize(imagePixFormat, width, 0), 32);
765766
size = new_size;
766767
}
767768

@@ -771,7 +772,7 @@ void Image::Assign(
771772
}
772773

773774
void Image::Assign(const Image &image) {
774-
unsigned int new_size = image.height * image.linesize;
775+
unsigned int new_size = av_image_get_buffer_size(AV_PIX_FMT_YUVJ420P, width, height, 32); // hardcoded hack
775776

776777
if ( image.buffer == nullptr ) {
777778
Error("Attempt to assign image with an empty buffer");
@@ -809,26 +810,14 @@ void Image::Assign(const Image &image) {
809810
pixels = width*height;
810811
colours = image.colours;
811812
subpixelorder = image.subpixelorder;
813+
imagePixFormat = image.imagePixFormat;
812814
size = new_size;
813815
linesize = image.linesize;
814816
update_function_pointers();
815817
}
816818

817-
if ( image.buffer != buffer ) {
818-
if (image.linesize > linesize) {
819-
Debug(1, "Must copy line by line due to different line size %d != %d", image.linesize, linesize);
820-
uint8_t *src_ptr = image.buffer;
821-
uint8_t *dst_ptr = buffer;
822-
for (unsigned int i=0; i< image.height; i++) {
823-
(*fptr_imgbufcpy)(dst_ptr, src_ptr, image.linesize);
824-
src_ptr += image.linesize;
825-
dst_ptr += linesize;
826-
}
827-
} else {
828-
Debug(4, "Doing full copy line size %d != %d", image.linesize, linesize);
829-
(*fptr_imgbufcpy)(buffer, image.buffer, size);
830-
}
831-
}
819+
if ( image.buffer != buffer )
820+
(*fptr_imgbufcpy)(buffer, image.buffer, size);
832821
}
833822

834823
Image *Image::HighlightEdges(
@@ -1639,6 +1628,12 @@ bool Image::EncodeJpeg(JOCTET *outbuffer, int *outbuffer_size, AVCodecContext *p
16391628
PopulateFrame(frame.get());
16401629
}
16411630

1631+
if (frame.get()->format != AV_PIX_FMT_YUV420P) {
1632+
Error("Jpeg frame format incorrect, got %d", frame.get()->format);
1633+
av_frame_unref(frame.get());
1634+
return false;
1635+
}
1636+
16421637
pkt = av_packet_alloc();
16431638

16441639
avcodec_send_frame(p_jpegcodeccontext, frame.get());
@@ -5460,7 +5455,9 @@ __attribute__((noinline)) void std_deinterlace_4field_abgr(uint8_t* col1, uint8_
54605455
}
54615456

54625457
AVPixelFormat Image::AVPixFormat() const {
5463-
if ( colours == ZM_COLOUR_RGB32 ) {
5458+
if ( subpixelorder == ZM_SUBPIX_ORDER_YUV420P) {
5459+
return AV_PIX_FMT_YUV420P;
5460+
} else if ( colours == ZM_COLOUR_RGB32 ) {
54645461
return AV_PIX_FMT_RGBA;
54655462
} else if ( colours == ZM_COLOUR_RGB24 ) {
54665463
if ( subpixelorder == ZM_SUBPIX_ORDER_BGR) {

src/zm_monitor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2783,7 +2783,9 @@ bool Monitor::Decode() {
27832783
int ret = packet->decode(camera->getVideoCodecContext());
27842784
if (ret > 0 and !zm_terminate) {
27852785
if (packet->in_frame and !packet->image) {
2786-
packet->image = new Image(camera_width, camera_height, camera->Colours(), camera->SubpixelOrder());
2786+
unsigned int subpix = packet->in_frame->format == AV_PIX_FMT_YUV420P ? ZM_SUBPIX_ORDER_YUV420P : camera->SubpixelOrder();
2787+
unsigned int colours = packet->in_frame->format == AV_PIX_FMT_YUV420P ? ZM_COLOUR_RGB24 : camera->Colours();
2788+
packet->image = new Image(camera_width, camera_height, colours, subpix);
27872789

27882790
if (convert_context || this->setupConvertContext(packet->in_frame.get(), packet->image)) {
27892791
if (!packet->image->Assign(packet->in_frame.get(), convert_context, dest_frame.get())) {

0 commit comments

Comments
 (0)