Merge branch 'Sune1337-zms/videostream-improvements'
This commit is contained in:
commit
f4b248a711
|
@ -133,6 +133,7 @@ int SWScale::Convert(const uint8_t* in_buffer, const size_t in_buffer_size, uint
|
||||||
return -3;
|
return -3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if LIBSWSCALE_VERSION_INT >= AV_VERSION_INT(0, 8, 0)
|
||||||
/* Warn if the input or output pixelformat is not supported */
|
/* Warn if the input or output pixelformat is not supported */
|
||||||
if(!sws_isSupportedInput(in_pf)) {
|
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));
|
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));
|
||||||
|
@ -140,6 +141,7 @@ int SWScale::Convert(const uint8_t* in_buffer, const size_t in_buffer_size, uint
|
||||||
if(!sws_isSupportedOutput(out_pf)) {
|
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));
|
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));
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Check the buffer sizes */
|
/* Check the buffer sizes */
|
||||||
size_t insize = avpicture_get_size(in_pf, width, height);
|
size_t insize = avpicture_get_size(in_pf, width, height);
|
||||||
|
|
|
@ -45,6 +45,12 @@ extern "C" {
|
||||||
#include <ffmpeg/avcodec.h>
|
#include <ffmpeg/avcodec.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(HAVE_LIBAVCODEC_AVCODEC_H) && LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54,25,0)
|
||||||
|
#define _AVCODECID AVCodecID
|
||||||
|
#else
|
||||||
|
#define _AVCODECID CodecID
|
||||||
|
#endif
|
||||||
|
|
||||||
// AVFORMAT
|
// AVFORMAT
|
||||||
#if HAVE_LIBAVFORMAT_AVFORMAT_H
|
#if HAVE_LIBAVFORMAT_AVFORMAT_H
|
||||||
#include <libavformat/avformat.h>
|
#include <libavformat/avformat.h>
|
||||||
|
@ -80,18 +86,6 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if FFMPEG_VERSION_INT == 0x000408
|
|
||||||
#define ZM_FFMPEG_048 1
|
|
||||||
#elif FFMPEG_VERSION_INT == 0x000409
|
|
||||||
#if LIBAVCODEC_VERSION_INT < ((50<<16)+(0<<8)+0)
|
|
||||||
#define ZM_FFMPEG_049 1
|
|
||||||
#else // LIBAVCODEC_VERSION_INT
|
|
||||||
#define ZM_FFMPEG_SVN 1
|
|
||||||
#endif // LIBAVCODEC_VERSION_INT
|
|
||||||
#else // FFMPEG_VERSION_INT
|
|
||||||
#define ZM_FFMPEG_SVN 1
|
|
||||||
#endif // FFMPEG_VERSION_INT
|
|
||||||
|
|
||||||
/* Fix for not having SWS_CPU_CAPS_SSE2 defined */
|
/* Fix for not having SWS_CPU_CAPS_SSE2 defined */
|
||||||
#ifndef SWS_CPU_CAPS_SSE2
|
#ifndef SWS_CPU_CAPS_SSE2
|
||||||
#define SWS_CPU_CAPS_SSE2 0x02000000
|
#define SWS_CPU_CAPS_SSE2 0x02000000
|
||||||
|
@ -147,7 +141,36 @@ protected:
|
||||||
#define AV_CODEC_ID_AMR_NB CODEC_ID_AMR_NB
|
#define AV_CODEC_ID_AMR_NB CODEC_ID_AMR_NB
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Some versions of libav does not contain this definition.
|
||||||
|
*/
|
||||||
|
#ifndef AV_ERROR_MAX_STRING_SIZE
|
||||||
|
#define AV_ERROR_MAX_STRING_SIZE 64
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* C++ friendly version of av_err2str taken from http://libav-users.943685.n4.nabble.com/Libav-user-g-4-7-2-fails-to-compile-av-err2str-td4656417.html.
|
||||||
|
* Newer g++ versions fail with "error: taking address of temporary array" when using native libav version.
|
||||||
|
*/
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
inline static const std::string av_make_error_string(int errnum)
|
||||||
|
{
|
||||||
|
char errbuf[AV_ERROR_MAX_STRING_SIZE];
|
||||||
|
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(50, 12, 13)
|
||||||
|
av_strerror(errnum, errbuf, AV_ERROR_MAX_STRING_SIZE);
|
||||||
|
#else
|
||||||
|
snprintf(errbuf, AV_ERROR_MAX_STRING_SIZE, "libav error %d", errnum);
|
||||||
|
#endif
|
||||||
|
return (std::string)errbuf;
|
||||||
|
}
|
||||||
|
|
||||||
|
#undef av_err2str
|
||||||
|
#define av_err2str(errnum) av_make_error_string(errnum).c_str()
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
|
||||||
#endif // ( HAVE_LIBAVUTIL_AVUTIL_H || HAVE_LIBAVCODEC_AVCODEC_H || HAVE_LIBAVFORMAT_AVFORMAT_H || HAVE_LIBAVDEVICE_AVDEVICE_H )
|
#endif // ( HAVE_LIBAVUTIL_AVUTIL_H || HAVE_LIBAVCODEC_AVCODEC_H || HAVE_LIBAVFORMAT_AVFORMAT_H || HAVE_LIBAVDEVICE_AVDEVICE_H )
|
||||||
|
|
||||||
#endif // ZM_FFMPEG_H
|
#endif // ZM_FFMPEG_H
|
||||||
|
|
||||||
|
|
|
@ -81,10 +81,10 @@ FfmpegCamera::~FfmpegCamera()
|
||||||
}
|
}
|
||||||
if ( mFormatContext )
|
if ( mFormatContext )
|
||||||
{
|
{
|
||||||
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(53, 4, 0)
|
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(53, 17, 0)
|
||||||
av_close_input_file( mFormatContext );
|
|
||||||
#else
|
|
||||||
avformat_close_input( &mFormatContext );
|
avformat_close_input( &mFormatContext );
|
||||||
|
#else
|
||||||
|
av_close_input_file( mFormatContext );
|
||||||
#endif
|
#endif
|
||||||
mFormatContext = NULL;
|
mFormatContext = NULL;
|
||||||
}
|
}
|
||||||
|
@ -207,6 +207,7 @@ int FfmpegCamera::PrimeCapture()
|
||||||
}
|
}
|
||||||
|
|
||||||
#if HAVE_LIBSWSCALE
|
#if HAVE_LIBSWSCALE
|
||||||
|
#if LIBSWSCALE_VERSION_INT >= AV_VERSION_INT(0, 8, 0)
|
||||||
if(!sws_isSupportedInput(mCodecContext->pix_fmt)) {
|
if(!sws_isSupportedInput(mCodecContext->pix_fmt)) {
|
||||||
Fatal("swscale does not support the codec format: %c%c%c%c",(mCodecContext->pix_fmt)&0xff,((mCodecContext->pix_fmt>>8)&0xff),((mCodecContext->pix_fmt>>16)&0xff),((mCodecContext->pix_fmt>>24)&0xff));
|
Fatal("swscale does not support the codec format: %c%c%c%c",(mCodecContext->pix_fmt)&0xff,((mCodecContext->pix_fmt>>8)&0xff),((mCodecContext->pix_fmt>>16)&0xff),((mCodecContext->pix_fmt>>24)&0xff));
|
||||||
}
|
}
|
||||||
|
@ -214,6 +215,7 @@ int FfmpegCamera::PrimeCapture()
|
||||||
if(!sws_isSupportedOutput(imagePixFormat)) {
|
if(!sws_isSupportedOutput(imagePixFormat)) {
|
||||||
Fatal("swscale does not support the target format: %c%c%c%c",(imagePixFormat)&0xff,((imagePixFormat>>8)&0xff),((imagePixFormat>>16)&0xff),((imagePixFormat>>24)&0xff));
|
Fatal("swscale does not support the target format: %c%c%c%c",(imagePixFormat)&0xff,((imagePixFormat>>8)&0xff),((imagePixFormat>>16)&0xff),((imagePixFormat>>24)&0xff));
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#else // HAVE_LIBSWSCALE
|
#else // HAVE_LIBSWSCALE
|
||||||
Fatal( "You must compile ffmpeg with the --enable-swscale option to use ffmpeg cameras" );
|
Fatal( "You must compile ffmpeg with the --enable-swscale option to use ffmpeg cameras" );
|
||||||
|
@ -252,7 +254,11 @@ int FfmpegCamera::Capture( Image &image )
|
||||||
Debug( 5, "Got packet from stream %d", packet.stream_index );
|
Debug( 5, "Got packet from stream %d", packet.stream_index );
|
||||||
if ( packet.stream_index == mVideoStreamId )
|
if ( packet.stream_index == mVideoStreamId )
|
||||||
{
|
{
|
||||||
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 25, 0)
|
||||||
if ( avcodec_decode_video2( mCodecContext, mRawFrame, &frameComplete, &packet ) < 0 )
|
if ( avcodec_decode_video2( mCodecContext, mRawFrame, &frameComplete, &packet ) < 0 )
|
||||||
|
#else
|
||||||
|
if ( avcodec_decode_video( mCodecContext, mRawFrame, &frameComplete, packet.data, packet.size ) < 0 )
|
||||||
|
#endif
|
||||||
Fatal( "Unable to decode frame at frame %d", frameCount );
|
Fatal( "Unable to decode frame at frame %d", frameCount );
|
||||||
|
|
||||||
Debug( 4, "Decoded video packet at frame %d", frameCount );
|
Debug( 4, "Decoded video packet at frame %d", frameCount );
|
||||||
|
|
|
@ -54,7 +54,7 @@ static PixelFormat getFfPixFormatFromV4lPalette( int v4l_version, int palette )
|
||||||
{
|
{
|
||||||
switch( palette )
|
switch( palette )
|
||||||
{
|
{
|
||||||
#ifdef V4L2_PIX_FMT_RGB444
|
#if defined(V4L2_PIX_FMT_RGB444) && defined(PIX_FMT_RGB444)
|
||||||
case V4L2_PIX_FMT_RGB444 :
|
case V4L2_PIX_FMT_RGB444 :
|
||||||
pixFormat = PIX_FMT_RGB444;
|
pixFormat = PIX_FMT_RGB444;
|
||||||
break;
|
break;
|
||||||
|
@ -423,6 +423,7 @@ LocalCamera::LocalCamera( int p_id, const std::string &p_device, int p_channel,
|
||||||
Panic("Unexpected colours: %d",colours);
|
Panic("Unexpected colours: %d",colours);
|
||||||
}
|
}
|
||||||
if( capture ) {
|
if( capture ) {
|
||||||
|
#if LIBSWSCALE_VERSION_INT >= AV_VERSION_INT(0, 8, 0)
|
||||||
if(!sws_isSupportedInput(capturePixFormat)) {
|
if(!sws_isSupportedInput(capturePixFormat)) {
|
||||||
Error("swscale does not support the used capture format: %c%c%c%c",(capturePixFormat)&0xff,((capturePixFormat>>8)&0xff),((capturePixFormat>>16)&0xff),((capturePixFormat>>24)&0xff));
|
Error("swscale does not support the used capture format: %c%c%c%c",(capturePixFormat)&0xff,((capturePixFormat>>8)&0xff),((capturePixFormat>>16)&0xff),((capturePixFormat>>24)&0xff));
|
||||||
conversion_type = 2; /* Try ZM format conversions */
|
conversion_type = 2; /* Try ZM format conversions */
|
||||||
|
@ -431,6 +432,7 @@ LocalCamera::LocalCamera( int p_id, const std::string &p_device, int p_channel,
|
||||||
Error("swscale does not support the target format: %c%c%c%c",(imagePixFormat)&0xff,((imagePixFormat>>8)&0xff),((imagePixFormat>>16)&0xff),((imagePixFormat>>24)&0xff));
|
Error("swscale does not support the target format: %c%c%c%c",(imagePixFormat)&0xff,((imagePixFormat>>8)&0xff),((imagePixFormat>>16)&0xff),((imagePixFormat>>24)&0xff));
|
||||||
conversion_type = 2; /* Try ZM format conversions */
|
conversion_type = 2; /* Try ZM format conversions */
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
/* Don't have swscale, see what we can do */
|
/* Don't have swscale, see what we can do */
|
||||||
|
|
642
src/zm_mpeg.cpp
642
src/zm_mpeg.cpp
|
@ -25,27 +25,11 @@
|
||||||
#include "zm_mpeg.h"
|
#include "zm_mpeg.h"
|
||||||
|
|
||||||
#if HAVE_LIBAVCODEC
|
#if HAVE_LIBAVCODEC
|
||||||
extern "C" {
|
extern "C"
|
||||||
#include <libavutil/mathematics.h>
|
|
||||||
}
|
|
||||||
|
|
||||||
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(54, 1, 0)
|
|
||||||
static int encode_frame(AVCodecContext *c, AVFrame *frame)
|
|
||||||
{
|
{
|
||||||
AVPacket pkt = { 0 };
|
#include <libavutil/mathematics.h>
|
||||||
int ret, got_output;
|
#include <libavcodec/avcodec.h>
|
||||||
|
|
||||||
av_init_packet(&pkt);
|
|
||||||
av_init_packet(&pkt);
|
|
||||||
ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
|
|
||||||
if (ret < 0)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
ret = pkt.size;
|
|
||||||
av_free_packet(&pkt);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
bool VideoStream::initialised = false;
|
bool VideoStream::initialised = false;
|
||||||
|
|
||||||
|
@ -53,41 +37,90 @@ VideoStream::MimeData VideoStream::mime_data[] = {
|
||||||
{ "asf", "video/x-ms-asf" },
|
{ "asf", "video/x-ms-asf" },
|
||||||
{ "swf", "application/x-shockwave-flash" },
|
{ "swf", "application/x-shockwave-flash" },
|
||||||
{ "flv", "video/x-flv" },
|
{ "flv", "video/x-flv" },
|
||||||
{ "mp4", "video/mp4" },
|
{ "mov", "video/quicktime" }
|
||||||
{ "move", "video/quicktime" }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void VideoStream::Initialise( )
|
void VideoStream::Initialise( )
|
||||||
{
|
{
|
||||||
|
if ( logDebugging() )
|
||||||
|
av_log_set_level( AV_LOG_DEBUG );
|
||||||
|
else
|
||||||
|
av_log_set_level( AV_LOG_QUIET );
|
||||||
|
|
||||||
av_register_all( );
|
av_register_all( );
|
||||||
|
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(53, 13, 0)
|
||||||
|
avformat_network_init();
|
||||||
|
#endif
|
||||||
initialised = true;
|
initialised = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void VideoStream::SetupFormat( const char *p_filename, const char *p_format )
|
void VideoStream::SetupFormat( )
|
||||||
{
|
{
|
||||||
filename = p_filename;
|
|
||||||
format = p_format;
|
|
||||||
|
|
||||||
/* auto detect the output format from the name. default is mpeg. */
|
|
||||||
of = av_guess_format( format, NULL, NULL);
|
|
||||||
if ( !of )
|
|
||||||
{
|
|
||||||
Warning( "Could not deduce output format from file extension: using mpeg" );
|
|
||||||
of = av_guess_format("mpeg", NULL, NULL);
|
|
||||||
}
|
|
||||||
if ( !of )
|
|
||||||
{
|
|
||||||
Fatal( "Could not find suitable output format" );
|
|
||||||
}
|
|
||||||
|
|
||||||
/* allocate the output media context */
|
/* allocate the output media context */
|
||||||
ofc = (AVFormatContext *)av_mallocz(sizeof(AVFormatContext));
|
ofc = NULL;
|
||||||
|
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(53, 5, 0)
|
||||||
|
avformat_alloc_output_context2( &ofc, NULL, format, filename );
|
||||||
|
#else
|
||||||
|
AVFormatContext *s= avformat_alloc_context();
|
||||||
|
if(!s)
|
||||||
|
{
|
||||||
|
Fatal( "avformat_alloc_context failed %d \"%s\"", (size_t)ofc, av_err2str((size_t)ofc) );
|
||||||
|
}
|
||||||
|
|
||||||
|
AVOutputFormat *oformat;
|
||||||
|
if (format) {
|
||||||
|
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(52, 45, 0)
|
||||||
|
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 {
|
||||||
|
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(52, 45, 0)
|
||||||
|
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);
|
||||||
|
if (!s->priv_data)
|
||||||
|
{
|
||||||
|
Fatal( "Could not allocate private data for output format." );
|
||||||
|
}
|
||||||
|
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(51, 10, 0)
|
||||||
|
if (s->oformat->priv_class) {
|
||||||
|
*(const AVClass**)s->priv_data = s->oformat->priv_class;
|
||||||
|
av_opt_set_defaults(s->priv_data);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
s->priv_data = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(filename)
|
||||||
|
{
|
||||||
|
snprintf( s->filename, sizeof(s->filename), filename );
|
||||||
|
}
|
||||||
|
|
||||||
|
ofc = s;
|
||||||
|
#endif
|
||||||
if ( !ofc )
|
if ( !ofc )
|
||||||
{
|
{
|
||||||
Panic( "Memory error" );
|
Fatal( "avformat_alloc_..._context failed: %d", ofc );
|
||||||
}
|
}
|
||||||
ofc->oformat = of;
|
|
||||||
snprintf( ofc->filename, sizeof(ofc->filename), "%s", filename );
|
of = ofc->oformat;
|
||||||
|
Debug( 1, "Using output format: %s (%s)", of->name, of->long_name );
|
||||||
}
|
}
|
||||||
|
|
||||||
void VideoStream::SetupCodec( int colours, int subpixelorder, int width, int height, int bitrate, double frame_rate )
|
void VideoStream::SetupCodec( int colours, int subpixelorder, int width, int height, int bitrate, double frame_rate )
|
||||||
|
@ -130,77 +163,117 @@ void VideoStream::SetupCodec( int colours, int subpixelorder, int width, int hei
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( strcmp( "rtp", of->name ) == 0 )
|
||||||
|
{
|
||||||
|
// RTP must have a packet_size.
|
||||||
|
// Not sure what this value should be really...
|
||||||
|
ofc->packet_size = width*height;
|
||||||
|
|
||||||
|
if ( of->video_codec == AV_CODEC_ID_NONE)
|
||||||
|
{
|
||||||
|
// RTP does not have a default codec in ffmpeg <= 0.8.
|
||||||
|
of->video_codec = CODEC_ID_MPEG4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_AVCODECID codec_id = of->video_codec;
|
||||||
|
if ( codec_name )
|
||||||
|
{
|
||||||
|
AVCodec *a = avcodec_find_encoder_by_name(codec_name);
|
||||||
|
if ( a )
|
||||||
|
{
|
||||||
|
codec_id = a->id;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(53, 11, 0)
|
||||||
|
Debug( 1, "Could not find codec \"%s\". Using default \"%s\"", codec_name, avcodec_get_name( codec_id ) );
|
||||||
|
#else
|
||||||
|
Debug( 1, "Could not find codec \"%s\". Using default \"%d\"", codec_name, codec_id );
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* add the video streams using the default format codecs
|
/* add the video streams using the default format codecs
|
||||||
and initialize the codecs */
|
and initialize the codecs */
|
||||||
ost = NULL;
|
ost = NULL;
|
||||||
if (of->video_codec != AV_CODEC_ID_NONE)
|
if ( codec_id != AV_CODEC_ID_NONE )
|
||||||
{
|
{
|
||||||
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(53, 4, 0)
|
codec = avcodec_find_encoder( codec_id );
|
||||||
ost = av_new_stream(ofc, 0);
|
if ( !codec )
|
||||||
|
{
|
||||||
|
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(53, 11, 0)
|
||||||
|
Fatal( "Could not find encoder for '%s'", avcodec_get_name( codec_id ) );
|
||||||
#else
|
#else
|
||||||
ost = avformat_new_stream(ofc, 0);
|
Fatal( "Could not find encoder for '%d'", codec_id );
|
||||||
#endif
|
#endif
|
||||||
if (!ost)
|
|
||||||
{
|
|
||||||
Panic( "Could not alloc stream" );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if ZM_FFMPEG_SVN
|
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(53, 11, 0)
|
||||||
|
Debug( 1, "Found encoder for '%s'", avcodec_get_name( codec_id ) );
|
||||||
|
#else
|
||||||
|
Debug( 1, "Found encoder for '%d'", codec_id );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(53, 10, 0)
|
||||||
|
ost = avformat_new_stream( ofc, codec );
|
||||||
|
#else
|
||||||
|
ost = av_new_stream( ofc, 0 );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if ( !ost )
|
||||||
|
{
|
||||||
|
Fatal( "Could not alloc stream" );
|
||||||
|
}
|
||||||
|
ost->id = ofc->nb_streams - 1;
|
||||||
|
|
||||||
|
Debug( 1, "Allocated stream" );
|
||||||
|
|
||||||
AVCodecContext *c = ost->codec;
|
AVCodecContext *c = ost->codec;
|
||||||
#else
|
|
||||||
AVCodecContext *c = &ost->codec;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
c->codec_id = of->video_codec;
|
c->codec_id = codec->id;
|
||||||
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(51,2,1)
|
c->codec_type = codec->type;
|
||||||
c->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
||||||
#else
|
|
||||||
c->codec_type = CODEC_TYPE_VIDEO;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* put sample parameters */
|
c->pix_fmt = strcmp( "mjpeg", ofc->oformat->name ) == 0 ? PIX_FMT_YUVJ422P : PIX_FMT_YUV420P;
|
||||||
|
if ( bitrate <= 100 )
|
||||||
|
{
|
||||||
|
// 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.
|
||||||
|
c->flags |= CODEC_FLAG_QSCALE;
|
||||||
|
c->global_quality = FF_QP2LAMBDA * (31 - (31 * (bitrate / 100.0)));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
c->bit_rate = bitrate;
|
c->bit_rate = bitrate;
|
||||||
c->pix_fmt = PIX_FMT_YUV420P;
|
}
|
||||||
|
|
||||||
/* resolution must be a multiple of two */
|
/* resolution must be a multiple of two */
|
||||||
c->width = width;
|
c->width = width;
|
||||||
c->height = height;
|
c->height = height;
|
||||||
#if ZM_FFMPEG_SVN
|
|
||||||
/* time base: this is the fundamental unit of time (in seconds) in terms
|
/* time base: this is the fundamental unit of time (in seconds) in terms
|
||||||
of which frame timestamps are represented. for fixed-fps content,
|
of which frame timestamps are represented. for fixed-fps content,
|
||||||
timebase should be 1/framerate and timestamp increments should be
|
timebase should be 1/framerate and timestamp increments should be
|
||||||
identically 1. */
|
identically 1. */
|
||||||
//c->time_base.den = (int)(frame_rate*100);
|
|
||||||
//c->time_base.num = 100;
|
|
||||||
c->time_base.den = frame_rate;
|
c->time_base.den = frame_rate;
|
||||||
c->time_base.num = 1;
|
c->time_base.num = 1;
|
||||||
#else
|
|
||||||
/* frames per second */
|
Debug( 1, "Will encode in %d fps.", c->time_base.den );
|
||||||
c->frame_rate = frame_rate;
|
|
||||||
c->frame_rate_base = 1;
|
/* emit one intra frame every second */
|
||||||
#endif
|
c->gop_size = frame_rate;
|
||||||
//c->gop_size = frame_rate/2; /* emit one intra frame every half second or so */
|
|
||||||
c->gop_size = 12;
|
|
||||||
if ( c->gop_size < 3 )
|
|
||||||
c->gop_size = 3;
|
|
||||||
// some formats want stream headers to be seperate
|
// some formats want stream headers to be seperate
|
||||||
if(!strcmp(ofc->oformat->name, "mp4") || !strcmp(ofc->oformat->name, "mov") || !strcmp(ofc->oformat->name, "3gp"))
|
if ( of->flags & AVFMT_GLOBALHEADER )
|
||||||
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
|
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Fatal( "of->video_codec == AV_CODEC_ID_NONE" );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void VideoStream::SetParameters( )
|
void VideoStream::SetParameters( )
|
||||||
{
|
{
|
||||||
/* set the output parameters (must be done even if no
|
|
||||||
parameters). */
|
|
||||||
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(53, 4, 0)
|
|
||||||
if ( av_set_parameters(ofc, NULL) < 0 )
|
|
||||||
#else
|
|
||||||
if ( avformat_write_header(ofc, NULL) < 0 )
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
Panic( "Invalid output format parameters" );
|
|
||||||
}
|
|
||||||
//dump_format(ofc, 0, filename, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *VideoStream::MimeType( ) const
|
const char *VideoStream::MimeType( ) const
|
||||||
|
@ -209,60 +282,59 @@ const char *VideoStream::MimeType() const
|
||||||
{
|
{
|
||||||
if ( strcmp( format, mime_data[i].format ) == 0 )
|
if ( strcmp( format, mime_data[i].format ) == 0 )
|
||||||
{
|
{
|
||||||
|
Debug( 1, "MimeType is \"%s\"", mime_data[i].mime_type );
|
||||||
return ( mime_data[i].mime_type);
|
return ( mime_data[i].mime_type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const char *mime_type = of->mime_type;
|
const char *mime_type = of->mime_type;
|
||||||
if ( !mime_type )
|
if ( !mime_type )
|
||||||
{
|
{
|
||||||
mime_type = "video/mpeg";
|
std::string mime = "video/";
|
||||||
|
mime = mime.append( format );
|
||||||
|
mime_type = mime.c_str( );
|
||||||
Warning( "Unable to determine mime type for '%s' format, using '%s' as default", format, mime_type );
|
Warning( "Unable to determine mime type for '%s' format, using '%s' as default", format, mime_type );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Debug( 1, "MimeType is \"%s\"", mime_type );
|
||||||
|
|
||||||
return ( mime_type);
|
return ( mime_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
void VideoStream::OpenStream( )
|
void VideoStream::OpenStream( )
|
||||||
{
|
{
|
||||||
|
int avRet;
|
||||||
|
|
||||||
/* now that all the parameters are set, we can open the
|
/* now that all the parameters are set, we can open the
|
||||||
video codecs and allocate the necessary encode buffers */
|
video codecs and allocate the necessary encode buffers */
|
||||||
if ( ost )
|
if ( ost )
|
||||||
{
|
{
|
||||||
#if ZM_FFMPEG_SVN
|
|
||||||
AVCodecContext *c = ost->codec;
|
AVCodecContext *c = ost->codec;
|
||||||
#else
|
|
||||||
AVCodecContext *c = &ost->codec;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* find the video encoder */
|
|
||||||
AVCodec *codec = avcodec_find_encoder(c->codec_id);
|
|
||||||
if ( !codec )
|
|
||||||
{
|
|
||||||
Panic( "codec not found" );
|
|
||||||
}
|
|
||||||
|
|
||||||
/* open the codec */
|
/* open the codec */
|
||||||
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(53, 7, 0)
|
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(53, 7, 0)
|
||||||
if ( avcodec_open(c, codec) < 0 )
|
if ( (avRet = avcodec_open( c, codec )) < 0 )
|
||||||
#else
|
#else
|
||||||
if ( avcodec_open2(c, codec, 0) < 0 )
|
if ( (avRet = avcodec_open2( c, codec, 0 )) < 0 )
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
Panic( "Could not open codec" );
|
Fatal( "Could not open codec. Error code %d \"%s\"", avRet, av_err2str( avRet ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Debug( 1, "Opened codec" );
|
||||||
|
|
||||||
/* allocate the encoded raw picture */
|
/* allocate the encoded raw picture */
|
||||||
opicture = avcodec_alloc_frame( );
|
opicture = avcodec_alloc_frame( );
|
||||||
if ( !opicture )
|
if ( !opicture )
|
||||||
{
|
{
|
||||||
Panic( "Could not allocate opicture" );
|
Panic( "Could not allocate opicture" );
|
||||||
}
|
}
|
||||||
|
|
||||||
int size = avpicture_get_size( c->pix_fmt, c->width, c->height );
|
int size = avpicture_get_size( c->pix_fmt, c->width, c->height );
|
||||||
uint8_t *opicture_buf = (uint8_t *)av_malloc( size );
|
uint8_t *opicture_buf = (uint8_t *)av_malloc( size );
|
||||||
if ( !opicture_buf )
|
if ( !opicture_buf )
|
||||||
{
|
{
|
||||||
av_free( opicture );
|
av_free( opicture );
|
||||||
Panic( "Could not allocate opicture" );
|
Panic( "Could not allocate opicture_buf" );
|
||||||
}
|
}
|
||||||
avpicture_fill( (AVPicture *)opicture, opicture_buf, c->pix_fmt, c->width, c->height );
|
avpicture_fill( (AVPicture *)opicture, opicture_buf, c->pix_fmt, c->width, c->height );
|
||||||
|
|
||||||
|
@ -275,14 +347,14 @@ void VideoStream::OpenStream()
|
||||||
tmp_opicture = avcodec_alloc_frame( );
|
tmp_opicture = avcodec_alloc_frame( );
|
||||||
if ( !tmp_opicture )
|
if ( !tmp_opicture )
|
||||||
{
|
{
|
||||||
Panic( "Could not allocate temporary opicture" );
|
Panic( "Could not allocate tmp_opicture" );
|
||||||
}
|
}
|
||||||
int size = avpicture_get_size( pf, c->width, c->height );
|
int size = avpicture_get_size( pf, c->width, c->height );
|
||||||
uint8_t *tmp_opicture_buf = (uint8_t *)av_malloc( size );
|
uint8_t *tmp_opicture_buf = (uint8_t *)av_malloc( size );
|
||||||
if ( !tmp_opicture_buf )
|
if ( !tmp_opicture_buf )
|
||||||
{
|
{
|
||||||
av_free( tmp_opicture );
|
av_free( tmp_opicture );
|
||||||
Panic( "Could not allocate temporary opicture" );
|
Panic( "Could not allocate tmp_opicture_buf" );
|
||||||
}
|
}
|
||||||
avpicture_fill( (AVPicture *)tmp_opicture, tmp_opicture_buf, pf, c->width, c->height );
|
avpicture_fill( (AVPicture *)tmp_opicture, tmp_opicture_buf, pf, c->width, c->height );
|
||||||
}
|
}
|
||||||
|
@ -291,55 +363,141 @@ void VideoStream::OpenStream()
|
||||||
/* open the output file, if needed */
|
/* open the output file, if needed */
|
||||||
if ( !(of->flags & AVFMT_NOFILE) )
|
if ( !(of->flags & AVFMT_NOFILE) )
|
||||||
{
|
{
|
||||||
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(51,2,1)
|
int ret;
|
||||||
if ( avio_open(&ofc->pb, filename, AVIO_FLAG_WRITE) < 0 )
|
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(53, 14, 0)
|
||||||
|
ret = avio_open2( &ofc->pb, filename, AVIO_FLAG_WRITE, NULL, NULL );
|
||||||
|
#elif LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(52, 102, 0)
|
||||||
|
ret = avio_open( &ofc->pb, filename, AVIO_FLAG_WRITE );
|
||||||
#else
|
#else
|
||||||
if ( url_fopen(&ofc->pb, filename, AVIO_FLAG_WRITE) < 0 )
|
ret = url_fopen( &ofc->pb, filename, AVIO_FLAG_WRITE );
|
||||||
#endif
|
#endif
|
||||||
|
if ( ret < 0 )
|
||||||
{
|
{
|
||||||
Fatal( "Could not open '%s'", filename );
|
Fatal( "Could not open '%s'", filename );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Debug( 1, "Opened output \"%s\"", filename );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Fatal( "of->flags & AVFMT_NOFILE" );
|
||||||
}
|
}
|
||||||
|
|
||||||
video_outbuf = NULL;
|
video_outbuf = NULL;
|
||||||
if ( !(ofc->oformat->flags & AVFMT_RAWPICTURE) )
|
if ( !(of->flags & AVFMT_RAWPICTURE) )
|
||||||
{
|
{
|
||||||
/* allocate output buffer */
|
/* allocate output buffer */
|
||||||
/* XXX: API change will be done */
|
/* XXX: API change will be done */
|
||||||
video_outbuf_size = 200000;
|
// TODO: Make buffer dynamic.
|
||||||
|
video_outbuf_size = 4000000;
|
||||||
video_outbuf = (uint8_t *)malloc( video_outbuf_size );
|
video_outbuf = (uint8_t *)malloc( video_outbuf_size );
|
||||||
}
|
}
|
||||||
|
|
||||||
/* write the stream header, if any */
|
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(52, 100, 1)
|
||||||
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(53, 4, 0)
|
av_dump_format(ofc, 0, filename, 1);
|
||||||
av_write_header(ofc);
|
|
||||||
#else
|
#else
|
||||||
avformat_write_header(ofc, NULL);
|
dump_format(ofc, 0, filename, 1);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(53, 4, 0)
|
||||||
|
int ret = av_write_header( ofc );
|
||||||
|
#else
|
||||||
|
int ret = avformat_write_header( ofc, NULL );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if ( ret < 0 )
|
||||||
|
{
|
||||||
|
Fatal( "?_write_header failed with error %d \"%s\"", ret, av_err2str( ret ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
VideoStream::VideoStream( const char *filename, const char *format, int bitrate, double frame_rate, int colours, int subpixelorder, int width, int height )
|
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),
|
||||||
|
last_pts( -1 ),
|
||||||
|
streaming_thread(0),
|
||||||
|
do_streaming(true),
|
||||||
|
buffer_copy(NULL),
|
||||||
|
buffer_copy_lock(new pthread_mutex_t),
|
||||||
|
buffer_copy_used(0),
|
||||||
|
packet_index(0)
|
||||||
{
|
{
|
||||||
if ( !initialised )
|
if ( !initialised )
|
||||||
{
|
{
|
||||||
Initialise( );
|
Initialise( );
|
||||||
}
|
}
|
||||||
|
|
||||||
SetupFormat( filename, format );
|
if ( format )
|
||||||
|
{
|
||||||
|
int length = strlen(format);
|
||||||
|
codec_and_format = new char[length+1];;
|
||||||
|
strcpy( codec_and_format, format );
|
||||||
|
format = codec_and_format;
|
||||||
|
char *f = strchr(codec_and_format, '/');
|
||||||
|
if (f != NULL)
|
||||||
|
{
|
||||||
|
*f = 0;
|
||||||
|
codec_name = f+1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SetupFormat( );
|
||||||
SetupCodec( colours, subpixelorder, width, height, bitrate, frame_rate );
|
SetupCodec( colours, subpixelorder, width, height, bitrate, frame_rate );
|
||||||
SetParameters( );
|
SetParameters( );
|
||||||
|
|
||||||
|
// Allocate buffered packets.
|
||||||
|
packet_buffers = new AVPacket*[2];
|
||||||
|
packet_buffers[0] = new AVPacket();
|
||||||
|
packet_buffers[1] = new AVPacket();
|
||||||
|
packet_index = 0;
|
||||||
|
|
||||||
|
// Initialize mutex used by streaming thread.
|
||||||
|
if ( pthread_mutex_init( buffer_copy_lock, NULL ) != 0 )
|
||||||
|
{
|
||||||
|
Fatal("pthread_mutex_init failed");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
VideoStream::~VideoStream( )
|
VideoStream::~VideoStream( )
|
||||||
{
|
{
|
||||||
|
Debug( 1, "VideoStream destructor." );
|
||||||
|
|
||||||
|
// Stop streaming thread.
|
||||||
|
if ( streaming_thread )
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( buffer_copy != NULL )
|
||||||
|
{
|
||||||
|
av_free( buffer_copy );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( buffer_copy_lock )
|
||||||
|
{
|
||||||
|
if ( pthread_mutex_destroy( buffer_copy_lock ) != 0 )
|
||||||
|
{
|
||||||
|
Error( "pthread_mutex_destroy failed" );
|
||||||
|
}
|
||||||
|
delete buffer_copy_lock;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (packet_buffers) {
|
||||||
|
delete packet_buffers[0];
|
||||||
|
delete packet_buffers[1];
|
||||||
|
delete[] packet_buffers;
|
||||||
|
}
|
||||||
|
|
||||||
/* close each codec */
|
/* close each codec */
|
||||||
if ( ost )
|
if ( ost )
|
||||||
{
|
{
|
||||||
#if ZM_FFMPEG_SVN
|
|
||||||
avcodec_close( ost->codec );
|
avcodec_close( ost->codec );
|
||||||
#else
|
|
||||||
avcodec_close(&ost->codec);
|
|
||||||
#endif
|
|
||||||
av_free( opicture->data[0] );
|
av_free( opicture->data[0] );
|
||||||
av_free( opicture );
|
av_free( opicture );
|
||||||
if ( tmp_opicture )
|
if ( tmp_opicture )
|
||||||
|
@ -362,43 +520,80 @@ VideoStream::~VideoStream()
|
||||||
if ( !(of->flags & AVFMT_NOFILE) )
|
if ( !(of->flags & AVFMT_NOFILE) )
|
||||||
{
|
{
|
||||||
/* close the output file */
|
/* close the output file */
|
||||||
#if ZM_FFMPEG_SVN
|
|
||||||
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(51,2,1)
|
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(51,2,1)
|
||||||
avio_close( ofc->pb );
|
avio_close( ofc->pb );
|
||||||
#else
|
#else
|
||||||
url_fclose( ofc->pb );
|
url_fclose( ofc->pb );
|
||||||
#endif
|
|
||||||
#else
|
|
||||||
url_fclose(&ofc->pb);
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* free the stream */
|
/* free the stream */
|
||||||
av_free( ofc );
|
av_free( ofc );
|
||||||
|
|
||||||
|
/* free format and codec_name data. */
|
||||||
|
if ( codec_and_format )
|
||||||
|
{
|
||||||
|
delete codec_and_format;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
double VideoStream::EncodeFrame( const uint8_t *buffer, int buffer_size, bool add_timestamp, unsigned int timestamp )
|
double VideoStream::EncodeFrame( const uint8_t *buffer, int buffer_size, bool _add_timestamp, unsigned int _timestamp )
|
||||||
|
{
|
||||||
|
if ( pthread_mutex_lock( buffer_copy_lock ) != 0 )
|
||||||
|
{
|
||||||
|
Fatal( "EncodeFrame: pthread_mutex_lock failed." );
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buffer_copy_size < buffer_size)
|
||||||
|
{
|
||||||
|
if ( buffer_copy )
|
||||||
|
{
|
||||||
|
av_free( buffer_copy );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allocate a buffer to store source images for the streaming thread to encode.
|
||||||
|
buffer_copy = (uint8_t *)av_malloc( buffer_size );
|
||||||
|
if ( !buffer_copy )
|
||||||
|
{
|
||||||
|
Panic( "Could not allocate buffer_copy" );
|
||||||
|
}
|
||||||
|
buffer_copy_size = buffer_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
add_timestamp = _add_timestamp;
|
||||||
|
timestamp = _timestamp;
|
||||||
|
buffer_copy_used = buffer_size;
|
||||||
|
memcpy(buffer_copy, buffer, buffer_size);
|
||||||
|
|
||||||
|
if ( pthread_mutex_unlock( buffer_copy_lock ) != 0 )
|
||||||
|
{
|
||||||
|
Fatal( "EncodeFrame: pthread_mutex_unlock failed." );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( streaming_thread == 0 )
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
double VideoStream::ActuallyEncodeFrame( const uint8_t *buffer, int buffer_size, bool add_timestamp, unsigned int timestamp )
|
||||||
{
|
{
|
||||||
#ifdef HAVE_LIBSWSCALE
|
#ifdef HAVE_LIBSWSCALE
|
||||||
static struct SwsContext *img_convert_ctx = 0;
|
static struct SwsContext *img_convert_ctx = 0;
|
||||||
#endif // HAVE_LIBSWSCALE
|
#endif // HAVE_LIBSWSCALE
|
||||||
double pts = 0.0;
|
|
||||||
|
|
||||||
|
|
||||||
if (ost)
|
|
||||||
{
|
|
||||||
#if ZM_FFMPEG_048
|
|
||||||
pts = (double)ost->pts.val * ofc->pts_num / ofc->pts_den;
|
|
||||||
#else
|
|
||||||
pts = (double)ost->pts.val * ost->time_base.num / ost->time_base.den;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
#if ZM_FFMPEG_SVN
|
|
||||||
AVCodecContext *c = ost->codec;
|
AVCodecContext *c = ost->codec;
|
||||||
#else
|
|
||||||
AVCodecContext *c = &ost->codec;
|
|
||||||
#endif
|
|
||||||
if ( c->pix_fmt != pf )
|
if ( c->pix_fmt != pf )
|
||||||
{
|
{
|
||||||
memcpy( tmp_opicture->data[0], buffer, buffer_size );
|
memcpy( tmp_opicture->data[0], buffer, buffer_size );
|
||||||
|
@ -420,69 +615,138 @@ double VideoStream::EncodeFrame( const uint8_t *buffer, int buffer_size, bool ad
|
||||||
}
|
}
|
||||||
AVFrame *opicture_ptr = opicture;
|
AVFrame *opicture_ptr = opicture;
|
||||||
|
|
||||||
int ret = 0;
|
AVPacket *pkt = packet_buffers[packet_index];
|
||||||
if ( ofc->oformat->flags & AVFMT_RAWPICTURE )
|
av_init_packet( pkt );
|
||||||
|
int got_packet = 0;
|
||||||
|
if ( of->flags & AVFMT_RAWPICTURE )
|
||||||
{
|
{
|
||||||
#if ZM_FFMPEG_048
|
|
||||||
ret = av_write_frame( ofc, ost->index, (uint8_t *)opicture_ptr, sizeof(AVPicture) );
|
|
||||||
#else
|
|
||||||
AVPacket pkt;
|
|
||||||
av_init_packet( &pkt );
|
|
||||||
|
|
||||||
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(51, 2, 1)
|
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(51, 2, 1)
|
||||||
pkt.flags |= AV_PKT_FLAG_KEY;
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
||||||
#else
|
#else
|
||||||
pkt.flags |= PKT_FLAG_KEY;
|
pkt->flags |= PKT_FLAG_KEY;
|
||||||
#endif
|
|
||||||
pkt.stream_index = ost->index;
|
|
||||||
pkt.data = (uint8_t *)opicture_ptr;
|
|
||||||
pkt.size = sizeof(AVPicture);
|
|
||||||
|
|
||||||
ret = av_write_frame(ofc, &pkt);
|
|
||||||
#endif
|
#endif
|
||||||
|
pkt->stream_index = ost->index;
|
||||||
|
pkt->data = (uint8_t *)opicture_ptr;
|
||||||
|
pkt->size = sizeof (AVPicture);
|
||||||
|
got_packet = 1;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if ( add_timestamp )
|
opicture_ptr->pts = c->frame_number;
|
||||||
ost->pts.val = timestamp;
|
opicture_ptr->quality = c->global_quality;
|
||||||
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(54, 1, 0) // NEXTIME
|
|
||||||
int out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, opicture_ptr);
|
|
||||||
#else
|
|
||||||
int out_size = encode_frame(c, opicture_ptr);
|
|
||||||
|
|
||||||
#endif
|
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(54, 0, 0)
|
||||||
if ( out_size > 0 )
|
int ret = avcodec_encode_video2( c, pkt, opicture_ptr, &got_packet );
|
||||||
{
|
|
||||||
#if ZM_FFMPEG_048
|
|
||||||
ret = av_write_frame(ofc, ost->index, video_outbuf, out_size);
|
|
||||||
#else
|
|
||||||
AVPacket pkt;
|
|
||||||
av_init_packet(&pkt);
|
|
||||||
|
|
||||||
#if ZM_FFMPEG_049
|
|
||||||
pkt.pts = c->coded_frame->pts;
|
|
||||||
#else
|
|
||||||
pkt.pts= av_rescale_q( c->coded_frame->pts, c->time_base, ost->time_base );
|
|
||||||
#endif
|
|
||||||
if(c->coded_frame->key_frame)
|
|
||||||
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(51,2,1)
|
|
||||||
pkt.flags |= AV_PKT_FLAG_KEY;
|
|
||||||
#else
|
|
||||||
pkt.flags |= PKT_FLAG_KEY;
|
|
||||||
#endif
|
|
||||||
pkt.stream_index = ost->index;
|
|
||||||
pkt.data = video_outbuf;
|
|
||||||
pkt.size = out_size;
|
|
||||||
|
|
||||||
ret = av_write_frame( ofc, &pkt );
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ( ret != 0 )
|
if ( ret != 0 )
|
||||||
{
|
{
|
||||||
Fatal( "Error %d while writing video frame: %s", ret, strerror( errno ) );
|
Fatal( "avcodec_encode_video2 failed with errorcode %d \"%s\"", ret, av_err2str( ret ) );
|
||||||
}
|
}
|
||||||
return( pts );
|
#else
|
||||||
|
int out_size = avcodec_encode_video( c, video_outbuf, video_outbuf_size, opicture_ptr );
|
||||||
|
got_packet = out_size > 0 ? 1 : 0;
|
||||||
|
pkt->data = got_packet ? video_outbuf : NULL;
|
||||||
|
pkt->size = got_packet ? out_size : 0;
|
||||||
|
#endif
|
||||||
|
if ( got_packet )
|
||||||
|
{
|
||||||
|
if ( c->coded_frame->key_frame )
|
||||||
|
{
|
||||||
|
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(51,2,1)
|
||||||
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
||||||
|
#else
|
||||||
|
pkt->flags |= PKT_FLAG_KEY;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( pkt->pts != (int64_t)AV_NOPTS_VALUE )
|
||||||
|
{
|
||||||
|
pkt->pts = av_rescale_q( pkt->pts, c->time_base, ost->time_base );
|
||||||
|
}
|
||||||
|
if ( pkt->dts != (int64_t)AV_NOPTS_VALUE )
|
||||||
|
{
|
||||||
|
pkt->dts = av_rescale_q( pkt->dts, c->time_base, ost->time_base );
|
||||||
|
}
|
||||||
|
pkt->duration = av_rescale_q( pkt->duration, c->time_base, ost->time_base );
|
||||||
|
pkt->stream_index = ost->index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ( opicture_ptr->pts);
|
||||||
|
}
|
||||||
|
|
||||||
|
int VideoStream::SendPacket(AVPacket *packet) {
|
||||||
|
|
||||||
|
int ret = av_write_frame( ofc, packet );
|
||||||
|
if ( ret != 0 )
|
||||||
|
{
|
||||||
|
Fatal( "Error %d while writing video frame: %s", ret, av_err2str( errno ) );
|
||||||
|
}
|
||||||
|
av_free_packet(packet);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *VideoStream::StreamingThreadCallback(void *ctx){
|
||||||
|
|
||||||
|
Debug( 1, "StreamingThreadCallback started" );
|
||||||
|
|
||||||
|
if (ctx == NULL) return NULL;
|
||||||
|
|
||||||
|
VideoStream* videoStream = reinterpret_cast<VideoStream*>(ctx);
|
||||||
|
|
||||||
|
const uint64_t nanosecond_multiplier = 1000000000;
|
||||||
|
|
||||||
|
uint64_t target_interval_ns = nanosecond_multiplier * ( ((double)videoStream->ost->codec->time_base.num) / (videoStream->ost->codec->time_base.den) );
|
||||||
|
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;
|
||||||
|
while(videoStream->do_streaming)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
|
||||||
|
if ( current_time_ns < target_ns )
|
||||||
|
{
|
||||||
|
// It's not time to render a frame yet.
|
||||||
|
usleep( (target_ns - current_time_ns) * 0.001 );
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
av_free_packet(packet);
|
||||||
|
videoStream->packet_index = videoStream->packet_index ? 0 : 1;
|
||||||
|
|
||||||
|
// Lock buffer and render next frame.
|
||||||
|
|
||||||
|
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 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // HAVE_LIBAVCODEC
|
#endif // HAVE_LIBAVCODEC
|
||||||
|
|
|
@ -38,24 +38,42 @@ protected:
|
||||||
static struct MimeData mime_data[];
|
static struct MimeData mime_data[];
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
char *codec_and_format;
|
||||||
const char *filename;
|
const char *filename;
|
||||||
const char *format;
|
const char *format;
|
||||||
|
const char *codec_name;
|
||||||
enum PixelFormat pf;
|
enum PixelFormat pf;
|
||||||
AVOutputFormat *of;
|
AVOutputFormat *of;
|
||||||
AVFormatContext *ofc;
|
AVFormatContext *ofc;
|
||||||
AVStream *ost;
|
AVStream *ost;
|
||||||
|
AVCodec *codec;
|
||||||
AVFrame *opicture;
|
AVFrame *opicture;
|
||||||
AVFrame *tmp_opicture;
|
AVFrame *tmp_opicture;
|
||||||
uint8_t *video_outbuf;
|
uint8_t *video_outbuf;
|
||||||
int video_outbuf_size;
|
int video_outbuf_size;
|
||||||
double pts;
|
double last_pts;
|
||||||
|
|
||||||
|
pthread_t streaming_thread;
|
||||||
|
bool do_streaming;
|
||||||
|
uint8_t *buffer_copy;
|
||||||
|
bool add_timestamp;
|
||||||
|
unsigned int timestamp;
|
||||||
|
pthread_mutex_t *buffer_copy_lock;
|
||||||
|
int buffer_copy_size;
|
||||||
|
int buffer_copy_used;
|
||||||
|
AVPacket** packet_buffers;
|
||||||
|
int packet_index;
|
||||||
|
int SendPacket(AVPacket *packet);
|
||||||
|
static void* StreamingThreadCallback(void *ctx);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static void Initialise();
|
static void Initialise();
|
||||||
|
|
||||||
void SetupFormat( const char *p_filename, const char *format );
|
void SetupFormat( );
|
||||||
void SetupCodec( int colours, int subpixelorder, int width, int height, int bitrate, double frame_rate );
|
void SetupCodec( int colours, int subpixelorder, int width, int height, int bitrate, double frame_rate );
|
||||||
void SetParameters();
|
void SetParameters();
|
||||||
|
void ActuallyOpenStream();
|
||||||
|
double ActuallyEncodeFrame( const uint8_t *buffer, int buffer_size, bool add_timestamp=false, unsigned int timestamp=0 );
|
||||||
|
|
||||||
public:
|
public:
|
||||||
VideoStream( const char *filename, const char *format, int bitrate, double frame_rate, int colours, int subpixelorder, int width, int height );
|
VideoStream( const char *filename, const char *format, int bitrate, double frame_rate, int colours, int subpixelorder, int width, int height );
|
||||||
|
|
|
@ -292,7 +292,11 @@ int RemoteCameraRtsp::Capture( Image &image )
|
||||||
{
|
{
|
||||||
packet.data = buffer.head();
|
packet.data = buffer.head();
|
||||||
packet.size = buffer.size();
|
packet.size = buffer.size();
|
||||||
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 25, 0)
|
||||||
int len = avcodec_decode_video2( mCodecContext, mRawFrame, &frameComplete, &packet );
|
int len = avcodec_decode_video2( mCodecContext, mRawFrame, &frameComplete, &packet );
|
||||||
|
#else
|
||||||
|
int len = avcodec_decode_video( mCodecContext, mRawFrame, &frameComplete, packet.data, packet.size );
|
||||||
|
#endif
|
||||||
if ( len < 0 )
|
if ( len < 0 )
|
||||||
{
|
{
|
||||||
Error( "Error while decoding frame %d", frameCount );
|
Error( "Error while decoding frame %d", frameCount );
|
||||||
|
|
|
@ -26,12 +26,6 @@
|
||||||
|
|
||||||
#if HAVE_LIBAVCODEC
|
#if HAVE_LIBAVCODEC
|
||||||
|
|
||||||
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54,25,0)
|
|
||||||
#define _AVCODECID AVCodecID
|
|
||||||
#else
|
|
||||||
#define _AVCODECID CodecID
|
|
||||||
#endif
|
|
||||||
|
|
||||||
RtpSource::RtpSource( int id, const std::string &localHost, int localPortBase, const std::string &remoteHost, int remotePortBase, uint32_t ssrc, uint16_t seq, uint32_t rtpClock, uint32_t rtpTime, _AVCODECID codecId ) :
|
RtpSource::RtpSource( int id, const std::string &localHost, int localPortBase, const std::string &remoteHost, int remotePortBase, uint32_t ssrc, uint16_t seq, uint32_t rtpClock, uint32_t rtpTime, _AVCODECID codecId ) :
|
||||||
mId( id ),
|
mId( id ),
|
||||||
mSsrc( ssrc ),
|
mSsrc( ssrc ),
|
||||||
|
@ -387,6 +381,4 @@ bool RtpSource::getFrame( Buffer &buffer )
|
||||||
return( true );
|
return( true );
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef _AVCODECID
|
|
||||||
|
|
||||||
#endif // HAVE_LIBAVCODEC
|
#endif // HAVE_LIBAVCODEC
|
||||||
|
|
|
@ -30,12 +30,6 @@
|
||||||
|
|
||||||
#if HAVE_LIBAVCODEC
|
#if HAVE_LIBAVCODEC
|
||||||
|
|
||||||
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54,25,0)
|
|
||||||
#define _AVCODECID AVCodecID
|
|
||||||
#else
|
|
||||||
#define _AVCODECID CodecID
|
|
||||||
#endif
|
|
||||||
|
|
||||||
struct RtpDataHeader;
|
struct RtpDataHeader;
|
||||||
|
|
||||||
class RtpSource
|
class RtpSource
|
||||||
|
@ -189,8 +183,6 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#undef _AVCODECID
|
|
||||||
|
|
||||||
#endif // HAVE_LIBAVCODEC
|
#endif // HAVE_LIBAVCODEC
|
||||||
|
|
||||||
#endif // ZM_RTP_SOURCE_H
|
#endif // ZM_RTP_SOURCE_H
|
||||||
|
|
|
@ -331,11 +331,7 @@ int RtspThread::run()
|
||||||
uint32_t rtpClock = 0;
|
uint32_t rtpClock = 0;
|
||||||
std::string trackUrl = mUrl;
|
std::string trackUrl = mUrl;
|
||||||
|
|
||||||
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54,25,0)
|
_AVCODECID codecId;
|
||||||
enum AVCodecID codecId;
|
|
||||||
#else
|
|
||||||
enum CodecID codecId;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if ( mFormatContext->nb_streams >= 1 )
|
if ( mFormatContext->nb_streams >= 1 )
|
||||||
{
|
{
|
||||||
|
|
16
src/zm_sdp.h
16
src/zm_sdp.h
|
@ -40,12 +40,12 @@ protected:
|
||||||
{
|
{
|
||||||
int payloadType;
|
int payloadType;
|
||||||
const char payloadName[6];
|
const char payloadName[6];
|
||||||
enum AVMediaType codecType;
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 64, 0)
|
||||||
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54,25,0)
|
AVMediaType codecType;
|
||||||
enum AVCodecID codecId;
|
|
||||||
#else
|
#else
|
||||||
enum CodecID codecId;
|
enum CodecType codecType;
|
||||||
#endif
|
#endif
|
||||||
|
_AVCODECID codecId;
|
||||||
int clockRate;
|
int clockRate;
|
||||||
int autoChannels;
|
int autoChannels;
|
||||||
};
|
};
|
||||||
|
@ -53,12 +53,12 @@ protected:
|
||||||
struct DynamicPayloadDesc
|
struct DynamicPayloadDesc
|
||||||
{
|
{
|
||||||
const char payloadName[32];
|
const char payloadName[32];
|
||||||
enum AVMediaType codecType;
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 64, 0)
|
||||||
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54,25,0)
|
AVMediaType codecType;
|
||||||
enum AVCodecID codecId;
|
|
||||||
#else
|
#else
|
||||||
enum CodecID codecId;
|
enum CodecType codecType;
|
||||||
#endif
|
#endif
|
||||||
|
_AVCODECID codecId;
|
||||||
|
|
||||||
//int clockRate;
|
//int clockRate;
|
||||||
//int autoChannels;
|
//int autoChannels;
|
||||||
|
|
Loading…
Reference in New Issue