add SWSCale::GetBufferSize to reduce duplicated lines of code. Makes input buffer size not have to match the aligned size. Reading from doesn't generally cause a crash.

This commit is contained in:
Isaac Connor 2021-02-18 13:45:06 -05:00
parent 127e15ba3a
commit 6d8a7a7a4c
2 changed files with 12 additions and 13 deletions

View File

@ -191,21 +191,11 @@ int SWScale::Convert(
#endif #endif
/* Check the buffer sizes */ /* Check the buffer sizes */
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0) size_t insize = GetBufferSize(in_pf, width, height);
size_t insize = av_image_get_buffer_size(in_pf, width, height, 32);
#else
size_t insize = avpicture_get_size(in_pf, width, height);
#endif
if ( insize != in_buffer_size ) { if ( insize != in_buffer_size ) {
Error("The input buffer size does not match the expected size for the input format. Required: %d Available: %d", insize, in_buffer_size); Debug(1, "The input buffer size does not match the expected size for the input format. Required: %d Available: %d", insize, in_buffer_size);
return -4;
} }
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0) size_t outsize = GetBufferSize(out_pf, new_width, new_height);
size_t outsize = av_image_get_buffer_size(out_pf, new_width, new_height, 32);
#else
size_t outsize = avpicture_get_size(out_pf, new_width, new_height);
#endif
if ( outsize < out_buffer_size ) { if ( outsize < out_buffer_size ) {
Error("The output buffer is undersized for the output format. Required: %d Available: %d", outsize, out_buffer_size); Error("The output buffer is undersized for the output format. Required: %d Available: %d", outsize, out_buffer_size);
return -5; return -5;
@ -305,4 +295,12 @@ int SWScale::ConvertDefaults(const uint8_t* in_buffer, const size_t in_buffer_si
return Convert(in_buffer,in_buffer_size,out_buffer,out_buffer_size,default_input_pf,default_output_pf,default_width,default_height); return Convert(in_buffer,in_buffer_size,out_buffer,out_buffer_size,default_input_pf,default_output_pf,default_width,default_height);
} }
size_t SWScale::GetBufferSize(enum _AVPIXELFORMAT pf, unsigned int width, unsigned int height) {
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
return av_image_get_buffer_size(pf, width, height, 32);
#else
return outsize = avpicture_get_size(pf, width,height);
#endif
}
#endif // HAVE_LIBSWSCALE && HAVE_LIBAVUTIL #endif // HAVE_LIBSWSCALE && HAVE_LIBAVUTIL

View File

@ -20,6 +20,7 @@ class SWScale {
int Convert(const Image* img, uint8_t* out_buffer, const size_t out_buffer_size, enum _AVPIXELFORMAT in_pf, enum _AVPIXELFORMAT out_pf, unsigned int width, unsigned int height); int Convert(const Image* img, uint8_t* out_buffer, const size_t out_buffer_size, enum _AVPIXELFORMAT in_pf, enum _AVPIXELFORMAT out_pf, unsigned int width, unsigned int height);
int Convert(const uint8_t* in_buffer, const size_t in_buffer_size, uint8_t* out_buffer, const size_t out_buffer_size, enum _AVPIXELFORMAT in_pf, enum _AVPIXELFORMAT out_pf, unsigned int width, unsigned int height); int Convert(const uint8_t* in_buffer, const size_t in_buffer_size, uint8_t* out_buffer, const size_t out_buffer_size, enum _AVPIXELFORMAT in_pf, enum _AVPIXELFORMAT out_pf, unsigned int width, unsigned int height);
int Convert(const uint8_t* in_buffer, const size_t in_buffer_size, uint8_t* out_buffer, const size_t out_buffer_size, enum _AVPIXELFORMAT in_pf, enum _AVPIXELFORMAT out_pf, unsigned int width, unsigned int height, unsigned int new_width, unsigned int new_height); int Convert(const uint8_t* in_buffer, const size_t in_buffer_size, uint8_t* out_buffer, const size_t out_buffer_size, enum _AVPIXELFORMAT in_pf, enum _AVPIXELFORMAT out_pf, unsigned int width, unsigned int height, unsigned int new_width, unsigned int new_height);
static size_t GetBufferSize(enum _AVPIXELFORMAT in_pf, unsigned int width, unsigned int height);
protected: protected:
bool gotdefaults; bool gotdefaults;