2004-03-04 23:05:54 +08:00
|
|
|
/*
|
|
|
|
* ZoneMinder MPEG class implementation, $Date$, $Revision$
|
2008-07-25 17:33:23 +08:00
|
|
|
* Copyright (C) 2001-2008 Philip Coombes
|
2004-03-04 23:05:54 +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.
|
2014-04-26 04:12:58 +08:00
|
|
|
*/
|
2004-03-04 23:05:54 +08:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "zm.h"
|
2011-06-22 23:38:35 +08:00
|
|
|
#include "zm_rgb.h"
|
2004-03-04 23:05:54 +08:00
|
|
|
#include "zm_mpeg.h"
|
|
|
|
|
|
|
|
#if HAVE_LIBAVCODEC
|
2017-01-12 03:18:11 +08:00
|
|
|
extern "C" {
|
2014-04-26 04:12:58 +08:00
|
|
|
#include <libavutil/mathematics.h>
|
|
|
|
#include <libavcodec/avcodec.h>
|
2013-03-17 07:45:21 +08:00
|
|
|
}
|
|
|
|
|
2004-03-04 23:05:54 +08:00
|
|
|
bool VideoStream::initialised = false;
|
|
|
|
|
2005-12-07 21:42:25 +08:00
|
|
|
VideoStream::MimeData VideoStream::mime_data[] = {
|
|
|
|
{ "asf", "video/x-ms-asf" },
|
|
|
|
{ "swf", "application/x-shockwave-flash" },
|
2010-03-01 03:16:40 +08:00
|
|
|
{ "flv", "video/x-flv" },
|
2014-04-26 04:12:58 +08:00
|
|
|
{ "mov", "video/quicktime" }
|
2005-12-07 21:42:25 +08:00
|
|
|
};
|
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
void VideoStream::Initialise( ) {
|
|
|
|
if ( logDebugging() ) {
|
|
|
|
av_log_set_level( AV_LOG_DEBUG );
|
|
|
|
} else {
|
|
|
|
av_log_set_level( AV_LOG_QUIET );
|
|
|
|
}
|
2015-05-30 01:23:00 +08:00
|
|
|
|
2014-04-26 04:12:58 +08:00
|
|
|
av_register_all( );
|
2015-05-29 23:38:02 +08:00
|
|
|
#if LIBAVFORMAT_VERSION_CHECK(53, 13, 0, 19, 0)
|
2014-04-26 04:12:58 +08:00
|
|
|
avformat_network_init();
|
|
|
|
#endif
|
2004-03-04 23:05:54 +08:00
|
|
|
initialised = true;
|
|
|
|
}
|
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
void VideoStream::SetupFormat( ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
/* allocate the output media context */
|
|
|
|
ofc = NULL;
|
2015-05-30 01:23:00 +08:00
|
|
|
#if (LIBAVFORMAT_VERSION_CHECK(53, 2, 0, 2, 0) && (LIBAVFORMAT_VERSION_MICRO >= 100))
|
2014-04-26 04:12:58 +08:00
|
|
|
avformat_alloc_output_context2( &ofc, NULL, format, filename );
|
|
|
|
#else
|
|
|
|
AVFormatContext *s= avformat_alloc_context();
|
2017-01-12 03:18:11 +08:00
|
|
|
if(!s) {
|
2014-04-26 04:12:58 +08:00
|
|
|
Fatal( "avformat_alloc_context failed %d \"%s\"", (size_t)ofc, av_err2str((size_t)ofc) );
|
2017-11-19 05:00:10 +08:00
|
|
|
return;
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
2015-05-30 01:23:00 +08:00
|
|
|
|
2014-04-26 04:12:58 +08:00
|
|
|
AVOutputFormat *oformat;
|
|
|
|
if (format) {
|
2015-05-29 23:38:02 +08:00
|
|
|
#if LIBAVFORMAT_VERSION_CHECK(52, 45, 0, 45, 0)
|
2014-04-26 04:12:58 +08:00
|
|
|
oformat = av_guess_format(format, NULL, NULL);
|
|
|
|
#else
|
|
|
|
oformat = guess_format(format, NULL, NULL);
|
|
|
|
#endif
|
|
|
|
if (!oformat) {
|
|
|
|
Fatal( "Requested output format '%s' is not a suitable output format", format );
|
|
|
|
}
|
|
|
|
} else {
|
2015-05-29 23:38:02 +08:00
|
|
|
#if LIBAVFORMAT_VERSION_CHECK(52, 45, 0, 45, 0)
|
2014-04-26 04:12:58 +08:00
|
|
|
oformat = av_guess_format(NULL, filename, NULL);
|
|
|
|
#else
|
|
|
|
oformat = guess_format(NULL, filename, NULL);
|
|
|
|
#endif
|
|
|
|
if (!oformat) {
|
|
|
|
Fatal( "Unable to find a suitable output format for '%s'", format );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s->oformat = oformat;
|
|
|
|
|
|
|
|
if (s->oformat->priv_data_size > 0) {
|
|
|
|
s->priv_data = av_mallocz(s->oformat->priv_data_size);
|
2017-11-17 20:52:26 +08:00
|
|
|
if ( !(s->priv_data) ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
Fatal( "Could not allocate private data for output format." );
|
|
|
|
}
|
2015-05-29 23:38:02 +08:00
|
|
|
#if LIBAVFORMAT_VERSION_CHECK(52, 92, 0, 92, 0)
|
2014-04-26 04:12:58 +08:00
|
|
|
if (s->oformat->priv_class) {
|
|
|
|
*(const AVClass**)s->priv_data = s->oformat->priv_class;
|
|
|
|
av_opt_set_defaults(s->priv_data);
|
|
|
|
}
|
|
|
|
#endif
|
2017-01-12 03:18:11 +08:00
|
|
|
} else {
|
2018-02-01 03:35:00 +08:00
|
|
|
Debug(1,"No allocating priv_data");
|
2014-04-26 04:12:58 +08:00
|
|
|
s->priv_data = NULL;
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( filename ) {
|
2015-03-30 12:26:59 +08:00
|
|
|
snprintf( s->filename, sizeof(s->filename), "%s", filename );
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
|
|
|
|
2014-04-26 04:12:58 +08:00
|
|
|
ofc = s;
|
|
|
|
#endif
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( !ofc ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
Fatal( "avformat_alloc_..._context failed: %d", ofc );
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
2014-04-26 04:12:58 +08:00
|
|
|
|
|
|
|
of = ofc->oformat;
|
|
|
|
Debug( 1, "Using output format: %s (%s)", of->name, of->long_name );
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
void VideoStream::SetupCodec( int colours, int subpixelorder, int width, int height, int bitrate, double frame_rate ) {
|
2011-06-22 23:38:35 +08:00
|
|
|
/* ffmpeg format matching */
|
|
|
|
switch(colours) {
|
|
|
|
case ZM_COLOUR_RGB24:
|
|
|
|
if(subpixelorder == ZM_SUBPIX_ORDER_BGR) {
|
|
|
|
/* BGR subpixel order */
|
2015-11-03 08:58:23 +08:00
|
|
|
pf = AV_PIX_FMT_BGR24;
|
2011-06-22 23:38:35 +08:00
|
|
|
} else {
|
|
|
|
/* Assume RGB subpixel order */
|
2015-11-03 08:58:23 +08:00
|
|
|
pf = AV_PIX_FMT_RGB24;
|
2011-06-22 23:38:35 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZM_COLOUR_RGB32:
|
|
|
|
if(subpixelorder == ZM_SUBPIX_ORDER_ARGB) {
|
|
|
|
/* ARGB subpixel order */
|
2015-11-03 08:58:23 +08:00
|
|
|
pf = AV_PIX_FMT_ARGB;
|
2011-06-22 23:38:35 +08:00
|
|
|
} else if(subpixelorder == ZM_SUBPIX_ORDER_ABGR) {
|
|
|
|
/* ABGR subpixel order */
|
2015-11-03 08:58:23 +08:00
|
|
|
pf = AV_PIX_FMT_ABGR;
|
2011-06-22 23:38:35 +08:00
|
|
|
} else if(subpixelorder == ZM_SUBPIX_ORDER_BGRA) {
|
|
|
|
/* BGRA subpixel order */
|
2015-11-03 08:58:23 +08:00
|
|
|
pf = AV_PIX_FMT_BGRA;
|
2011-06-22 23:38:35 +08:00
|
|
|
} else {
|
|
|
|
/* Assume RGBA subpixel order */
|
2015-11-03 08:58:23 +08:00
|
|
|
pf = AV_PIX_FMT_RGBA;
|
2011-06-22 23:38:35 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZM_COLOUR_GRAY8:
|
2015-11-03 08:58:23 +08:00
|
|
|
pf = AV_PIX_FMT_GRAY8;
|
2011-06-22 23:38:35 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Panic("Unexpected colours: %d",colours);
|
|
|
|
break;
|
|
|
|
}
|
2004-03-04 23:05:54 +08:00
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( strcmp( "rtp", of->name ) == 0 ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
// RTP must have a packet_size.
|
|
|
|
// Not sure what this value should be really...
|
|
|
|
ofc->packet_size = width*height;
|
2018-02-01 03:35:00 +08:00
|
|
|
Debug(1,"Setting packet_size to %d", ofc->packet_size);
|
2014-04-26 04:12:58 +08:00
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( of->video_codec == AV_CODEC_ID_NONE ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
// RTP does not have a default codec in ffmpeg <= 0.8.
|
2014-10-16 01:23:29 +08:00
|
|
|
of->video_codec = AV_CODEC_ID_MPEG4;
|
2014-04-26 04:12:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_AVCODECID codec_id = of->video_codec;
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( codec_name ) {
|
|
|
|
AVCodec *a = avcodec_find_encoder_by_name(codec_name);
|
|
|
|
if ( a ) {
|
|
|
|
codec_id = a->id;
|
2018-02-01 03:35:00 +08:00
|
|
|
Debug( 1, "Using codec \"%s\"", codec_name );
|
2017-01-12 03:18:11 +08:00
|
|
|
} else {
|
2015-05-30 01:23:00 +08:00
|
|
|
#if (LIBAVFORMAT_VERSION_CHECK(53, 8, 0, 11, 0) && (LIBAVFORMAT_VERSION_MICRO >= 100))
|
2017-01-12 03:18:11 +08:00
|
|
|
Debug( 1, "Could not find codec \"%s\". Using default \"%s\"", codec_name, avcodec_get_name( codec_id ) );
|
2014-04-26 04:12:58 +08:00
|
|
|
#else
|
2017-01-12 03:18:11 +08:00
|
|
|
Debug( 1, "Could not find codec \"%s\". Using default \"%d\"", codec_name, codec_id );
|
2014-04-26 04:12:58 +08:00
|
|
|
#endif
|
2017-01-12 03:18:11 +08:00
|
|
|
}
|
2014-04-26 04:12:58 +08:00
|
|
|
}
|
|
|
|
|
2004-03-04 23:05:54 +08:00
|
|
|
/* add the video streams using the default format codecs
|
|
|
|
and initialize the codecs */
|
|
|
|
ost = NULL;
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( codec_id != AV_CODEC_ID_NONE ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
codec = avcodec_find_encoder( codec_id );
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( !codec ) {
|
2015-05-30 01:23:00 +08:00
|
|
|
#if (LIBAVFORMAT_VERSION_CHECK(53, 8, 0, 11, 0) && (LIBAVFORMAT_VERSION_MICRO >= 100))
|
2014-04-26 04:12:58 +08:00
|
|
|
Fatal( "Could not find encoder for '%s'", avcodec_get_name( codec_id ) );
|
2013-03-17 07:45:21 +08:00
|
|
|
#else
|
2014-04-26 04:12:58 +08:00
|
|
|
Fatal( "Could not find encoder for '%d'", codec_id );
|
2013-03-17 07:45:21 +08:00
|
|
|
#endif
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
2014-04-26 04:12:58 +08:00
|
|
|
|
2015-05-30 01:23:00 +08:00
|
|
|
#if (LIBAVFORMAT_VERSION_CHECK(53, 8, 0, 11, 0) && (LIBAVFORMAT_VERSION_MICRO >= 100))
|
2014-04-26 04:12:58 +08:00
|
|
|
Debug( 1, "Found encoder for '%s'", avcodec_get_name( codec_id ) );
|
2005-10-04 04:38:40 +08:00
|
|
|
#else
|
2014-04-26 04:12:58 +08:00
|
|
|
Debug( 1, "Found encoder for '%d'", codec_id );
|
2005-10-04 04:38:40 +08:00
|
|
|
#endif
|
|
|
|
|
2015-05-29 23:38:02 +08:00
|
|
|
#if LIBAVFORMAT_VERSION_CHECK(53, 10, 0, 17, 0)
|
2014-04-26 04:12:58 +08:00
|
|
|
ost = avformat_new_stream( ofc, codec );
|
2011-05-16 04:38:51 +08:00
|
|
|
#else
|
2014-04-26 04:12:58 +08:00
|
|
|
ost = av_new_stream( ofc, 0 );
|
2011-05-16 04:38:51 +08:00
|
|
|
#endif
|
2014-04-26 04:12:58 +08:00
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( !ost ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
Fatal( "Could not alloc stream" );
|
2017-12-13 02:25:41 +08:00
|
|
|
return;
|
2014-04-26 04:12:58 +08:00
|
|
|
}
|
2018-02-01 03:35:00 +08:00
|
|
|
Debug( 1, "Allocated stream (%d) !=? (%d)", ost->id , ofc->nb_streams - 1 );
|
2014-04-26 04:12:58 +08:00
|
|
|
ost->id = ofc->nb_streams - 1;
|
|
|
|
|
2017-11-08 10:21:51 +08:00
|
|
|
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
|
2018-02-01 03:35:00 +08:00
|
|
|
|
|
|
|
codec_context = avcodec_alloc_context3(NULL);
|
|
|
|
//avcodec_parameters_to_context(codec_context, ost->codecpar);
|
2017-11-08 10:21:51 +08:00
|
|
|
#else
|
|
|
|
codec_context = ost->codec;
|
|
|
|
#endif
|
2014-04-26 04:12:58 +08:00
|
|
|
|
2017-11-08 10:21:51 +08:00
|
|
|
codec_context->codec_id = codec->id;
|
|
|
|
codec_context->codec_type = codec->type;
|
2014-04-26 04:12:58 +08:00
|
|
|
|
2018-02-01 03:35:00 +08:00
|
|
|
codec_context->pix_fmt = strcmp("mjpeg", ofc->oformat->name) == 0 ? AV_PIX_FMT_YUVJ422P : AV_PIX_FMT_YUV420P;
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( bitrate <= 100 ) {
|
2014-04-26 04:12:58 +08: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 10:21:51 +08:00
|
|
|
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
|
|
|
|
codec_context->flags |= AV_CODEC_FLAG_QSCALE;
|
|
|
|
#else
|
|
|
|
codec_context->flags |= CODEC_FLAG_QSCALE;
|
|
|
|
#endif
|
|
|
|
codec_context->global_quality = FF_QP2LAMBDA * (31 - (31 * (bitrate / 100.0)));
|
2017-01-12 03:18:11 +08:00
|
|
|
} else {
|
2017-11-08 10:21:51 +08:00
|
|
|
codec_context->bit_rate = bitrate;
|
2014-04-26 04:12:58 +08:00
|
|
|
}
|
2004-03-04 23:05:54 +08:00
|
|
|
|
|
|
|
/* resolution must be a multiple of two */
|
2017-11-08 10:21:51 +08:00
|
|
|
codec_context->width = width;
|
|
|
|
codec_context->height = height;
|
2005-10-04 04:38:40 +08: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 10:21:51 +08:00
|
|
|
codec_context->time_base.den = frame_rate;
|
|
|
|
codec_context->time_base.num = 1;
|
2018-02-01 03:35:00 +08:00
|
|
|
ost->time_base.den = frame_rate;
|
|
|
|
ost->time_base.num = 1;
|
|
|
|
|
2014-04-26 04:12:58 +08:00
|
|
|
|
2018-02-01 03:35:00 +08:00
|
|
|
Debug( 1, "Will encode in %d fps. %dx%d", codec_context->time_base.den, width, height );
|
2014-04-26 04:12:58 +08:00
|
|
|
|
|
|
|
/* emit one intra frame every second */
|
2017-11-08 10:21:51 +08:00
|
|
|
codec_context->gop_size = frame_rate;
|
2014-04-26 04:12:58 +08:00
|
|
|
|
2015-04-19 18:38:23 +08:00
|
|
|
// some formats want stream headers to be separate
|
2014-04-26 04:12:58 +08:00
|
|
|
if ( of->flags & AVFMT_GLOBALHEADER )
|
2017-11-08 10:21:51 +08:00
|
|
|
#if LIBAVCODEC_VERSION_CHECK(56, 35, 0, 64, 0)
|
|
|
|
codec_context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
|
|
|
|
#else
|
|
|
|
codec_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
|
2018-02-01 03:35:00 +08:00
|
|
|
#endif
|
|
|
|
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
|
|
|
|
avcodec_parameters_from_context(ost->codecpar, codec_context);
|
|
|
|
zm_dump_codecpar(ost->codecpar);
|
2017-11-08 10:21:51 +08:00
|
|
|
#endif
|
2017-01-12 03:18:11 +08:00
|
|
|
} else {
|
2014-10-12 04:38:38 +08:00
|
|
|
Fatal( "of->video_codec == AV_CODEC_ID_NONE" );
|
2014-04-26 04:12:58 +08:00
|
|
|
}
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
void VideoStream::SetParameters( ) {
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
const char *VideoStream::MimeType( ) const {
|
|
|
|
for ( unsigned int i = 0; i < sizeof (mime_data) / sizeof (*mime_data); i++ ) {
|
|
|
|
if ( strcmp( format, mime_data[i].format ) == 0 ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
Debug( 1, "MimeType is \"%s\"", mime_data[i].mime_type );
|
2018-02-01 03:35:00 +08:00
|
|
|
return mime_data[i].mime_type;
|
2005-12-07 21:42:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const char *mime_type = of->mime_type;
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( !mime_type ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
std::string mime = "video/";
|
|
|
|
mime = mime.append( format );
|
|
|
|
mime_type = mime.c_str( );
|
2008-07-14 22:43:47 +08:00
|
|
|
Warning( "Unable to determine mime type for '%s' format, using '%s' as default", format, mime_type );
|
2005-12-07 21:42:25 +08:00
|
|
|
}
|
|
|
|
|
2018-02-01 03:35:00 +08:00
|
|
|
Debug(1, "MimeType is \"%s\"", mime_type );
|
2014-04-26 04:12:58 +08:00
|
|
|
|
2018-02-01 03:35:00 +08:00
|
|
|
return mime_type;
|
2005-12-07 21:42:25 +08:00
|
|
|
}
|
|
|
|
|
2018-04-12 23:29:35 +08:00
|
|
|
bool VideoStream::OpenStream( ) {
|
2017-11-17 20:52:26 +08:00
|
|
|
int ret;
|
2014-04-26 04:12:58 +08:00
|
|
|
|
2004-03-04 23:05:54 +08:00
|
|
|
/* now that all the parameters are set, we can open the
|
|
|
|
video codecs and allocate the necessary encode buffers */
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( ost ) {
|
2018-02-01 03:35:00 +08:00
|
|
|
Debug(1,"Opening codec");
|
|
|
|
|
2004-03-04 23:05:54 +08:00
|
|
|
/* open the codec */
|
2015-05-29 23:38:02 +08:00
|
|
|
#if !LIBAVFORMAT_VERSION_CHECK(53, 8, 0, 8, 0)
|
2018-02-01 03:35:00 +08:00
|
|
|
if ( (ret = avcodec_open(codec_context, codec)) < 0 )
|
2013-03-17 07:45:21 +08:00
|
|
|
#else
|
2018-02-01 03:35:00 +08:00
|
|
|
if ( (ret = avcodec_open2(codec_context, codec, 0)) < 0 )
|
2013-03-17 07:45:21 +08:00
|
|
|
#endif
|
2004-03-04 23:05:54 +08:00
|
|
|
{
|
2018-04-12 23:29:35 +08:00
|
|
|
Error("Could not open codec. Error code %d \"%s\"", ret, av_err2str(ret));
|
|
|
|
return false;
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
|
|
|
|
2014-04-26 04:12:58 +08:00
|
|
|
Debug( 1, "Opened codec" );
|
|
|
|
|
2004-03-04 23:05:54 +08:00
|
|
|
/* allocate the encoded raw picture */
|
2017-11-08 10:21:51 +08:00
|
|
|
opicture = zm_av_frame_alloc( );
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( !opicture ) {
|
2018-04-12 23:29:35 +08:00
|
|
|
Error("Could not allocate opicture");
|
|
|
|
return false;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2018-02-01 03:35:00 +08:00
|
|
|
opicture->width = codec_context->width;
|
|
|
|
opicture->height = codec_context->height;
|
|
|
|
opicture->format = codec_context->pix_fmt;
|
2016-04-29 19:27:28 +08:00
|
|
|
|
2016-03-02 22:03:55 +08:00
|
|
|
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
|
2018-04-12 23:29:35 +08:00
|
|
|
int size = av_image_get_buffer_size(codec_context->pix_fmt, codec_context->width, codec_context->height, 1);
|
2016-03-02 22:03:55 +08:00
|
|
|
#else
|
2018-04-12 23:29:35 +08:00
|
|
|
int size = avpicture_get_size(codec_context->pix_fmt, codec_context->width, codec_context->height);
|
2016-03-02 22:03:55 +08:00
|
|
|
#endif
|
|
|
|
|
2018-04-12 23:29:35 +08:00
|
|
|
uint8_t *opicture_buf = (uint8_t *)av_malloc(size);
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( !opicture_buf ) {
|
|
|
|
av_frame_free( &opicture );
|
2018-04-12 23:29:35 +08:00
|
|
|
Error( "Could not allocate opicture_buf" );
|
|
|
|
return false;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
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
|
|
|
av_image_fill_arrays(opicture->data, opicture->linesize,
|
2017-11-08 10:21:51 +08:00
|
|
|
opicture_buf, codec_context->pix_fmt, codec_context->width, codec_context->height, 1);
|
2016-03-02 22:03:55 +08:00
|
|
|
#else
|
2017-11-08 10:21:51 +08:00
|
|
|
avpicture_fill( (AVPicture *)opicture, opicture_buf, codec_context->pix_fmt,
|
|
|
|
codec_context->width, codec_context->height );
|
2016-03-02 22:03:55 +08:00
|
|
|
#endif
|
2004-03-04 23:05:54 +08:00
|
|
|
|
2016-04-04 22:11:48 +08: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 */
|
|
|
|
tmp_opicture = NULL;
|
2017-11-08 10:21:51 +08:00
|
|
|
if ( codec_context->pix_fmt != pf ) {
|
2015-05-29 23:38:02 +08:00
|
|
|
#if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101)
|
|
|
|
tmp_opicture = av_frame_alloc( );
|
|
|
|
#else
|
2014-04-26 04:12:58 +08:00
|
|
|
tmp_opicture = avcodec_alloc_frame( );
|
2015-05-29 23:38:02 +08:00
|
|
|
#endif
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( !tmp_opicture ) {
|
2018-04-12 23:29:35 +08:00
|
|
|
Error( "Could not allocate tmp_opicture" );
|
|
|
|
return false;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2016-03-02 22:03:55 +08:00
|
|
|
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
|
2017-11-08 10:21:51 +08:00
|
|
|
int size = av_image_get_buffer_size( pf, codec_context->width, codec_context->height,1 );
|
2016-03-02 22:03:55 +08:00
|
|
|
#else
|
2017-11-08 10:21:51 +08:00
|
|
|
int size = avpicture_get_size( pf, codec_context->width, codec_context->height );
|
2016-03-02 22:03:55 +08:00
|
|
|
#endif
|
2016-04-04 22:11:48 +08:00
|
|
|
uint8_t *tmp_opicture_buf = (uint8_t *)av_malloc( size );
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( !tmp_opicture_buf ) {
|
2016-05-04 02:34:58 +08:00
|
|
|
av_frame_free( &tmp_opicture );
|
2018-04-12 23:29:35 +08:00
|
|
|
Error( "Could not allocate tmp_opicture_buf" );
|
|
|
|
return false;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
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
|
|
|
av_image_fill_arrays(tmp_opicture->data,
|
|
|
|
tmp_opicture->linesize, tmp_opicture_buf, pf,
|
2017-11-08 10:21:51 +08:00
|
|
|
codec_context->width, codec_context->height, 1);
|
2016-03-02 22:03:55 +08:00
|
|
|
#else
|
2016-04-29 19:27:28 +08:00
|
|
|
avpicture_fill( (AVPicture *)tmp_opicture,
|
2017-11-08 10:21:51 +08:00
|
|
|
tmp_opicture_buf, pf, codec_context->width, codec_context->height );
|
2016-03-02 22:03:55 +08:00
|
|
|
#endif
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2018-02-01 03:35:00 +08:00
|
|
|
} // end if ost
|
2004-03-04 23:05:54 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
/* open the output file, if needed */
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( !(of->flags & AVFMT_NOFILE) ) {
|
2015-05-29 23:38:02 +08:00
|
|
|
#if LIBAVFORMAT_VERSION_CHECK(53, 15, 0, 21, 0)
|
2014-04-26 04:12:58 +08:00
|
|
|
ret = avio_open2( &ofc->pb, filename, AVIO_FLAG_WRITE, NULL, NULL );
|
2015-05-29 23:38:02 +08:00
|
|
|
#elif LIBAVFORMAT_VERSION_CHECK(52, 102, 0, 102, 0)
|
2014-04-26 04:12:58 +08:00
|
|
|
ret = avio_open( &ofc->pb, filename, AVIO_FLAG_WRITE );
|
2011-05-16 04:38:51 +08:00
|
|
|
#else
|
2014-04-26 04:12:58 +08:00
|
|
|
ret = url_fopen( &ofc->pb, filename, AVIO_FLAG_WRITE );
|
2011-05-16 04:38:51 +08:00
|
|
|
#endif
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( ret < 0 ) {
|
2018-04-12 23:29:35 +08:00
|
|
|
Error("Could not open '%s'", filename);
|
|
|
|
return false;
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
2014-04-26 04:12:58 +08:00
|
|
|
|
2018-04-12 23:29:35 +08:00
|
|
|
Debug(1, "Opened output \"%s\"", filename);
|
2017-01-12 03:18:11 +08:00
|
|
|
} else {
|
2018-04-12 23:29:35 +08:00
|
|
|
Error( "of->flags & AVFMT_NOFILE" );
|
|
|
|
return false;
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
video_outbuf = NULL;
|
2017-11-08 10:21:51 +08:00
|
|
|
#if LIBAVFORMAT_VERSION_CHECK(57, 0, 0, 0, 0)
|
2018-02-01 03:35:00 +08:00
|
|
|
if (codec_context->codec_type == AVMEDIA_TYPE_VIDEO &&
|
|
|
|
codec_context->codec_id == AV_CODEC_ID_RAWVIDEO) {
|
2017-11-08 10:21:51 +08:00
|
|
|
#else
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( !(of->flags & AVFMT_RAWPICTURE) ) {
|
2017-11-08 10:21:51 +08:00
|
|
|
#endif
|
2004-03-04 23:05:54 +08:00
|
|
|
/* allocate output buffer */
|
|
|
|
/* XXX: API change will be done */
|
2014-04-26 04:12:58 +08:00
|
|
|
// TODO: Make buffer dynamic.
|
|
|
|
video_outbuf_size = 4000000;
|
|
|
|
video_outbuf = (uint8_t *)malloc( video_outbuf_size );
|
2015-01-30 04:32:50 +08:00
|
|
|
if ( video_outbuf == NULL ) {
|
|
|
|
Fatal("Unable to malloc memory for outbuf");
|
|
|
|
}
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
2015-05-29 23:38:02 +08:00
|
|
|
|
|
|
|
#if LIBAVFORMAT_VERSION_CHECK(52, 101, 0, 101, 0)
|
2014-04-26 04:12:58 +08:00
|
|
|
av_dump_format(ofc, 0, filename, 1);
|
|
|
|
#else
|
|
|
|
dump_format(ofc, 0, filename, 1);
|
|
|
|
#endif
|
2015-05-29 23:38:02 +08:00
|
|
|
|
|
|
|
#if !LIBAVFORMAT_VERSION_CHECK(53, 2, 0, 4, 0)
|
2018-04-12 23:29:35 +08:00
|
|
|
ret = av_write_header(ofc);
|
2013-03-17 07:45:21 +08:00
|
|
|
#else
|
2018-04-12 23:29:35 +08:00
|
|
|
ret = avformat_write_header(ofc, NULL);
|
2013-03-17 07:45:21 +08:00
|
|
|
#endif
|
2015-05-29 23:38:02 +08:00
|
|
|
|
2018-04-12 23:29:35 +08:00
|
|
|
if ( ret < 0 ) {
|
|
|
|
Error("?_write_header failed with error %d \"%s\"", ret, av_err2str(ret));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
|
|
|
|
2014-04-26 04:12:58 +08: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),
|
2017-11-19 05:00:10 +08:00
|
|
|
opicture(NULL),
|
|
|
|
tmp_opicture(NULL),
|
|
|
|
video_outbuf(NULL),
|
|
|
|
video_outbuf_size(0),
|
2014-04-26 04:12:58 +08:00
|
|
|
last_pts( -1 ),
|
|
|
|
streaming_thread(0),
|
|
|
|
do_streaming(true),
|
2017-11-19 05:00:10 +08:00
|
|
|
add_timestamp(false),
|
|
|
|
timestamp(0),
|
2014-04-26 04:12:58 +08:00
|
|
|
buffer_copy(NULL),
|
|
|
|
buffer_copy_lock(new pthread_mutex_t),
|
2015-02-05 09:19:45 +08:00
|
|
|
buffer_copy_size(0),
|
2014-04-27 00:25:48 +08:00
|
|
|
buffer_copy_used(0),
|
2017-01-12 03:18:11 +08:00
|
|
|
packet_index(0)
|
2004-03-04 23:05:54 +08:00
|
|
|
{
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( !initialised ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
Initialise( );
|
|
|
|
}
|
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( format ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
int length = strlen(format);
|
|
|
|
codec_and_format = new char[length+1];;
|
|
|
|
strcpy( codec_and_format, format );
|
|
|
|
format = codec_and_format;
|
2014-10-16 02:35:01 +08:00
|
|
|
codec_name = NULL;
|
2014-04-26 04:12:58 +08:00
|
|
|
char *f = strchr(codec_and_format, '/');
|
2017-01-12 03:18:11 +08:00
|
|
|
if (f != NULL) {
|
2014-04-26 04:12:58 +08:00
|
|
|
*f = 0;
|
|
|
|
codec_name = f+1;
|
|
|
|
}
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
|
|
|
|
2018-02-01 03:35:00 +08:00
|
|
|
codec_context = NULL;
|
2014-04-26 04:12:58 +08:00
|
|
|
SetupFormat( );
|
2011-06-22 23:38:35 +08:00
|
|
|
SetupCodec( colours, subpixelorder, width, height, bitrate, frame_rate );
|
2014-04-26 04:12:58 +08:00
|
|
|
SetParameters( );
|
2014-04-27 00:25:48 +08:00
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
// Allocate buffered packets.
|
|
|
|
packet_buffers = new AVPacket*[2];
|
|
|
|
packet_buffers[0] = new AVPacket();
|
|
|
|
packet_buffers[1] = new AVPacket();
|
|
|
|
packet_index = 0;
|
2014-04-26 04:12:58 +08:00
|
|
|
|
|
|
|
// Initialize mutex used by streaming thread.
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( pthread_mutex_init( buffer_copy_lock, NULL ) != 0 ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
Fatal("pthread_mutex_init failed");
|
|
|
|
}
|
2017-11-08 10:21:51 +08:00
|
|
|
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
VideoStream::~VideoStream( ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
Debug( 1, "VideoStream destructor." );
|
|
|
|
|
|
|
|
// Stop streaming thread.
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( streaming_thread ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
do_streaming = false;
|
|
|
|
void* thread_exit_code;
|
|
|
|
|
|
|
|
Debug( 1, "Asking streaming thread to exit." );
|
|
|
|
|
|
|
|
// Wait for thread to exit.
|
|
|
|
pthread_join(streaming_thread, &thread_exit_code);
|
|
|
|
}
|
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( buffer_copy != NULL ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
av_free( buffer_copy );
|
|
|
|
}
|
2014-04-27 00:25:48 +08:00
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( buffer_copy_lock ) {
|
|
|
|
if ( pthread_mutex_destroy( buffer_copy_lock ) != 0 ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
Error( "pthread_mutex_destroy failed" );
|
|
|
|
}
|
|
|
|
delete buffer_copy_lock;
|
|
|
|
}
|
2014-04-27 00:25:48 +08:00
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
if (packet_buffers) {
|
|
|
|
delete packet_buffers[0];
|
|
|
|
delete packet_buffers[1];
|
|
|
|
delete[] packet_buffers;
|
|
|
|
}
|
2014-04-26 04:12:58 +08:00
|
|
|
|
2004-03-04 23:05:54 +08:00
|
|
|
/* close each codec */
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( ost ) {
|
2017-11-08 10:21:51 +08:00
|
|
|
avcodec_close( codec_context );
|
2014-04-26 04:12:58 +08:00
|
|
|
av_free( opicture->data[0] );
|
2015-11-03 08:58:23 +08:00
|
|
|
av_frame_free( &opicture );
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( tmp_opicture ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
av_free( tmp_opicture->data[0] );
|
2015-11-03 08:58:23 +08:00
|
|
|
av_frame_free( &tmp_opicture );
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
2014-04-26 04:12:58 +08:00
|
|
|
av_free( video_outbuf );
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* write the trailer, if any */
|
2014-04-26 04:12:58 +08:00
|
|
|
av_write_trailer( ofc );
|
|
|
|
|
2004-03-04 23:05:54 +08:00
|
|
|
/* free the streams */
|
2017-01-12 03:18:11 +08:00
|
|
|
for ( unsigned int i = 0; i < ofc->nb_streams; i++ ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
av_freep( &ofc->streams[i] );
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( !(of->flags & AVFMT_NOFILE) ) {
|
2004-03-04 23:05:54 +08:00
|
|
|
/* close the output file */
|
2015-05-29 23:38:02 +08:00
|
|
|
#if LIBAVFORMAT_VERSION_CHECK(52, 105, 0, 105, 0)
|
2014-04-26 04:12:58 +08:00
|
|
|
avio_close( ofc->pb );
|
2011-05-16 04:38:51 +08:00
|
|
|
#else
|
2014-04-26 04:12:58 +08:00
|
|
|
url_fclose( ofc->pb );
|
2007-12-17 21:17:09 +08:00
|
|
|
#endif
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* free the stream */
|
2014-04-26 04:12:58 +08:00
|
|
|
av_free( ofc );
|
|
|
|
|
|
|
|
/* free format and codec_name data. */
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( codec_and_format ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
delete codec_and_format;
|
|
|
|
}
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
double VideoStream::EncodeFrame( const uint8_t *buffer, int buffer_size, bool _add_timestamp, unsigned int _timestamp ) {
|
2018-04-12 23:29:35 +08:00
|
|
|
if ( pthread_mutex_lock(buffer_copy_lock) != 0 ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
Fatal( "EncodeFrame: pthread_mutex_lock failed." );
|
|
|
|
}
|
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
if (buffer_copy_size < buffer_size) {
|
|
|
|
if ( buffer_copy ) {
|
2018-04-12 23:29:35 +08:00
|
|
|
av_free(buffer_copy);
|
2014-04-26 04:12:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate a buffer to store source images for the streaming thread to encode.
|
2018-04-12 23:29:35 +08:00
|
|
|
buffer_copy = (uint8_t *)av_malloc(buffer_size);
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( !buffer_copy ) {
|
2018-04-12 23:29:35 +08:00
|
|
|
Error( "Could not allocate buffer_copy" );
|
|
|
|
pthread_mutex_unlock(buffer_copy_lock);
|
2017-12-13 02:19:23 +08:00
|
|
|
return 0;
|
2014-04-26 04:12:58 +08:00
|
|
|
}
|
|
|
|
buffer_copy_size = buffer_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_timestamp = _add_timestamp;
|
|
|
|
timestamp = _timestamp;
|
|
|
|
buffer_copy_used = buffer_size;
|
|
|
|
memcpy(buffer_copy, buffer, buffer_size);
|
|
|
|
|
2018-04-12 23:29:35 +08:00
|
|
|
if ( pthread_mutex_unlock(buffer_copy_lock) != 0 ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
Fatal( "EncodeFrame: pthread_mutex_unlock failed." );
|
2004-03-08 18:33:19 +08:00
|
|
|
}
|
2014-04-26 04:12:58 +08:00
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( streaming_thread == 0 ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
Debug( 1, "Starting streaming thread" );
|
|
|
|
|
|
|
|
// Start a thread for streaming encoded video.
|
|
|
|
if (pthread_create( &streaming_thread, NULL, StreamingThreadCallback, (void*) this) != 0){
|
|
|
|
// Log a fatal error and exit the process.
|
|
|
|
Fatal( "VideoStream failed to create streaming thread." );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//return ActuallyEncodeFrame( buffer, buffer_size, add_timestamp, timestamp);
|
|
|
|
|
|
|
|
return _timestamp;
|
|
|
|
}
|
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
double VideoStream::ActuallyEncodeFrame( const uint8_t *buffer, int buffer_size, bool add_timestamp, unsigned int timestamp ) {
|
2017-11-19 05:00:10 +08:00
|
|
|
|
|
|
|
if ( codec_context->pix_fmt != pf ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
#ifdef HAVE_LIBSWSCALE
|
|
|
|
static struct SwsContext *img_convert_ctx = 0;
|
|
|
|
#endif // HAVE_LIBSWSCALE
|
2004-03-04 23:05:54 +08:00
|
|
|
memcpy( tmp_opicture->data[0], buffer, buffer_size );
|
2007-09-18 22:00:33 +08:00
|
|
|
#ifdef HAVE_LIBSWSCALE
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( !img_convert_ctx ) {
|
2017-11-08 10:21:51 +08:00
|
|
|
img_convert_ctx = sws_getCachedContext( NULL, codec_context->width, codec_context->height, pf, codec_context->width, codec_context->height, codec_context->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL );
|
2014-04-26 04:12:58 +08:00
|
|
|
if ( !img_convert_ctx )
|
|
|
|
Panic( "Unable to initialise image scaling context" );
|
|
|
|
}
|
2017-11-08 10:21:51 +08:00
|
|
|
sws_scale( img_convert_ctx, tmp_opicture->data, tmp_opicture->linesize, 0, codec_context->height, opicture->data, opicture->linesize );
|
2007-09-18 22:00:33 +08:00
|
|
|
#else // HAVE_LIBSWSCALE
|
2014-04-26 04:12:58 +08:00
|
|
|
Fatal( "swscale is required for MPEG mode" );
|
2007-09-18 22:00:33 +08:00
|
|
|
#endif // HAVE_LIBSWSCALE
|
2017-01-12 03:18:11 +08:00
|
|
|
} else {
|
2004-03-04 23:05:54 +08:00
|
|
|
memcpy( opicture->data[0], buffer, buffer_size );
|
|
|
|
}
|
|
|
|
AVFrame *opicture_ptr = opicture;
|
2014-04-26 04:12:58 +08:00
|
|
|
|
2014-04-27 00:25:48 +08:00
|
|
|
AVPacket *pkt = packet_buffers[packet_index];
|
|
|
|
av_init_packet( pkt );
|
2017-01-12 03:18:11 +08:00
|
|
|
int got_packet = 0;
|
2017-11-08 10:21:51 +08:00
|
|
|
#if LIBAVFORMAT_VERSION_CHECK(57, 0, 0, 0, 0)
|
|
|
|
if (codec_context->codec_type == AVMEDIA_TYPE_VIDEO &&
|
|
|
|
codec_context->codec_id == AV_CODEC_ID_RAWVIDEO) {
|
|
|
|
#else
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( of->flags & AVFMT_RAWPICTURE ) {
|
2017-11-08 10:21:51 +08:00
|
|
|
#endif
|
|
|
|
|
2015-05-29 23:38:02 +08:00
|
|
|
#if LIBAVCODEC_VERSION_CHECK(52, 30, 2, 30, 2)
|
2014-04-27 00:25:48 +08:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2011-05-16 04:38:51 +08:00
|
|
|
#else
|
2014-04-27 00:25:48 +08:00
|
|
|
pkt->flags |= PKT_FLAG_KEY;
|
2004-09-23 22:20:54 +08:00
|
|
|
#endif
|
2014-04-27 00:25:48 +08:00
|
|
|
pkt->stream_index = ost->index;
|
|
|
|
pkt->data = (uint8_t *)opicture_ptr;
|
|
|
|
pkt->size = sizeof (AVPicture);
|
2017-01-12 03:18:11 +08:00
|
|
|
got_packet = 1;
|
|
|
|
} else {
|
2017-11-08 10:21:51 +08:00
|
|
|
opicture_ptr->pts = codec_context->frame_number;
|
|
|
|
opicture_ptr->quality = codec_context->global_quality;
|
|
|
|
|
|
|
|
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
|
|
|
|
avcodec_send_frame(codec_context, opicture_ptr);
|
|
|
|
int ret = avcodec_receive_packet(codec_context, pkt);
|
|
|
|
if ( ret < 0 ) {
|
|
|
|
if ( AVERROR_EOF != ret ) {
|
|
|
|
Error("ERror encoding video (%d) (%s)", ret,
|
|
|
|
av_err2str(ret));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
got_packet = 1;
|
|
|
|
}
|
|
|
|
#else
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2015-05-29 23:38:02 +08:00
|
|
|
#if LIBAVFORMAT_VERSION_CHECK(54, 1, 0, 2, 100)
|
2017-11-08 10:21:51 +08:00
|
|
|
int ret = avcodec_encode_video2( codec_context, pkt, opicture_ptr, &got_packet );
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( ret != 0 ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
Fatal( "avcodec_encode_video2 failed with errorcode %d \"%s\"", ret, av_err2str( ret ) );
|
|
|
|
}
|
2005-12-09 08:25:12 +08:00
|
|
|
#else
|
2017-11-08 10:21:51 +08:00
|
|
|
int out_size = avcodec_encode_video( codec_context, video_outbuf, video_outbuf_size, opicture_ptr );
|
2014-04-26 04:12:58 +08:00
|
|
|
got_packet = out_size > 0 ? 1 : 0;
|
2014-04-27 00:25:48 +08:00
|
|
|
pkt->data = got_packet ? video_outbuf : NULL;
|
|
|
|
pkt->size = got_packet ? out_size : 0;
|
2017-11-08 10:21:51 +08:00
|
|
|
#endif
|
2005-12-09 08:25:12 +08:00
|
|
|
#endif
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( got_packet ) {
|
2016-04-29 19:27:28 +08:00
|
|
|
// if ( c->coded_frame->key_frame )
|
|
|
|
// {
|
2016-03-03 14:16:06 +08:00
|
|
|
//#if LIBAVCODEC_VERSION_CHECK(52, 30, 2, 30, 2)
|
2016-04-29 19:27:28 +08:00
|
|
|
// pkt->flags |= AV_PKT_FLAG_KEY;
|
2016-03-03 14:16:06 +08:00
|
|
|
//#else
|
2016-04-29 19:27:28 +08:00
|
|
|
// pkt->flags |= PKT_FLAG_KEY;
|
2016-03-03 14:16:06 +08:00
|
|
|
//#endif
|
2016-04-29 19:27:28 +08:00
|
|
|
// }
|
2016-04-04 22:11:48 +08:00
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( pkt->pts != (int64_t)AV_NOPTS_VALUE ) {
|
2017-11-08 10:21:51 +08:00
|
|
|
pkt->pts = av_rescale_q( pkt->pts, codec_context->time_base, ost->time_base );
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( pkt->dts != (int64_t)AV_NOPTS_VALUE ) {
|
2017-11-08 10:21:51 +08:00
|
|
|
pkt->dts = av_rescale_q( pkt->dts, codec_context->time_base, ost->time_base );
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2017-11-08 10:21:51 +08:00
|
|
|
pkt->duration = av_rescale_q( pkt->duration, codec_context->time_base, ost->time_base );
|
2016-04-04 22:11:48 +08:00
|
|
|
pkt->stream_index = ost->index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ( opicture_ptr->pts);
|
2014-04-26 04:12:58 +08:00
|
|
|
}
|
|
|
|
|
2014-04-27 00:25:48 +08:00
|
|
|
int VideoStream::SendPacket(AVPacket *packet) {
|
|
|
|
|
|
|
|
int ret = av_write_frame( ofc, packet );
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( ret != 0 ) {
|
2014-04-27 00:25:48 +08:00
|
|
|
Fatal( "Error %d while writing video frame: %s", ret, av_err2str( errno ) );
|
|
|
|
}
|
2015-11-04 12:30:14 +08:00
|
|
|
#if LIBAVCODEC_VERSION_CHECK(57, 8, 0, 12, 100)
|
2017-01-12 03:18:11 +08:00
|
|
|
av_packet_unref( packet );
|
2015-11-04 12:30:14 +08:00
|
|
|
#else
|
2017-01-12 03:18:11 +08:00
|
|
|
av_free_packet( packet );
|
2015-11-04 12:30:14 +08:00
|
|
|
#endif
|
2014-04-27 00:25:48 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-26 04:12:58 +08:00
|
|
|
void *VideoStream::StreamingThreadCallback(void *ctx){
|
|
|
|
|
|
|
|
Debug( 1, "StreamingThreadCallback started" );
|
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
if (ctx == NULL) return NULL;
|
|
|
|
|
|
|
|
VideoStream* videoStream = reinterpret_cast<VideoStream*>(ctx);
|
2014-04-26 04:12:58 +08:00
|
|
|
|
|
|
|
const uint64_t nanosecond_multiplier = 1000000000;
|
2017-11-08 10:21:51 +08:00
|
|
|
|
|
|
|
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
|
|
|
|
uint64_t target_interval_ns = nanosecond_multiplier * ( ((double)videoStream->codec_context->time_base.num) / (videoStream->codec_context->time_base.den) );
|
|
|
|
#else
|
|
|
|
uint64_t target_interval_ns = nanosecond_multiplier * ( ((double)videoStream->codec_context->time_base.num) / (videoStream->codec_context->time_base.den) );
|
|
|
|
#endif
|
2014-04-26 04:12:58 +08:00
|
|
|
uint64_t frame_count = 0;
|
|
|
|
timespec start_time;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &start_time);
|
|
|
|
uint64_t start_time_ns = (start_time.tv_sec*nanosecond_multiplier) + start_time.tv_nsec;
|
2017-01-12 03:18:11 +08:00
|
|
|
while(videoStream->do_streaming) {
|
2014-04-26 04:12:58 +08:00
|
|
|
timespec current_time;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, ¤t_time);
|
|
|
|
uint64_t current_time_ns = (current_time.tv_sec*nanosecond_multiplier) + current_time.tv_nsec;
|
|
|
|
uint64_t target_ns = start_time_ns + (target_interval_ns * frame_count);
|
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( current_time_ns < target_ns ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
// It's not time to render a frame yet.
|
|
|
|
usleep( (target_ns - current_time_ns) * 0.001 );
|
|
|
|
}
|
2017-01-12 03:18:11 +08: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);
|
|
|
|
}
|
2015-11-04 12:30:14 +08:00
|
|
|
#if LIBAVCODEC_VERSION_CHECK(57, 8, 0, 12, 100)
|
2017-01-12 03:18:11 +08:00
|
|
|
av_packet_unref( packet);
|
2015-11-04 12:30:14 +08:00
|
|
|
#else
|
2017-01-12 03:18:11 +08:00
|
|
|
av_free_packet( packet );
|
2015-11-04 12:30:14 +08:00
|
|
|
#endif
|
2017-01-12 03:18:11 +08:00
|
|
|
videoStream->packet_index = videoStream->packet_index ? 0 : 1;
|
|
|
|
|
|
|
|
// Lock buffer and render next frame.
|
2014-04-27 00:25:48 +08:00
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( pthread_mutex_lock( videoStream->buffer_copy_lock ) != 0 ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
Fatal( "StreamingThreadCallback: pthread_mutex_lock failed." );
|
|
|
|
}
|
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( videoStream->buffer_copy ) {
|
2014-04-27 00:25:48 +08:00
|
|
|
// Encode next frame.
|
2014-04-26 04:12:58 +08:00
|
|
|
videoStream->ActuallyEncodeFrame( videoStream->buffer_copy, videoStream->buffer_copy_used, videoStream->add_timestamp, videoStream->timestamp );
|
|
|
|
}
|
|
|
|
|
2017-01-12 03:18:11 +08:00
|
|
|
if ( pthread_mutex_unlock( videoStream->buffer_copy_lock ) != 0 ) {
|
2014-04-26 04:12:58 +08:00
|
|
|
Fatal( "StreamingThreadCallback: pthread_mutex_unlock failed." );
|
|
|
|
}
|
|
|
|
|
|
|
|
frame_count++;
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
2014-04-26 04:12:58 +08:00
|
|
|
|
|
|
|
return 0;
|
2004-03-04 23:05:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HAVE_LIBAVCODEC
|