2004-03-04 15:05:54 +00:00
|
|
|
/*
|
|
|
|
* ZoneMinder MPEG class implementation, $Date$, $Revision$
|
2008-07-25 09:33:23 +00:00
|
|
|
* Copyright (C) 2001-2008 Philip Coombes
|
2022-01-10 15:13:53 +00:00
|
|
|
*
|
2004-03-04 15:05:54 +00: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.
|
2022-01-10 15:13:53 +00:00
|
|
|
*
|
2004-03-04 15:05:54 +00:00
|
|
|
* 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.
|
2022-01-10 15:13:53 +00:00
|
|
|
*
|
2004-03-04 15:05:54 +00:00
|
|
|
* 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 15:23:16 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2014-04-25 20:12:58 +00:00
|
|
|
*/
|
2004-03-04 15:05:54 +00:00
|
|
|
|
2021-02-04 03:47:28 +00:00
|
|
|
#include "zm_mpeg.h"
|
2004-03-04 15:05:54 +00:00
|
|
|
|
2021-02-04 03:47:28 +00:00
|
|
|
#include "zm_logger.h"
|
2011-06-22 15:38:35 +00:00
|
|
|
#include "zm_rgb.h"
|
2021-06-13 13:56:13 +00:00
|
|
|
#include "zm_time.h"
|
2013-03-16 23:45:21 +00:00
|
|
|
|
2004-03-04 15:05:54 +00:00
|
|
|
bool VideoStream::initialised = false;
|
|
|
|
|
2005-12-07 13:42:25 +00:00
|
|
|
VideoStream::MimeData VideoStream::mime_data[] = {
|
|
|
|
{ "asf", "video/x-ms-asf" },
|
|
|
|
{ "swf", "application/x-shockwave-flash" },
|
2010-02-28 19:16:40 +00:00
|
|
|
{ "flv", "video/x-flv" },
|
2014-04-25 20:12:58 +00:00
|
|
|
{ "mov", "video/quicktime" }
|
2005-12-07 13:42:25 +00:00
|
|
|
};
|
|
|
|
|
2017-01-11 19:18:11 +00:00
|
|
|
void VideoStream::Initialise( ) {
|
2020-08-05 23:17:00 +00:00
|
|
|
FFMPEGInit();
|
2004-03-04 15:05:54 +00:00
|
|
|
initialised = true;
|
|
|
|
}
|
|
|
|
|
2017-01-11 19:18:11 +00:00
|
|
|
void VideoStream::SetupFormat( ) {
|
2014-04-25 20:12:58 +00:00
|
|
|
/* allocate the output media context */
|
2020-08-25 23:45:48 +00:00
|
|
|
ofc = nullptr;
|
|
|
|
avformat_alloc_output_context2(&ofc, nullptr, format, filename);
|
2015-05-29 17:23:00 +00:00
|
|
|
|
2022-01-10 15:13:53 +00:00
|
|
|
if (!ofc) {
|
2021-04-28 22:13:16 +00:00
|
|
|
Fatal("avformat_alloc_..._context failed");
|
2004-03-04 15:05:54 +00:00
|
|
|
}
|
2014-04-25 20:12:58 +00:00
|
|
|
|
|
|
|
of = ofc->oformat;
|
2022-01-10 15:13:53 +00:00
|
|
|
Debug(1, "Using output format: %s (%s)", of->name, of->long_name);
|
2004-03-04 15:05:54 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 15:13:53 +00:00
|
|
|
int VideoStream::SetupCodec(
|
|
|
|
int colours,
|
|
|
|
int subpixelorder,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int bitrate,
|
|
|
|
double frame_rate
|
|
|
|
) {
|
2011-06-22 15:38:35 +00:00
|
|
|
/* ffmpeg format matching */
|
2022-01-10 15:13:53 +00:00
|
|
|
switch (colours) {
|
2011-06-22 15:38:35 +00:00
|
|
|
case ZM_COLOUR_RGB24:
|
2022-01-10 15:13:53 +00:00
|
|
|
if (subpixelorder == ZM_SUBPIX_ORDER_BGR) {
|
2011-06-22 15:38:35 +00:00
|
|
|
/* BGR subpixel order */
|
2015-11-03 00:58:23 +00:00
|
|
|
pf = AV_PIX_FMT_BGR24;
|
2011-06-22 15:38:35 +00:00
|
|
|
} else {
|
|
|
|
/* Assume RGB subpixel order */
|
2015-11-03 00:58:23 +00:00
|
|
|
pf = AV_PIX_FMT_RGB24;
|
2011-06-22 15:38:35 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZM_COLOUR_RGB32:
|
2022-01-10 15:13:53 +00:00
|
|
|
if (subpixelorder == ZM_SUBPIX_ORDER_ARGB) {
|
2011-06-22 15:38:35 +00:00
|
|
|
/* ARGB subpixel order */
|
2015-11-03 00:58:23 +00:00
|
|
|
pf = AV_PIX_FMT_ARGB;
|
2022-01-10 15:13:53 +00:00
|
|
|
} else if (subpixelorder == ZM_SUBPIX_ORDER_ABGR) {
|
2011-06-22 15:38:35 +00:00
|
|
|
/* ABGR subpixel order */
|
2015-11-03 00:58:23 +00:00
|
|
|
pf = AV_PIX_FMT_ABGR;
|
2022-01-10 15:13:53 +00:00
|
|
|
} else if (subpixelorder == ZM_SUBPIX_ORDER_BGRA) {
|
2011-06-22 15:38:35 +00:00
|
|
|
/* BGRA subpixel order */
|
2015-11-03 00:58:23 +00:00
|
|
|
pf = AV_PIX_FMT_BGRA;
|
2011-06-22 15:38:35 +00:00
|
|
|
} else {
|
|
|
|
/* Assume RGBA subpixel order */
|
2015-11-03 00:58:23 +00:00
|
|
|
pf = AV_PIX_FMT_RGBA;
|
2011-06-22 15:38:35 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZM_COLOUR_GRAY8:
|
2015-11-03 00:58:23 +00:00
|
|
|
pf = AV_PIX_FMT_GRAY8;
|
2011-06-22 15:38:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Panic("Unexpected colours: %d",colours);
|
|
|
|
break;
|
|
|
|
}
|
2004-03-04 15:05:54 +00:00
|
|
|
|
2022-01-10 15:13:53 +00:00
|
|
|
if (strcmp("rtp", of->name) == 0) {
|
2014-04-25 20:12:58 +00:00
|
|
|
// RTP must have a packet_size.
|
|
|
|
// Not sure what this value should be really...
|
|
|
|
ofc->packet_size = width*height;
|
2018-01-31 19:35:00 +00:00
|
|
|
Debug(1,"Setting packet_size to %d", ofc->packet_size);
|
2014-04-25 20:12:58 +00:00
|
|
|
}
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2014-04-25 20:12:58 +00:00
|
|
|
_AVCODECID codec_id = of->video_codec;
|
2022-01-10 15:13:53 +00:00
|
|
|
if (codec_name) {
|
2022-03-11 12:48:29 +00:00
|
|
|
const AVCodec *a = avcodec_find_encoder_by_name(codec_name);
|
2022-01-10 15:13:53 +00:00
|
|
|
if (a) {
|
2017-01-11 19:18:11 +00:00
|
|
|
codec_id = a->id;
|
2020-08-05 23:17:00 +00:00
|
|
|
Debug(1, "Using codec \"%s\"", codec_name);
|
2017-01-11 19:18:11 +00:00
|
|
|
} else {
|
2020-08-05 23:17:00 +00:00
|
|
|
Debug(1, "Could not find codec \"%s\". Using default \"%s\"", codec_name, avcodec_get_name(codec_id));
|
2017-01-11 19:18:11 +00:00
|
|
|
}
|
2014-04-25 20:12:58 +00:00
|
|
|
}
|
|
|
|
|
2004-03-04 15:05:54 +00:00
|
|
|
/* add the video streams using the default format codecs
|
|
|
|
and initialize the codecs */
|
2020-08-25 23:45:48 +00:00
|
|
|
ost = nullptr;
|
2022-01-10 15:13:53 +00:00
|
|
|
if (codec_id != AV_CODEC_ID_NONE) {
|
2020-08-05 23:17:00 +00:00
|
|
|
codec = avcodec_find_encoder(codec_id);
|
2022-01-10 15:13:53 +00:00
|
|
|
if (!codec) {
|
|
|
|
Error("Could not find encoder for '%s'", avcodec_get_name(codec_id));
|
|
|
|
return -1;
|
2004-03-04 15:05:54 +00:00
|
|
|
}
|
2014-04-25 20:12:58 +00:00
|
|
|
|
2020-08-05 23:17:00 +00:00
|
|
|
Debug(1, "Found encoder for '%s'", avcodec_get_name(codec_id));
|
2022-01-10 15:13:53 +00:00
|
|
|
ost = avformat_new_stream(ofc, codec);
|
|
|
|
if (!ost) {
|
|
|
|
Error("Could not alloc stream");
|
|
|
|
return -1;
|
2014-04-25 20:12:58 +00:00
|
|
|
}
|
2022-01-10 15:13:53 +00:00
|
|
|
Debug(1, "Allocated stream (%d) !=? (%d)", ost->id , ofc->nb_streams - 1);
|
2014-04-25 20:12:58 +00:00
|
|
|
ost->id = ofc->nb_streams - 1;
|
|
|
|
|
2020-08-25 23:45:48 +00:00
|
|
|
codec_context = avcodec_alloc_context3(nullptr);
|
2018-01-31 19:35:00 +00:00
|
|
|
//avcodec_parameters_to_context(codec_context, ost->codecpar);
|
2017-11-08 02:21:51 +00:00
|
|
|
codec_context->codec_id = codec->id;
|
|
|
|
codec_context->codec_type = codec->type;
|
2014-04-25 20:12:58 +00:00
|
|
|
|
2018-01-31 19:35:00 +00:00
|
|
|
codec_context->pix_fmt = strcmp("mjpeg", ofc->oformat->name) == 0 ? AV_PIX_FMT_YUVJ422P : AV_PIX_FMT_YUV420P;
|
2022-01-10 15:13:53 +00:00
|
|
|
if (bitrate <= 100) {
|
2014-04-25 20:12:58 +00:00
|
|
|
// Quality based bitrate control (VBR). Scale is 1..31 where 1 is best.
|
|
|
|
// This gets rid of artifacts in the beginning of the movie; and well, even quality.
|
2017-11-08 02:21:51 +00:00
|
|
|
codec_context->flags |= AV_CODEC_FLAG_QSCALE;
|
|
|
|
codec_context->global_quality = FF_QP2LAMBDA * (31 - (31 * (bitrate / 100.0)));
|
2017-01-11 19:18:11 +00:00
|
|
|
} else {
|
2017-11-08 02:21:51 +00:00
|
|
|
codec_context->bit_rate = bitrate;
|
2014-04-25 20:12:58 +00:00
|
|
|
}
|
2004-03-04 15:05:54 +00:00
|
|
|
|
|
|
|
/* resolution must be a multiple of two */
|
2017-11-08 02:21:51 +00:00
|
|
|
codec_context->width = width;
|
|
|
|
codec_context->height = height;
|
2005-10-03 20:38:40 +00:00
|
|
|
/* time base: this is the fundamental unit of time (in seconds) in terms
|
|
|
|
of which frame timestamps are represented. for fixed-fps content,
|
|
|
|
timebase should be 1/framerate and timestamp increments should be
|
|
|
|
identically 1. */
|
2017-11-08 02:21:51 +00:00
|
|
|
codec_context->time_base.den = frame_rate;
|
|
|
|
codec_context->time_base.num = 1;
|
2018-01-31 19:35:00 +00:00
|
|
|
ost->time_base.den = frame_rate;
|
|
|
|
ost->time_base.num = 1;
|
|
|
|
Debug( 1, "Will encode in %d fps. %dx%d", codec_context->time_base.den, width, height );
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2014-04-25 20:12:58 +00:00
|
|
|
/* emit one intra frame every second */
|
2017-11-08 02:21:51 +00:00
|
|
|
codec_context->gop_size = frame_rate;
|
2014-04-25 20:12:58 +00:00
|
|
|
|
2015-04-19 10:38:23 +00:00
|
|
|
// some formats want stream headers to be separate
|
2022-01-10 15:13:53 +00:00
|
|
|
if (of->flags & AVFMT_GLOBALHEADER)
|
2017-11-08 02:21:51 +00:00
|
|
|
codec_context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
|
2021-06-05 15:01:46 +00:00
|
|
|
|
2018-01-31 19:35:00 +00:00
|
|
|
avcodec_parameters_from_context(ost->codecpar, codec_context);
|
|
|
|
zm_dump_codecpar(ost->codecpar);
|
2017-01-11 19:18:11 +00:00
|
|
|
} else {
|
2022-01-10 15:13:53 +00:00
|
|
|
Error("of->video_codec == AV_CODEC_ID_NONE");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2004-03-04 15:05:54 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 19:18:11 +00:00
|
|
|
void VideoStream::SetParameters( ) {
|
2004-03-04 15:05:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 16:04:52 +00:00
|
|
|
const char *VideoStream::MimeType() const {
|
2017-01-11 19:18:11 +00:00
|
|
|
for ( unsigned int i = 0; i < sizeof (mime_data) / sizeof (*mime_data); i++ ) {
|
2021-02-05 16:04:52 +00:00
|
|
|
if ( strcmp(format, mime_data[i].format) == 0 ) {
|
|
|
|
Debug(1, "MimeType is \"%s\"", mime_data[i].mime_type);
|
2018-01-31 19:35:00 +00:00
|
|
|
return mime_data[i].mime_type;
|
2005-12-07 13:42:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const char *mime_type = of->mime_type;
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( !mime_type ) {
|
2021-02-05 16:04:52 +00:00
|
|
|
std::string mime = std::string("video/") + format;
|
|
|
|
mime_type = strdup(mime.c_str()); // mem leak
|
|
|
|
Warning( "Unable to determine mime type for '%s' format, using '%s' as default", format, mime_type);
|
2005-12-07 13:42:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 16:04:52 +00:00
|
|
|
Debug(1, "MimeType is \"%s\"", mime_type);
|
2014-04-25 20:12:58 +00:00
|
|
|
|
2018-01-31 19:35:00 +00:00
|
|
|
return mime_type;
|
2005-12-07 13:42:25 +00:00
|
|
|
}
|
|
|
|
|
2018-04-12 15:29:35 +00:00
|
|
|
bool VideoStream::OpenStream( ) {
|
2017-11-17 12:52:26 +00:00
|
|
|
int ret;
|
2014-04-25 20:12:58 +00:00
|
|
|
|
2022-01-10 15:13:53 +00:00
|
|
|
/* now that all the parameters are set, we can open the
|
2004-03-04 15:05:54 +00:00
|
|
|
video codecs and allocate the necessary encode buffers */
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( ost ) {
|
2018-01-31 19:35:00 +00:00
|
|
|
Debug(1,"Opening codec");
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2004-03-04 15:05:54 +00:00
|
|
|
/* open the codec */
|
2021-06-05 15:01:46 +00:00
|
|
|
|
|
|
|
if ((ret = avcodec_open2(codec_context, codec, nullptr)) < 0) {
|
2018-04-12 15:29:35 +00:00
|
|
|
Error("Could not open codec. Error code %d \"%s\"", ret, av_err2str(ret));
|
|
|
|
return false;
|
2004-03-04 15:05:54 +00:00
|
|
|
}
|
|
|
|
|
2014-04-25 20:12:58 +00:00
|
|
|
Debug( 1, "Opened codec" );
|
|
|
|
|
2004-03-04 15:05:54 +00:00
|
|
|
/* allocate the encoded raw picture */
|
2017-11-08 02:21:51 +00:00
|
|
|
opicture = zm_av_frame_alloc( );
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( !opicture ) {
|
2018-04-12 15:29:35 +00:00
|
|
|
Error("Could not allocate opicture");
|
|
|
|
return false;
|
2016-04-04 14:11:48 +00:00
|
|
|
}
|
2018-01-31 19:35:00 +00:00
|
|
|
opicture->width = codec_context->width;
|
|
|
|
opicture->height = codec_context->height;
|
|
|
|
opicture->format = codec_context->pix_fmt;
|
2016-04-29 11:27:28 +00:00
|
|
|
|
2018-04-12 15:29:35 +00:00
|
|
|
int size = av_image_get_buffer_size(codec_context->pix_fmt, codec_context->width, codec_context->height, 1);
|
2016-03-02 14:03:55 +00:00
|
|
|
|
2018-04-12 15:29:35 +00:00
|
|
|
uint8_t *opicture_buf = (uint8_t *)av_malloc(size);
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( !opicture_buf ) {
|
|
|
|
av_frame_free( &opicture );
|
2018-04-12 15:29:35 +00:00
|
|
|
Error( "Could not allocate opicture_buf" );
|
|
|
|
return false;
|
2016-04-04 14:11:48 +00:00
|
|
|
}
|
2016-04-29 11:27:28 +00:00
|
|
|
av_image_fill_arrays(opicture->data, opicture->linesize,
|
2017-11-08 02:21:51 +00:00
|
|
|
opicture_buf, codec_context->pix_fmt, codec_context->width, codec_context->height, 1);
|
2004-03-04 15:05:54 +00:00
|
|
|
|
2016-04-04 14:11:48 +00:00
|
|
|
/* if the output format is not identical to the input format, then a temporary
|
|
|
|
picture is needed too. It is then converted to the required
|
|
|
|
output format */
|
2020-08-25 23:45:48 +00:00
|
|
|
tmp_opicture = nullptr;
|
2017-11-08 02:21:51 +00:00
|
|
|
if ( codec_context->pix_fmt != pf ) {
|
2021-06-05 15:01:46 +00:00
|
|
|
tmp_opicture = av_frame_alloc();
|
|
|
|
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( !tmp_opicture ) {
|
2018-04-12 15:29:35 +00:00
|
|
|
Error( "Could not allocate tmp_opicture" );
|
|
|
|
return false;
|
2016-04-04 14:11:48 +00:00
|
|
|
}
|
2022-07-14 14:35:22 +00:00
|
|
|
size = av_image_get_buffer_size(pf, codec_context->width, codec_context->height, 1);
|
2016-04-04 14:11:48 +00:00
|
|
|
uint8_t *tmp_opicture_buf = (uint8_t *)av_malloc( size );
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( !tmp_opicture_buf ) {
|
2016-05-03 18:34:58 +00:00
|
|
|
av_frame_free( &tmp_opicture );
|
2018-04-12 15:29:35 +00:00
|
|
|
Error( "Could not allocate tmp_opicture_buf" );
|
|
|
|
return false;
|
2016-04-04 14:11:48 +00:00
|
|
|
}
|
2021-06-05 15:01:46 +00:00
|
|
|
|
2016-04-29 11:27:28 +00:00
|
|
|
av_image_fill_arrays(tmp_opicture->data,
|
|
|
|
tmp_opicture->linesize, tmp_opicture_buf, pf,
|
2017-11-08 02:21:51 +00:00
|
|
|
codec_context->width, codec_context->height, 1);
|
2016-04-04 14:11:48 +00:00
|
|
|
}
|
2018-01-31 19:35:00 +00:00
|
|
|
} // end if ost
|
2004-03-04 15:05:54 +00:00
|
|
|
|
2016-04-04 14:11:48 +00:00
|
|
|
/* open the output file, if needed */
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( !(of->flags & AVFMT_NOFILE) ) {
|
2020-08-25 23:45:48 +00:00
|
|
|
ret = avio_open2( &ofc->pb, filename, AVIO_FLAG_WRITE, nullptr, nullptr );
|
2021-06-05 15:01:46 +00:00
|
|
|
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( ret < 0 ) {
|
2018-04-12 15:29:35 +00:00
|
|
|
Error("Could not open '%s'", filename);
|
|
|
|
return false;
|
2004-03-04 15:05:54 +00:00
|
|
|
}
|
2014-04-25 20:12:58 +00:00
|
|
|
|
2018-04-12 15:29:35 +00:00
|
|
|
Debug(1, "Opened output \"%s\"", filename);
|
2017-01-11 19:18:11 +00:00
|
|
|
} else {
|
2018-04-12 15:29:35 +00:00
|
|
|
Error( "of->flags & AVFMT_NOFILE" );
|
|
|
|
return false;
|
2004-03-04 15:05:54 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 23:45:48 +00:00
|
|
|
video_outbuf = nullptr;
|
2018-01-31 19:35:00 +00:00
|
|
|
if (codec_context->codec_type == AVMEDIA_TYPE_VIDEO &&
|
|
|
|
codec_context->codec_id == AV_CODEC_ID_RAWVIDEO) {
|
2004-03-04 15:05:54 +00:00
|
|
|
/* allocate output buffer */
|
|
|
|
/* XXX: API change will be done */
|
2014-04-25 20:12:58 +00:00
|
|
|
// TODO: Make buffer dynamic.
|
|
|
|
video_outbuf_size = 4000000;
|
|
|
|
video_outbuf = (uint8_t *)malloc( video_outbuf_size );
|
2020-08-25 23:45:48 +00:00
|
|
|
if ( video_outbuf == nullptr ) {
|
2015-01-29 20:32:50 +00:00
|
|
|
Fatal("Unable to malloc memory for outbuf");
|
|
|
|
}
|
2004-03-04 15:05:54 +00:00
|
|
|
}
|
2015-05-29 15:38:02 +00:00
|
|
|
|
2014-04-25 20:12:58 +00:00
|
|
|
av_dump_format(ofc, 0, filename, 1);
|
2015-05-29 15:38:02 +00:00
|
|
|
|
2020-08-25 23:45:48 +00:00
|
|
|
ret = avformat_write_header(ofc, nullptr);
|
2015-05-29 15:38:02 +00:00
|
|
|
|
2018-04-12 15:29:35 +00:00
|
|
|
if ( ret < 0 ) {
|
|
|
|
Error("?_write_header failed with error %d \"%s\"", ret, av_err2str(ret));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2004-03-04 15:05:54 +00:00
|
|
|
}
|
|
|
|
|
2014-04-25 20:12:58 +00:00
|
|
|
VideoStream::VideoStream( const char *in_filename, const char *in_format, int bitrate, double frame_rate, int colours, int subpixelorder, int width, int height ) :
|
|
|
|
filename(in_filename),
|
|
|
|
format(in_format),
|
2020-08-25 23:45:48 +00:00
|
|
|
opicture(nullptr),
|
|
|
|
tmp_opicture(nullptr),
|
|
|
|
video_outbuf(nullptr),
|
2017-11-18 21:00:10 +00:00
|
|
|
video_outbuf_size(0),
|
2014-04-25 20:12:58 +00:00
|
|
|
last_pts( -1 ),
|
|
|
|
streaming_thread(0),
|
|
|
|
do_streaming(true),
|
2017-11-18 21:00:10 +00:00
|
|
|
add_timestamp(false),
|
|
|
|
timestamp(0),
|
2020-08-25 23:45:48 +00:00
|
|
|
buffer_copy(nullptr),
|
2014-04-25 20:12:58 +00:00
|
|
|
buffer_copy_lock(new pthread_mutex_t),
|
2015-02-05 01:19:45 +00:00
|
|
|
buffer_copy_size(0),
|
2014-04-26 16:25:48 +00:00
|
|
|
buffer_copy_used(0),
|
2017-01-11 19:18:11 +00:00
|
|
|
packet_index(0)
|
2004-03-04 15:05:54 +00:00
|
|
|
{
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( !initialised ) {
|
2014-04-25 20:12:58 +00:00
|
|
|
Initialise( );
|
|
|
|
}
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( format ) {
|
2014-04-25 20:12:58 +00:00
|
|
|
int length = strlen(format);
|
|
|
|
codec_and_format = new char[length+1];;
|
|
|
|
strcpy( codec_and_format, format );
|
|
|
|
format = codec_and_format;
|
2020-08-25 23:45:48 +00:00
|
|
|
codec_name = nullptr;
|
2014-04-25 20:12:58 +00:00
|
|
|
char *f = strchr(codec_and_format, '/');
|
2020-08-25 23:45:48 +00:00
|
|
|
if (f != nullptr) {
|
2014-04-25 20:12:58 +00:00
|
|
|
*f = 0;
|
|
|
|
codec_name = f+1;
|
|
|
|
}
|
2004-03-04 15:05:54 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 23:45:48 +00:00
|
|
|
codec_context = nullptr;
|
2014-04-25 20:12:58 +00:00
|
|
|
SetupFormat( );
|
2011-06-22 15:38:35 +00:00
|
|
|
SetupCodec( colours, subpixelorder, width, height, bitrate, frame_rate );
|
2014-04-25 20:12:58 +00:00
|
|
|
SetParameters( );
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2017-01-11 19:18:11 +00:00
|
|
|
// Allocate buffered packets.
|
|
|
|
packet_buffers = new AVPacket*[2];
|
|
|
|
packet_buffers[0] = new AVPacket();
|
|
|
|
packet_buffers[1] = new AVPacket();
|
|
|
|
packet_index = 0;
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2014-04-25 20:12:58 +00:00
|
|
|
// Initialize mutex used by streaming thread.
|
2020-08-25 23:45:48 +00:00
|
|
|
if ( pthread_mutex_init( buffer_copy_lock, nullptr ) != 0 ) {
|
2014-04-25 20:12:58 +00:00
|
|
|
Fatal("pthread_mutex_init failed");
|
|
|
|
}
|
2017-11-08 02:21:51 +00:00
|
|
|
|
2004-03-04 15:05:54 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 19:18:11 +00:00
|
|
|
VideoStream::~VideoStream( ) {
|
2014-04-25 20:12:58 +00:00
|
|
|
Debug( 1, "VideoStream destructor." );
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2014-04-25 20:12:58 +00:00
|
|
|
// Stop streaming thread.
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( streaming_thread ) {
|
2014-04-25 20:12:58 +00:00
|
|
|
do_streaming = false;
|
|
|
|
void* thread_exit_code;
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2014-04-25 20:12:58 +00:00
|
|
|
Debug( 1, "Asking streaming thread to exit." );
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2014-04-25 20:12:58 +00:00
|
|
|
// Wait for thread to exit.
|
|
|
|
pthread_join(streaming_thread, &thread_exit_code);
|
|
|
|
}
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2020-08-25 23:45:48 +00:00
|
|
|
if ( buffer_copy != nullptr ) {
|
2014-04-25 20:12:58 +00:00
|
|
|
av_free( buffer_copy );
|
|
|
|
}
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( buffer_copy_lock ) {
|
|
|
|
if ( pthread_mutex_destroy( buffer_copy_lock ) != 0 ) {
|
2014-04-25 20:12:58 +00:00
|
|
|
Error( "pthread_mutex_destroy failed" );
|
|
|
|
}
|
|
|
|
delete buffer_copy_lock;
|
|
|
|
}
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2017-01-11 19:18:11 +00:00
|
|
|
if (packet_buffers) {
|
|
|
|
delete packet_buffers[0];
|
|
|
|
delete packet_buffers[1];
|
|
|
|
delete[] packet_buffers;
|
|
|
|
}
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2004-03-04 15:05:54 +00:00
|
|
|
/* close each codec */
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( ost ) {
|
2017-11-08 02:21:51 +00:00
|
|
|
avcodec_close( codec_context );
|
2014-04-25 20:12:58 +00:00
|
|
|
av_free( opicture->data[0] );
|
2015-11-03 00:58:23 +00:00
|
|
|
av_frame_free( &opicture );
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( tmp_opicture ) {
|
2014-04-25 20:12:58 +00:00
|
|
|
av_free( tmp_opicture->data[0] );
|
2015-11-03 00:58:23 +00:00
|
|
|
av_frame_free( &tmp_opicture );
|
2004-03-04 15:05:54 +00:00
|
|
|
}
|
2014-04-25 20:12:58 +00:00
|
|
|
av_free( video_outbuf );
|
2004-03-04 15:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* write the trailer, if any */
|
2014-04-25 20:12:58 +00:00
|
|
|
av_write_trailer( ofc );
|
|
|
|
|
2004-03-04 15:05:54 +00:00
|
|
|
/* free the streams */
|
2017-01-11 19:18:11 +00:00
|
|
|
for ( unsigned int i = 0; i < ofc->nb_streams; i++ ) {
|
2014-04-25 20:12:58 +00:00
|
|
|
av_freep( &ofc->streams[i] );
|
2004-03-04 15:05:54 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( !(of->flags & AVFMT_NOFILE) ) {
|
2004-03-04 15:05:54 +00:00
|
|
|
/* close the output file */
|
2014-04-25 20:12:58 +00:00
|
|
|
avio_close( ofc->pb );
|
2004-03-04 15:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* free the stream */
|
2014-04-25 20:12:58 +00:00
|
|
|
av_free( ofc );
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2014-04-25 20:12:58 +00:00
|
|
|
/* free format and codec_name data. */
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( codec_and_format ) {
|
2014-04-25 20:12:58 +00:00
|
|
|
delete codec_and_format;
|
|
|
|
}
|
2004-03-04 15:05:54 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 19:18:11 +00:00
|
|
|
double VideoStream::EncodeFrame( const uint8_t *buffer, int buffer_size, bool _add_timestamp, unsigned int _timestamp ) {
|
2018-04-12 15:29:35 +00:00
|
|
|
if ( pthread_mutex_lock(buffer_copy_lock) != 0 ) {
|
2014-04-25 20:12:58 +00:00
|
|
|
Fatal( "EncodeFrame: pthread_mutex_lock failed." );
|
|
|
|
}
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2017-01-11 19:18:11 +00:00
|
|
|
if (buffer_copy_size < buffer_size) {
|
|
|
|
if ( buffer_copy ) {
|
2018-04-12 15:29:35 +00:00
|
|
|
av_free(buffer_copy);
|
2014-04-25 20:12:58 +00:00
|
|
|
}
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2014-04-25 20:12:58 +00:00
|
|
|
// Allocate a buffer to store source images for the streaming thread to encode.
|
2018-04-12 15:29:35 +00:00
|
|
|
buffer_copy = (uint8_t *)av_malloc(buffer_size);
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( !buffer_copy ) {
|
2018-04-12 15:29:35 +00:00
|
|
|
Error( "Could not allocate buffer_copy" );
|
|
|
|
pthread_mutex_unlock(buffer_copy_lock);
|
2017-12-12 18:19:23 +00:00
|
|
|
return 0;
|
2014-04-25 20:12:58 +00:00
|
|
|
}
|
|
|
|
buffer_copy_size = buffer_size;
|
|
|
|
}
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2014-04-25 20:12:58 +00:00
|
|
|
add_timestamp = _add_timestamp;
|
|
|
|
timestamp = _timestamp;
|
|
|
|
buffer_copy_used = buffer_size;
|
|
|
|
memcpy(buffer_copy, buffer, buffer_size);
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2018-04-12 15:29:35 +00:00
|
|
|
if ( pthread_mutex_unlock(buffer_copy_lock) != 0 ) {
|
2014-04-25 20:12:58 +00:00
|
|
|
Fatal( "EncodeFrame: pthread_mutex_unlock failed." );
|
2004-03-08 10:33:19 +00:00
|
|
|
}
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( streaming_thread == 0 ) {
|
2014-04-25 20:12:58 +00:00
|
|
|
Debug( 1, "Starting streaming thread" );
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2014-04-25 20:12:58 +00:00
|
|
|
// Start a thread for streaming encoded video.
|
2020-08-25 23:45:48 +00:00
|
|
|
if (pthread_create( &streaming_thread, nullptr, StreamingThreadCallback, (void*) this) != 0){
|
2014-04-25 20:12:58 +00:00
|
|
|
// Log a fatal error and exit the process.
|
|
|
|
Fatal( "VideoStream failed to create streaming thread." );
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2014-04-25 20:12:58 +00:00
|
|
|
//return ActuallyEncodeFrame( buffer, buffer_size, add_timestamp, timestamp);
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2014-04-25 20:12:58 +00:00
|
|
|
return _timestamp;
|
|
|
|
}
|
|
|
|
|
2017-01-11 19:18:11 +00:00
|
|
|
double VideoStream::ActuallyEncodeFrame( const uint8_t *buffer, int buffer_size, bool add_timestamp, unsigned int timestamp ) {
|
2017-11-18 21:00:10 +00:00
|
|
|
if ( codec_context->pix_fmt != pf ) {
|
2022-01-10 15:13:53 +00:00
|
|
|
static struct SwsContext *img_convert_ctx = nullptr;
|
2004-03-04 15:05:54 +00:00
|
|
|
memcpy( tmp_opicture->data[0], buffer, buffer_size );
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( !img_convert_ctx ) {
|
2020-08-25 23:45:48 +00:00
|
|
|
img_convert_ctx = sws_getCachedContext( nullptr, codec_context->width, codec_context->height, pf, codec_context->width, codec_context->height, codec_context->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr );
|
2014-04-25 20:12:58 +00:00
|
|
|
if ( !img_convert_ctx )
|
|
|
|
Panic( "Unable to initialise image scaling context" );
|
|
|
|
}
|
2017-11-08 02:21:51 +00:00
|
|
|
sws_scale( img_convert_ctx, tmp_opicture->data, tmp_opicture->linesize, 0, codec_context->height, opicture->data, opicture->linesize );
|
2017-01-11 19:18:11 +00:00
|
|
|
} else {
|
2004-03-04 15:05:54 +00:00
|
|
|
memcpy( opicture->data[0], buffer, buffer_size );
|
|
|
|
}
|
|
|
|
AVFrame *opicture_ptr = opicture;
|
2022-01-10 15:13:53 +00:00
|
|
|
|
2014-04-26 16:25:48 +00:00
|
|
|
AVPacket *pkt = packet_buffers[packet_index];
|
|
|
|
av_init_packet( pkt );
|
2022-01-10 15:13:53 +00:00
|
|
|
if (codec_context->codec_type == AVMEDIA_TYPE_VIDEO &&
|
|
|
|
codec_context->codec_id == AV_CODEC_ID_RAWVIDEO) {
|
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
|
|
|
pkt->stream_index = ost->index;
|
|
|
|
pkt->data = (uint8_t *)opicture_ptr;
|
2022-04-03 20:28:38 +00:00
|
|
|
pkt->size = buffer_size;
|
2017-01-11 19:18:11 +00:00
|
|
|
} else {
|
2017-11-08 02:21:51 +00:00
|
|
|
opicture_ptr->pts = codec_context->frame_number;
|
|
|
|
opicture_ptr->quality = codec_context->global_quality;
|
|
|
|
|
2022-01-10 15:13:53 +00:00
|
|
|
avcodec_send_frame(codec_context, opicture_ptr);
|
|
|
|
int ret = avcodec_receive_packet(codec_context, pkt);
|
|
|
|
if (ret < 0) {
|
|
|
|
if (AVERROR_EOF != ret) {
|
2022-07-14 14:35:22 +00:00
|
|
|
Error("Error encoding video (%d) (%s)", ret, av_err2str(ret));
|
2017-11-08 02:21:51 +00:00
|
|
|
}
|
2022-01-10 15:13:53 +00:00
|
|
|
} else {
|
|
|
|
// if ( c->coded_frame->key_frame )
|
|
|
|
// {
|
|
|
|
// pkt->flags |= AV_PKT_FLAG_KEY;
|
|
|
|
// }
|
2016-04-04 14:11:48 +00:00
|
|
|
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( pkt->pts != (int64_t)AV_NOPTS_VALUE ) {
|
2017-11-08 02:21:51 +00:00
|
|
|
pkt->pts = av_rescale_q( pkt->pts, codec_context->time_base, ost->time_base );
|
2016-04-04 14:11:48 +00:00
|
|
|
}
|
2017-01-11 19:18:11 +00:00
|
|
|
if ( pkt->dts != (int64_t)AV_NOPTS_VALUE ) {
|
2017-11-08 02:21:51 +00:00
|
|
|
pkt->dts = av_rescale_q( pkt->dts, codec_context->time_base, ost->time_base );
|
2016-04-04 14:11:48 +00:00
|
|
|
}
|
2017-11-08 02:21:51 +00:00
|
|
|
pkt->duration = av_rescale_q( pkt->duration, codec_context->time_base, ost->time_base );
|
2016-04-04 14:11:48 +00:00
|
|
|
pkt->stream_index = ost->index;
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 15:13:53 +00:00
|
|
|
|
|
|
|
return opicture_ptr->pts;
|
2014-04-25 20:12:58 +00:00
|
|
|
}
|
|
|
|
|
2014-04-26 16:25:48 +00:00
|
|
|
int VideoStream::SendPacket(AVPacket *packet) {
|
2022-01-10 15:13:53 +00:00
|
|
|
int ret = av_write_frame(ofc, packet);
|
|
|
|
if (ret < 0) {
|
|
|
|
Error("Error %d while writing video frame: %s", ret, av_err2str(errno));
|
|
|
|
}
|
|
|
|
av_packet_unref(packet);
|
|
|
|
return ret;
|
2014-04-26 16:25:48 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 13:56:13 +00:00
|
|
|
void *VideoStream::StreamingThreadCallback(void *ctx) {
|
|
|
|
Debug(1, "StreamingThreadCallback started");
|
2017-01-11 19:18:11 +00:00
|
|
|
|
2021-06-13 13:56:13 +00:00
|
|
|
if (ctx == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-04-25 20:12:58 +00:00
|
|
|
|
2021-06-13 13:56:13 +00:00
|
|
|
VideoStream *videoStream = reinterpret_cast<VideoStream *>(ctx);
|
2017-11-08 02:21:51 +00:00
|
|
|
|
2021-06-13 13:56:13 +00:00
|
|
|
TimePoint::duration target_interval = std::chrono::duration_cast<TimePoint::duration>(FPSeconds(
|
|
|
|
videoStream->codec_context->time_base.num / static_cast<double>(videoStream->codec_context->time_base.den)));
|
2021-06-05 15:01:46 +00:00
|
|
|
|
2021-06-13 13:56:13 +00:00
|
|
|
uint64_t frame_count = 0;
|
|
|
|
TimePoint start_time = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
while (videoStream->do_streaming) {
|
|
|
|
TimePoint current_time = std::chrono::steady_clock::now();
|
|
|
|
TimePoint target = start_time + (target_interval * frame_count);
|
|
|
|
|
|
|
|
if (current_time < target) {
|
|
|
|
// It's not time to render a frame yet.
|
|
|
|
std::this_thread::sleep_for(target - current_time);
|
|
|
|
}
|
2017-01-11 19:18:11 +00:00
|
|
|
|
|
|
|
// By sending the last rendered frame we deliver frames to the client more accurate.
|
|
|
|
// If we're encoding the frame before sending it there will be lag.
|
|
|
|
// Since this lag is not constant the client may skip frames.
|
|
|
|
|
|
|
|
// Get the last rendered packet.
|
|
|
|
AVPacket *packet = videoStream->packet_buffers[videoStream->packet_index];
|
|
|
|
if (packet->size) {
|
|
|
|
videoStream->SendPacket(packet);
|
|
|
|
}
|
2021-06-13 13:56:13 +00:00
|
|
|
av_packet_unref(packet);
|
2021-06-05 15:01:46 +00:00
|
|
|
|
2017-01-11 19:18:11 +00:00
|
|
|
videoStream->packet_index = videoStream->packet_index ? 0 : 1;
|
|
|
|
|
|
|
|
// Lock buffer and render next frame.
|
2021-06-13 13:56:13 +00:00
|
|
|
if (pthread_mutex_lock(videoStream->buffer_copy_lock) != 0) {
|
|
|
|
Fatal("StreamingThreadCallback: pthread_mutex_lock failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (videoStream->buffer_copy) {
|
|
|
|
// Encode next frame.
|
|
|
|
videoStream->ActuallyEncodeFrame(videoStream->buffer_copy,
|
|
|
|
videoStream->buffer_copy_used,
|
|
|
|
videoStream->add_timestamp,
|
|
|
|
videoStream->timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pthread_mutex_unlock(videoStream->buffer_copy_lock) != 0) {
|
|
|
|
Fatal("StreamingThreadCallback: pthread_mutex_unlock failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
frame_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2004-03-04 15:05:54 +00:00
|
|
|
}
|