spacing, return a success value from setupCodec instead of using Fatal

This commit is contained in:
Isaac Connor 2022-01-10 10:13:53 -05:00
parent b406bf8fd1
commit 037771539d
2 changed files with 93 additions and 91 deletions

View File

@ -42,7 +42,7 @@ void VideoStream::SetupFormat( ) {
ofc = nullptr;
avformat_alloc_output_context2(&ofc, nullptr, format, filename);
if ( !ofc ) {
if (!ofc) {
Fatal("avformat_alloc_..._context failed");
}
@ -50,11 +50,18 @@ void VideoStream::SetupFormat( ) {
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 ) {
int VideoStream::SetupCodec(
int colours,
int subpixelorder,
int width,
int height,
int bitrate,
double frame_rate
) {
/* ffmpeg format matching */
switch ( colours ) {
switch (colours) {
case ZM_COLOUR_RGB24:
if ( subpixelorder == ZM_SUBPIX_ORDER_BGR ) {
if (subpixelorder == ZM_SUBPIX_ORDER_BGR) {
/* BGR subpixel order */
pf = AV_PIX_FMT_BGR24;
} else {
@ -63,13 +70,13 @@ void VideoStream::SetupCodec( int colours, int subpixelorder, int width, int hei
}
break;
case ZM_COLOUR_RGB32:
if ( subpixelorder == ZM_SUBPIX_ORDER_ARGB ) {
if (subpixelorder == ZM_SUBPIX_ORDER_ARGB) {
/* ARGB subpixel order */
pf = AV_PIX_FMT_ARGB;
} else if ( subpixelorder == ZM_SUBPIX_ORDER_ABGR ) {
} else if (subpixelorder == ZM_SUBPIX_ORDER_ABGR) {
/* ABGR subpixel order */
pf = AV_PIX_FMT_ABGR;
} else if ( subpixelorder == ZM_SUBPIX_ORDER_BGRA ) {
} else if (subpixelorder == ZM_SUBPIX_ORDER_BGRA) {
/* BGRA subpixel order */
pf = AV_PIX_FMT_BGRA;
} else {
@ -85,22 +92,22 @@ void VideoStream::SetupCodec( int colours, int subpixelorder, int width, int hei
break;
}
if ( strcmp("rtp", of->name) == 0 ) {
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;
Debug(1,"Setting packet_size to %d", ofc->packet_size);
if ( of->video_codec == AV_CODEC_ID_NONE ) {
if (of->video_codec == AV_CODEC_ID_NONE) {
// RTP does not have a default codec in ffmpeg <= 0.8.
of->video_codec = AV_CODEC_ID_MPEG4;
}
}
_AVCODECID codec_id = of->video_codec;
if ( codec_name ) {
if (codec_name) {
AVCodec *a = avcodec_find_encoder_by_name(codec_name);
if ( a ) {
if (a) {
codec_id = a->id;
Debug(1, "Using codec \"%s\"", codec_name);
} else {
@ -111,31 +118,29 @@ void VideoStream::SetupCodec( int colours, int subpixelorder, int width, int hei
/* add the video streams using the default format codecs
and initialize the codecs */
ost = nullptr;
if ( codec_id != AV_CODEC_ID_NONE ) {
if (codec_id != AV_CODEC_ID_NONE) {
codec = avcodec_find_encoder(codec_id);
if ( !codec ) {
Fatal("Could not find encoder for '%s'", avcodec_get_name(codec_id));
if (!codec) {
Error("Could not find encoder for '%s'", avcodec_get_name(codec_id));
return -1;
}
Debug(1, "Found encoder for '%s'", avcodec_get_name(codec_id));
ost = avformat_new_stream( ofc, codec );
if ( !ost ) {
Fatal( "Could not alloc stream" );
return;
ost = avformat_new_stream(ofc, codec);
if (!ost) {
Error("Could not alloc stream");
return -1;
}
Debug( 1, "Allocated stream (%d) !=? (%d)", ost->id , ofc->nb_streams - 1 );
Debug(1, "Allocated stream (%d) !=? (%d)", ost->id , ofc->nb_streams - 1);
ost->id = ofc->nb_streams - 1;
codec_context = avcodec_alloc_context3(nullptr);
//avcodec_parameters_to_context(codec_context, ost->codecpar);
codec_context->codec_id = codec->id;
codec_context->codec_type = codec->type;
codec_context->pix_fmt = strcmp("mjpeg", ofc->oformat->name) == 0 ? AV_PIX_FMT_YUVJ422P : AV_PIX_FMT_YUV420P;
if ( bitrate <= 100 ) {
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.
codec_context->flags |= AV_CODEC_FLAG_QSCALE;
@ -155,22 +160,22 @@ void VideoStream::SetupCodec( int colours, int subpixelorder, int width, int hei
codec_context->time_base.num = 1;
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 );
/* emit one intra frame every second */
codec_context->gop_size = frame_rate;
// some formats want stream headers to be separate
if ( of->flags & AVFMT_GLOBALHEADER )
if (of->flags & AVFMT_GLOBALHEADER)
codec_context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
avcodec_parameters_from_context(ost->codecpar, codec_context);
zm_dump_codecpar(ost->codecpar);
} else {
Fatal( "of->video_codec == AV_CODEC_ID_NONE" );
Error("of->video_codec == AV_CODEC_ID_NONE");
return -1;
}
return 0;
}
void VideoStream::SetParameters( ) {
@ -461,7 +466,6 @@ double VideoStream::EncodeFrame( const uint8_t *buffer, int buffer_size, bool _a
}
double VideoStream::ActuallyEncodeFrame( const uint8_t *buffer, int buffer_size, bool add_timestamp, unsigned int timestamp ) {
if ( codec_context->pix_fmt != pf ) {
static struct SwsContext *img_convert_ctx = nullptr;
memcpy( tmp_opicture->data[0], buffer, buffer_size );
@ -492,20 +496,19 @@ double VideoStream::ActuallyEncodeFrame( const uint8_t *buffer, int buffer_size,
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));
if (ret < 0) {
if (AVERROR_EOF != ret) {
Error("ERror encoding video (%d) (%s)", ret, av_err2str(ret));
}
} else {
got_packet = 1;
}
if ( got_packet ) {
// if ( c->coded_frame->key_frame )
// {
// pkt->flags |= AV_PKT_FLAG_KEY;
// }
if (got_packet) {
// if ( c->coded_frame->key_frame )
// {
// pkt->flags |= AV_PKT_FLAG_KEY;
// }
if ( pkt->pts != (int64_t)AV_NOPTS_VALUE ) {
pkt->pts = av_rescale_q( pkt->pts, codec_context->time_base, ost->time_base );
@ -518,16 +521,15 @@ double VideoStream::ActuallyEncodeFrame( const uint8_t *buffer, int buffer_size,
}
}
return ( opicture_ptr->pts);
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 ) );
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 );
av_packet_unref(packet);
return ret;
}

View File

@ -68,7 +68,7 @@ protected:
static void Initialise();
void SetupFormat( );
void SetupCodec( int colours, int subpixelorder, int width, int height, int bitrate, double frame_rate );
int SetupCodec( int colours, int subpixelorder, int width, int height, int bitrate, double frame_rate );
void SetParameters();
void ActuallyOpenStream();
double ActuallyEncodeFrame( const uint8_t *buffer, int buffer_size, bool add_timestamp=false, unsigned int timestamp=0 );