@@ -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
235233Image::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
773774void 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
834823Image *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
54625457AVPixelFormat 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) {
0 commit comments