2008-07-16 16:35:59 +08:00
|
|
|
/*
|
2008-07-25 17:08:15 +08:00
|
|
|
* ZoneMinder FFMPEG implementation, $Date$, $Revision$
|
2008-07-25 17:33:23 +08:00
|
|
|
* Copyright (C) 2001-2008 Philip Coombes
|
2008-07-16 16:35:59 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2016-12-26 23:23:16 +08:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2008-07-16 16:35:59 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "zm_ffmpeg.h"
|
2013-11-26 22:34:26 +08:00
|
|
|
#include "zm_image.h"
|
|
|
|
#include "zm_rgb.h"
|
|
|
|
|
|
|
|
#if HAVE_LIBAVCODEC || HAVE_LIBAVUTIL || HAVE_LIBSWSCALE
|
|
|
|
|
|
|
|
#if HAVE_LIBAVUTIL
|
2015-11-04 09:46:26 +08:00
|
|
|
enum _AVPIXELFORMAT GetFFMPEGPixelFormat(unsigned int p_colours, unsigned p_subpixelorder) {
|
2016-04-04 22:11:48 +08:00
|
|
|
enum _AVPIXELFORMAT pf;
|
|
|
|
|
|
|
|
Debug(8,"Colours: %d SubpixelOrder: %d",p_colours,p_subpixelorder);
|
|
|
|
|
|
|
|
switch(p_colours) {
|
|
|
|
case ZM_COLOUR_RGB24:
|
|
|
|
{
|
|
|
|
if(p_subpixelorder == ZM_SUBPIX_ORDER_BGR) {
|
|
|
|
/* BGR subpixel order */
|
|
|
|
pf = AV_PIX_FMT_BGR24;
|
|
|
|
} else {
|
|
|
|
/* Assume RGB subpixel order */
|
|
|
|
pf = AV_PIX_FMT_RGB24;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ZM_COLOUR_RGB32:
|
|
|
|
{
|
|
|
|
if(p_subpixelorder == ZM_SUBPIX_ORDER_ARGB) {
|
|
|
|
/* ARGB subpixel order */
|
|
|
|
pf = AV_PIX_FMT_ARGB;
|
|
|
|
} else if(p_subpixelorder == ZM_SUBPIX_ORDER_ABGR) {
|
|
|
|
/* ABGR subpixel order */
|
|
|
|
pf = AV_PIX_FMT_ABGR;
|
|
|
|
} else if(p_subpixelorder == ZM_SUBPIX_ORDER_BGRA) {
|
|
|
|
/* BGRA subpixel order */
|
|
|
|
pf = AV_PIX_FMT_BGRA;
|
|
|
|
} else {
|
|
|
|
/* Assume RGBA subpixel order */
|
|
|
|
pf = AV_PIX_FMT_RGBA;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ZM_COLOUR_GRAY8:
|
|
|
|
pf = AV_PIX_FMT_GRAY8;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Panic("Unexpected colours: %d",p_colours);
|
|
|
|
pf = AV_PIX_FMT_GRAY8; /* Just to shush gcc variable may be unused warning */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pf;
|
2013-11-26 22:34:26 +08:00
|
|
|
}
|
2016-08-07 22:55:19 +08:00
|
|
|
/* The following is copied directly from newer ffmpeg. */
|
2016-08-10 01:14:10 +08:00
|
|
|
#if LIBAVUTIL_VERSION_CHECK(52, 7, 0, 17, 100)
|
2016-08-07 22:55:19 +08:00
|
|
|
#else
|
|
|
|
static int parse_key_value_pair(AVDictionary **pm, const char **buf,
|
|
|
|
const char *key_val_sep, const char *pairs_sep,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
char *key = av_get_token(buf, key_val_sep);
|
|
|
|
char *val = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (key && *key && strspn(*buf, key_val_sep)) {
|
|
|
|
(*buf)++;
|
|
|
|
val = av_get_token(buf, pairs_sep);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key && *key && val && *val)
|
|
|
|
ret = av_dict_set(pm, key, val, flags);
|
|
|
|
else
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
|
|
|
|
av_freep(&key);
|
|
|
|
av_freep(&val);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
int av_dict_parse_string(AVDictionary **pm, const char *str,
|
|
|
|
const char *key_val_sep, const char *pairs_sep,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* ignore STRDUP flags */
|
|
|
|
flags &= ~(AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
|
|
|
|
|
|
|
|
while (*str) {
|
|
|
|
if ((ret = parse_key_value_pair(pm, &str, key_val_sep, pairs_sep, flags)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (*str)
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2013-11-26 22:34:26 +08:00
|
|
|
#endif // HAVE_LIBAVUTIL
|
|
|
|
|
|
|
|
#if HAVE_LIBSWSCALE && HAVE_LIBAVUTIL
|
|
|
|
SWScale::SWScale() : gotdefaults(false), swscale_ctx(NULL), input_avframe(NULL), output_avframe(NULL) {
|
2016-04-04 22:11:48 +08:00
|
|
|
Debug(4,"SWScale object created");
|
2013-11-26 22:34:26 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
/* Allocate AVFrame for the input */
|
2015-05-29 23:38:02 +08:00
|
|
|
#if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101)
|
2016-04-04 22:11:48 +08:00
|
|
|
input_avframe = av_frame_alloc();
|
2015-05-29 23:38:02 +08:00
|
|
|
#else
|
2016-04-04 22:11:48 +08:00
|
|
|
input_avframe = avcodec_alloc_frame();
|
2015-05-29 23:38:02 +08:00
|
|
|
#endif
|
2016-04-04 22:11:48 +08:00
|
|
|
if(input_avframe == NULL) {
|
|
|
|
Fatal("Failed allocating AVFrame for the input");
|
|
|
|
}
|
2013-11-26 22:34:26 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
/* Allocate AVFrame for the output */
|
2015-05-29 23:38:02 +08:00
|
|
|
#if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101)
|
2016-04-04 22:11:48 +08:00
|
|
|
output_avframe = av_frame_alloc();
|
2015-05-29 23:38:02 +08:00
|
|
|
#else
|
2016-04-04 22:11:48 +08:00
|
|
|
output_avframe = avcodec_alloc_frame();
|
2015-05-29 23:38:02 +08:00
|
|
|
#endif
|
2016-04-04 22:11:48 +08:00
|
|
|
if(output_avframe == NULL) {
|
|
|
|
Fatal("Failed allocating AVFrame for the output");
|
|
|
|
}
|
2013-11-26 22:34:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SWScale::~SWScale() {
|
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
/* Free up everything */
|
2015-11-04 12:30:14 +08:00
|
|
|
#if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101)
|
2016-04-04 22:11:48 +08:00
|
|
|
av_frame_free( &input_avframe );
|
2015-11-04 12:30:14 +08:00
|
|
|
#else
|
2016-04-04 22:11:48 +08:00
|
|
|
av_freep( &input_avframe );
|
2015-11-04 12:30:14 +08:00
|
|
|
#endif
|
2016-04-04 22:11:48 +08:00
|
|
|
//input_avframe = NULL;
|
2013-11-26 22:34:26 +08:00
|
|
|
|
2015-11-04 12:30:14 +08:00
|
|
|
#if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101)
|
2016-04-04 22:11:48 +08:00
|
|
|
av_frame_free( &output_avframe );
|
2015-11-04 12:30:14 +08:00
|
|
|
#else
|
2016-04-04 22:11:48 +08:00
|
|
|
av_freep( &output_avframe );
|
2015-11-04 12:30:14 +08:00
|
|
|
#endif
|
2016-04-04 22:11:48 +08:00
|
|
|
//output_avframe = NULL;
|
|
|
|
|
|
|
|
if(swscale_ctx) {
|
|
|
|
sws_freeContext(swscale_ctx);
|
|
|
|
swscale_ctx = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Debug(4,"SWScale object destroyed");
|
2013-11-26 22:34:26 +08:00
|
|
|
}
|
|
|
|
|
2015-11-04 09:46:26 +08:00
|
|
|
int SWScale::SetDefaults(enum _AVPIXELFORMAT in_pf, enum _AVPIXELFORMAT out_pf, unsigned int width, unsigned int height) {
|
2013-11-26 22:34:26 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
/* Assign the defaults */
|
|
|
|
default_input_pf = in_pf;
|
|
|
|
default_output_pf = out_pf;
|
|
|
|
default_width = width;
|
|
|
|
default_height = height;
|
2013-11-26 22:34:26 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
gotdefaults = true;
|
2013-11-26 22:34:26 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
return 0;
|
2013-11-26 22:34:26 +08:00
|
|
|
}
|
|
|
|
|
2015-11-04 09:46:26 +08:00
|
|
|
int SWScale::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) {
|
2016-04-04 22:11:48 +08:00
|
|
|
/* Parameter checking */
|
|
|
|
if(in_buffer == NULL || out_buffer == NULL) {
|
|
|
|
Error("NULL Input or output buffer");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(in_pf == 0 || out_pf == 0) {
|
|
|
|
Error("Invalid input or output pixel formats");
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
if(!width || !height) {
|
|
|
|
Error("Invalid width or height");
|
|
|
|
return -3;
|
|
|
|
}
|
2013-11-26 22:34:26 +08:00
|
|
|
|
2015-05-29 23:38:02 +08:00
|
|
|
#if LIBSWSCALE_VERSION_CHECK(0, 8, 0, 8, 0)
|
2016-04-04 22:11:48 +08:00
|
|
|
/* Warn if the input or output pixelformat is not supported */
|
|
|
|
if(!sws_isSupportedInput(in_pf)) {
|
|
|
|
Warning("swscale does not support the input format: %c%c%c%c",(in_pf)&0xff,((in_pf)&0xff),((in_pf>>16)&0xff),((in_pf>>24)&0xff));
|
|
|
|
}
|
|
|
|
if(!sws_isSupportedOutput(out_pf)) {
|
|
|
|
Warning("swscale does not support the output format: %c%c%c%c",(out_pf)&0xff,((out_pf>>8)&0xff),((out_pf>>16)&0xff),((out_pf>>24)&0xff));
|
|
|
|
}
|
2014-04-26 04:12:58 +08:00
|
|
|
#endif
|
2013-11-26 22:34:26 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
/* Check the buffer sizes */
|
2016-03-02 22:03:55 +08:00
|
|
|
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
|
2016-04-29 19:27:28 +08:00
|
|
|
size_t insize = av_image_get_buffer_size(in_pf, width, height,1);
|
2016-03-02 22:03:55 +08:00
|
|
|
#else
|
2016-04-04 22:11:48 +08:00
|
|
|
size_t insize = avpicture_get_size(in_pf, width, height);
|
2016-03-02 22:03:55 +08:00
|
|
|
#endif
|
2016-04-04 22:11:48 +08:00
|
|
|
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);
|
|
|
|
return -4;
|
|
|
|
}
|
2016-03-02 22:03:55 +08:00
|
|
|
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
|
2016-04-29 19:27:28 +08:00
|
|
|
size_t outsize = av_image_get_buffer_size(out_pf, width, height,1);
|
2016-03-02 22:03:55 +08:00
|
|
|
#else
|
2016-04-04 22:11:48 +08:00
|
|
|
size_t outsize = avpicture_get_size(out_pf, width, height);
|
2016-03-02 22:03:55 +08:00
|
|
|
#endif
|
2016-04-04 22:11:48 +08:00
|
|
|
if(outsize < out_buffer_size) {
|
|
|
|
Error("The output buffer is undersized for the output format. Required: %d Available: %d", outsize, out_buffer_size);
|
|
|
|
return -5;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the context */
|
|
|
|
swscale_ctx = sws_getCachedContext( NULL, width, height, in_pf, width, height, out_pf, 0, NULL, NULL, NULL );
|
|
|
|
if(swscale_ctx == NULL) {
|
|
|
|
Error("Failed getting swscale context");
|
|
|
|
return -6;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill in the buffers */
|
|
|
|
if(!avpicture_fill( (AVPicture*)input_avframe, (uint8_t*)in_buffer, in_pf, width, height ) ) {
|
|
|
|
Error("Failed filling input frame with input buffer");
|
|
|
|
return -7;
|
|
|
|
}
|
|
|
|
if(!avpicture_fill( (AVPicture*)output_avframe, out_buffer, out_pf, width, height ) ) {
|
|
|
|
Error("Failed filling output frame with output buffer");
|
|
|
|
return -8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the conversion */
|
|
|
|
if(!sws_scale(swscale_ctx, input_avframe->data, input_avframe->linesize, 0, height, output_avframe->data, output_avframe->linesize ) ) {
|
|
|
|
Error("swscale conversion failed");
|
|
|
|
return -10;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2013-11-26 22:34:26 +08:00
|
|
|
}
|
|
|
|
|
2015-11-04 09:46:26 +08:00
|
|
|
int SWScale::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) {
|
2016-04-04 22:11:48 +08:00
|
|
|
if(img->Width() != width) {
|
|
|
|
Error("Source image width differs. Source: %d Output: %d",img->Width(), width);
|
|
|
|
return -12;
|
|
|
|
}
|
2013-11-26 22:34:26 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
if(img->Height() != height) {
|
|
|
|
Error("Source image height differs. Source: %d Output: %d",img->Height(), height);
|
|
|
|
return -13;
|
|
|
|
}
|
2013-11-26 22:34:26 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
return Convert(img->Buffer(),img->Size(),out_buffer,out_buffer_size,in_pf,out_pf,width,height);
|
2013-11-26 22:34:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int SWScale::ConvertDefaults(const Image* img, uint8_t* out_buffer, const size_t out_buffer_size) {
|
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
if(!gotdefaults) {
|
|
|
|
Error("Defaults are not set");
|
|
|
|
return -24;
|
|
|
|
}
|
2013-11-26 22:34:26 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
return Convert(img,out_buffer,out_buffer_size,default_input_pf,default_output_pf,default_width,default_height);
|
2013-11-26 22:34:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int SWScale::ConvertDefaults(const uint8_t* in_buffer, const size_t in_buffer_size, uint8_t* out_buffer, const size_t out_buffer_size) {
|
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
if(!gotdefaults) {
|
|
|
|
Error("Defaults are not set");
|
|
|
|
return -24;
|
|
|
|
}
|
2013-11-26 22:34:26 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
return Convert(in_buffer,in_buffer_size,out_buffer,out_buffer_size,default_input_pf,default_output_pf,default_width,default_height);
|
2013-11-26 22:34:26 +08:00
|
|
|
}
|
|
|
|
#endif // HAVE_LIBSWSCALE && HAVE_LIBAVUTIL
|
2008-07-16 16:35:59 +08:00
|
|
|
|
2016-08-07 22:55:19 +08:00
|
|
|
|
2013-11-26 22:34:26 +08:00
|
|
|
#endif // HAVE_LIBAVCODEC || HAVE_LIBAVUTIL || HAVE_LIBSWSCALE
|